AI
Ask the app's AI for summaries, suggestions, drafts and classifications. Calls go through /__appbricx/ai/chat with the project's own key or plan credits; the provider key never reaches the browser.
On this page
AI is metered to the project, with per-app and per-user budgets. A call rejects when AI is off for the project, the budget is spent or there are no credits: catch it and show a fallback.
app.ai
app.ai.askmethod
One answer for one prompt.
app.ai.ask(prompt: string, opts?: AppAiOptions): Promise<string>Parameters
| Name | Type | Description |
|---|---|---|
promptrequired | string | What to ask. |
optsoptional | AppAiOptions | { system?, maxTokens? }: the assistant's role and rules, and an upper bound on the answer's length (the project's cap still applies). |
- Returns
- The answer text.
- Errors
- "AI isn't available in this preview" without a runtime.
- The server's message: AI_DISABLED_FOR_PROJECT, BUDGET_EXCEEDED ("Per-user token budget exceeded" or "Project token budget exceeded"), INSUFFICIENT_CREDITS, RATE_LIMITED, PROVIDER_ERROR.
- Permissions
- Any caller of the app may ask; spend is metered to the project and to the end user, and the model allow-list applies.
try {
const summary = await app.ai.ask(`Summarize in two sentences:\n${ticket.body}`, { maxTokens: 120 });
setSummary(summary);
} catch {
setSummary("Summary unavailable.");
}Source: packages/appkit/src/kits/primitives/app-handle.ts:107
app.ai.chatmethod
The next assistant turn of a conversation.
app.ai.chat(messages: AppAiMessage[], opts?: AppAiOptions): Promise<string>Parameters
| Name | Type | Description |
|---|---|---|
messagesrequired | AppAiMessage[] | The conversation so far: [{ role: "user" | "assistant", content }]. Empty turns are dropped; at most 64 messages. |
optsoptional | AppAiOptions | { system?, maxTokens? }. |
- Returns
- The assistant's reply text.
- Errors
- "ask the AI something first" when no non-empty turn is left.
- The same server errors as ask.
- Permissions
- As ask.
const reply = await app.ai.chat(
[...history, { role: "user", content: input }],
{ system: "You are the help desk for Tablefor. Be brief." },
);
setHistory((h) => [...h, { role: "user", content: input }, { role: "assistant", content: reply }]);Source: packages/appkit/src/kits/primitives/app-handle.ts:109
Types
type AppAiMessage = {
role: "user" | "assistant";
content: string;
}Source: packages/appkit/src/kits/primitives/app-handle.ts:93
type AppAiOptions = {
system?: string;
maxTokens?: number;
}Source: packages/appkit/src/kits/primitives/app-handle.ts:94