Add a Kai chat to your app
Give the visitors of your Python/JS app a ready-made Kai chat: turn on Kai in the app, mount the server relay, render the chat component, and know what Kai can read.
Turn on Kai in the app and your app’s visitors can ask Kai about the data behind the page they’re looking at. You don’t write the chat yourself: the app’s backend mounts a relay from @keboola/api-client, and the page renders a ready-made chat component. The Kai token stays on the server.
Use it when visitors want to ask follow-up questions that your charts and tables don’t answer, such as “Why did revenue drop last week?” or “Which regions are behind plan?”. Kai in an app only reads; it can’t change anything in the project.
On this page: Before you start · Turn on Kai in the app · Mount the relay · Render the chat · Customize the chat · What Kai can read · Beta limits · HTTP API · Troubleshooting
Before you start
Section titled “Before you start”You need:
- A Python/JS app with a React frontend and a Node (Express or Fetch) backend. Build locally covers the layout.
- Kai turned on in the project. See Kai.
- Storage Access turned on for the app. Kai works in the app’s workspace. See Storage Access.
- No row-level or column-level security. Kai isn’t available in an app with row or column policies, or in a project with the
row-level-securityfeature, yet. @keboola/api-client62.0.0 or later. Older versions have nokbc.kai.- React 19 in the app. The chat component needs
reactandreact-dom19.
The chat spends the project’s Kai credits, like any other Kai conversation.
1. Turn on Kai in the app
Section titled “1. Turn on Kai in the app”- Open the app in Apps and go to Advanced Settings.
- Turn on Kai in the app.
- Deploy the app again. Turning Kai on or off takes effect at the next deploy.

Each deploy gives the app its own Kai token, valid for 90 days. The platform puts it in the KAI_STORAGE_API_TOKEN environment variable and sets KAI_ENABLED. The relay in the next step reads both; your code doesn’t need to.
If Kai can’t be turned on, the setting says why, for example “Kai needs Storage access. Turn it on first.”
In an app with a Keboola-managed repository, Set up with Kai under the setting opens the Builder and asks Kai to check or add the app’s chat.
2. Mount the relay on the backend
Section titled “2. Mount the relay on the backend”The relay ships in @keboola/api-client. initializeApp() returns kai, which you mount on the app’s own server:
import express from 'express';import { initializeApp } from '@keboola/api-client/sdk/dataAppServer';
const app = express();const kbc = initializeApp();
app.use('/api/kai', kbc.kai.expressHandler());Mount it before any handler that answers unmatched /api paths with 404. You can mount it at another path, but the chat component expects /api/kai unless you tell it otherwise.
On a server built on the Fetch API (Request in, Response out), call the handler directly:
const response = await kbc.kai.handle(request);What the relay does for you:
- Picks the token. It uses
KAI_STORAGE_API_TOKENwhenKAI_ENABLEDis on, and neverKBC_TOKEN. It sends the app’s workspace with every request. - Keeps the token on the server. The browser talks only to your app. The token never reaches it.
- Rebuilds every chat request. It passes on one text message (up to 8,000 characters) and a flag that hides a message from the chat, such as the “continue” sent on reconnect. It drops everything else the browser sends, such as a model, project or branch. Kai always runs with read-only tools.
- Binds chats to the browser. The relay issues chat IDs and keeps them in a signed cookie (
HttpOnly; Secure; SameSite=None; Partitioned). A chat opens only in the browser that started it. The cookie keeps the last 5 chats. - Refuses approvals. A visitor can decline an action Kai proposes, never approve it.
- Refuses requests from other sites. A request from another origin gets 403; a request body over 64 KB gets 413.
- Streams answers. Kai’s streamed answer passes through unbuffered, also behind nginx.
Run it locally
Section titled “Run it locally”On your computer there’s no Kai token. To try the chat locally, let the relay use your own token from STORAGE_API_TOKEN:
const kbc = initializeApp({ kai: { allowPersonalToken: true } });The relay uses STORAGE_API_TOKEN only with this option and only while KAI_ENABLED is off. Kai then reads with your access, for every visitor of the app. Keep this option out of deployed apps.
3. Render the chat
Section titled “3. Render the chat”The browser half is the @keboola/kai-app-chat package. Install it in the app:
npm install @keboola/kai-app-chatAdd the component once, for example in your root layout:
import { KaiChat } from '@keboola/kai-app-chat';import '@keboola/kai-app-chat/styles.css';
export function App() { return ( <> {/* your app */} <KaiChat title="Ask Kai" /> </> );}By default the component is a button in the bottom-right corner of the page that opens a panel, full screen on a phone. Kai’s answers stream into the panel. When Kai is off for the app, the run has no Kai token, or the backend serves no relay at the chat’s path, it renders nothing.
Under Kai’s last finished answer, the chat offers up to two follow-up questions Kai suggests. A click asks one. Each finished answer has a copy button that copies the answer’s markdown.
When Kai can’t start, the chat shows the error with New chat to try again. When the chat’s code doesn’t download, for example while the visitor is offline, the error shows in the chat’s place or above the floating button, and Try again downloads it again. The rest of the page keeps working.
The tab keeps its chat in sessionStorage: a refresh brings the transcript back and joins an answer still running. A new tab starts a new chat; a duplicated tab shares the chat of the tab it copies. Where sessionStorage is blocked, as in some sandboxed frames, the chat lasts until a refresh.

Customize the chat
Section titled “Customize the chat”You can change the chat at three levels.
| Level | What you change | How |
|---|---|---|
| Theme | Light or dark mode, colors, corner radius | The colorScheme prop, and CSS variables on .kai-chat |
| Props | Title, greeting, suggested questions, placement, relay path | Props on <KaiChat> |
| Your own UI | The whole chat window | The useKaiChat() hook from @keboola/kai-app-chat |
colorScheme is light (the default), dark or system, which follows the visitor’s operating system.
<KaiChat colorScheme="system" />To change colors or the corner radius, set the design CSS variables on .kai-chat. Their values are RGB channels separated by spaces, not hex:
.kai-chat,.kai-chat[data-theme='dark'] { --primary: 234 88 12;}Put the rule in a stylesheet imported after @keboola/kai-app-chat/styles.css; otherwise the chat keeps its default color. The second selector applies the color in dark mode too. For a different color in each mode, write a separate rule for each selector.
The chat uses your app’s --font-sans CSS variable when the app defines one, otherwise Inter.
The chat’s styles are scoped to .kai-chat, so they don’t change the rest of your app. Your app’s unlayered rules on bare elements, such as strong { color: … } or button { padding: … }, still reach into the chat. A few rules in the stylesheet stay global: the Inter @font-face, Tailwind’s @property rules and @keyframes such as spin, pulse and fadeIn.
<KaiChat title="Ask Kai" greeting="Ask about the numbers on this page." suggestedPrompts={['Why did revenue drop last week?', 'Top 5 regions by margin']} placement="panel" endpoint="/api/kai"/>titleheads the chat. The default is “Ask Kai”.greetingshows while the chat is empty.suggestedPromptsoffers up to four questions while the chat is empty.placementisfloating(the default),panelorinline.floatingis a button in the bottom-right corner that opens a panel, full screen on a phone.panelandinlinefill their parent element.endpointis the path where you mounted the relay. The default is/api/kai.colorSchemeislight(the default),darkorsystem.defaultOpenopens a floating chat on page load. Otherwise a floating chat starts its chat and loads its code on first open, not on page load.classNameadds your own class to the chat’s root element.
Your own UI
Section titled “Your own UI”To build the chat window yourself, use the useKaiChat() hook. It gives you the chat’s state and actions, and no markup:
import { useKaiChat } from '@keboola/kai-app-chat';
const kai = useKaiChat(); // { endpoint: '/api/kai' } by defaultif (kai.enabled === false) return null;kai.send('Why did revenue drop?');enabledisundefineduntil the backend answers, andfalsewhen the app has no Kai or the backend serves no relay at the endpoint. Any other failure to reach the relay sets anunavailableerror.messagesare the chat’s messages, as AI SDKUIMessageobjects.statusis the AI SDK’s chat status,startinguntil the tab has its chat, orreconnectingwhile a dropped answer is retried.erroris{ type, message }, with a message fit for the visitor. A rate limit or an overloaded Kai addsretryAfterSeconds.send(text)returnsfalsewhen the question didn’t go out: the text is empty, it’s over 8,000 characters (this also sets atoo_longerror), or the chat can’t take one, for example while it’s still answering.stop()stops the answer.reset()starts a new chat. A chat the backend forgot, for example after a redeploy, fails until you callreset().
What visitors can ask and what Kai can read
Section titled “What visitors can ask and what Kai can read”Visitors can ask anything about the data Kai can read: totals, trends, comparisons, why a number changed, which rows stand out. Kai answers by querying the data, and it can explain how Keboola works. It can’t create, change or run anything.
Kai reads the same data the app reads. It depends on the app’s input mapping:
- Read-only access to the whole Storage (the default). Kai reads every bucket that exists in the app’s branch when the app is deployed. Its queries run in the app’s workspace.
- Direct Grant input mapping. Kai sees only the buckets of the tables the app maps, and its queries reach only the mapped tables. The access is granted per bucket, not per table: Kai lists every table in such a bucket with its columns and row count, also the tables the app doesn’t map. Keep a table that visitors must not learn about in a bucket the app doesn’t map.
Both depend on the buckets that exist at deploy time. Deploy the app again after you add a bucket or change the mapping.
All visitors of one app are one Kai user. Kai doesn’t know who is asking, and every visitor reads the same data, whoever signed in to the app. Row-level security per visitor isn’t available in the beta.
Limits of the beta
Section titled “Limits of the beta”- React apps only. Streamlit apps, apps with a Python backend (for example FastAPI) and apps without React have no ready-made chat yet. See Use the HTTP API instead.
- One Kai user per app. There are no per-visitor permissions and no row-level security per visitor.
- Read-only. Kai can’t change the project. The relay refuses every approval; declining passes.
- No answers to Kai’s questions. When Kai asks the visitor a follow-up question, the visitor can’t answer it yet.
- Chats stay in one browser. A chat opens only in the browser that started it. A redeploy gives the app a new token, which ends the open chats. On the next page load the chat starts a new one by itself.
- Message size. One message holds up to 8,000 characters of text. Files can’t be attached.
- One message rate limit per app. All visitors of an app share one message rate limit.
Use the HTTP API instead
Section titled “Use the HTTP API instead”If your app doesn’t use React, or you want full control, your backend can call Kai’s HTTP API itself. The app-building skill’s Kai integration reference (opens in a new tab) describes the token, the headers, the stream and a relay for an Express backend.
If you write your own relay, keep the same rules: the token stays on the server, and the relay passes on only “no” to an approval. Every visitor is the same Kai user, so an approval from one visitor would run Kai’s action for all of them.
Troubleshooting
Section titled “Troubleshooting”The chat doesn’t appear
Section titled “The chat doesn’t appear”The component renders nothing when Kai is off for the app or the run has no Kai token.
- Check that Kai in the app is on in Advanced Settings, then deploy the app again.
- Check the app’s logs. “Kai is on, but this run has no Kai token; deploy the app again.” means the app started without a Kai token. A new deploy by a project member gives it one.
- Check that the relay is mounted at the path the component calls (
/api/kaiby default).
Deploy fails after turning Kai on
Section titled “Deploy fails after turning Kai on”The Kai in the app setting says why. If it says “This project doesn’t allow Kai in apps, so deploys fail until you turn Kai off.”, contact support. Kai also needs Storage Access, and it isn’t available with row or column policies. Turn Kai off to deploy the app without it.
- “Kai requests must come from the app itself.” The request came from another site. Serve the page and the relay from the same app.
- “Visitors cannot approve Kai actions.” The chat sent an approval. In an app, a visitor can only decline.
404 “Chat not found”
Section titled “404 “Chat not found””The backend no longer knows the chat. It was started in another browser, the cookie was cleared, the browser has started 5 newer chats since, or the app was deployed again.
When this happens on page load, the chat starts a new chat by itself. Only a chat lost while the tab is open shows an error. The error has no button: the visitor starts a new chat with the pencil icon (New chat) in the chat’s header.
If you build your own UI, create chats one at a time. When two new chats are created at the same moment, the browser keeps only the second, and the first answers 404.
Kai counts an app as one user. All visitors of one app share its limits. The limits of one app don’t affect another app or the author’s own Kai.
A 429 means one of these limits was reached:
- Too many messages in a short time. Kai limits how fast messages can come. Wait and send again. You can’t change this limit.
- The per-user Kai spend limit. It applies to the app as one user.
- The project’s monthly Kai budget.
Set the spend limits in Settings → Kai Agent → Kai spend limits. See Kai pricing and limits. When the response has a Retry-After header, the chat can send again after that time.
Was this page helpful?
Thank you for helping improve Keboola's documentation!