AI in your app
Your app can use AI for its own users: suggest a class, draft a reply, summarise a week, sort new tickets before anyone looks. One call from a page or a workflow returns text. The model key never reaches the browser, and usage is metered to the project. Appbricx doesn't write your app with AI: your coding agent does that, on your own model subscription.
When to use it
- Suggestions behind a button: a class to take, a reply to send.
- Summaries of a record, a thread or a period.
- Triage in a workflow: tag or prioritise new rows before anyone looks.
- Hints with a system prompt that forbids giving the answer away.
From a page
The booking app's "Not sure what to take?" box:
import { useState } from "react";
import { useAppHandle } from "@appbricx/ui";
const FALLBACK = "Try Sunrise Flow: it suits every level.";
export function ClassFinder({ classes }: { classes: string[] }) {
const app = useAppHandle();
const [advice, setAdvice] = useState("");
const ask = async (feeling: string) => {
try {
const answer = await app.ai.ask("How I feel today: " + feeling + "\n\nClasses this week:\n" + classes.join("\n"), {
system: "You are the front desk of a small yoga and pilates studio. Recommend ONE class from the list and say why in two short sentences. Never invent classes.",
maxTokens: 600,
});
setAdvice(answer.trim() || FALLBACK); // an empty answer counts as a failure
} catch {
setAdvice(FALLBACK); // AI off, over budget, or no credits
}
};
// …
}A conversation keeps the turns and sends them each time:
const next = await app.ai.chat(
[...history, { role: "user", content: question }],
{ system: "You are a maths tutor. One hint at a time. Never give the final answer.", maxTokens: 400 },
);In a workflow
Keep the prompt in a template, prompts/triage.md, next to your other messages:
---
system: You sort support tickets. Reply with one word, urgent, normal or low, and nothing else.
max_tokens: 5
vars:
body: string
sample:
body: Nobody can pay at checkout since this morning.
---
Ticket: {{body}}// workflows/triage.ts
import { on } from "@appbricx/workflows";
type Ticket = { id: string; body: string };
export const triageTicket = on.created<Ticket>("ticket", async ({ row }, step) => {
let priority = "normal";
try {
const answer = (await step.prompt("triage", { body: row.body })).trim().toLowerCase();
if (answer === "urgent" || answer === "normal" || answer === "low") priority = answer;
} catch {
step.ctx.log("AI unavailable: the ticket keeps its default priority");
}
await step.sql("set priority", "update ticket set priority = $1 where id = $2", [priority, row.id]);
return { priority };
});step.prompt records the answer, so a retried run doesn't ask (or pay) twice. Its vars are typed from the template's front matter. step.ctx.ai.ask(prompt, { system, maxTokens }) asks without a template; put it inside step.run for the same reason.
Check it
message_previews { "project_id": "…" } // the triage prompt rendered with its sample
workflows { "project_id": "…", "action": "dry_run", "id": "triageticket",
"row": { "id": "…", "body": "Nobody can pay at checkout" } }Which key pays
Add your own model key (Anthropic, OpenAI and others) to your workspace's AI settings and pin it to the project, and your provider bills you directly at cost. With no key pinned, calls use your plan's AI credits (see pricing). The full order is in AI key resolution.
Gotchas
- Always have a fallback. Calls reject when AI is off, a budget is reached or there are no credits. Never make a core action depend on an answer.
- Leave room in
maxTokensfor free text; a tight cap can return an empty answer. Treat an empty string like a failure. - Call it on a click, not on every render. Each call is metered.
- Give it the facts. Pass the rows it should choose from and tell it not to invent others.
- Server actions don't have
ctx.aiin their types yet. Use the page's handle or a workflow.