16. AI Integration
promptAgent(agentId, message)
async· returnsstring
Sends a message to one of your application's agents and returns its reply as text.
Prop
Type
const summary = await context.functions.promptAgent(
"support-summarizer",
"Summarize this complaint in one sentence: " + context.record.ComplaintText
);
await context.functions.update("complaints", context.record.ID, { AISummary: summary });The return value is a plain string — the agent's text response. There is no message array, no choices, no token usage.
The model, system prompt, temperature, and any tools the agent can use all live in the agent's definition. A process supplies only the message. To change how the agent behaves, edit the agent rather than the process.
This is the only AI function
promptAgent is the entire AI surface for business processes. CallAIAsync, CallAIWithAttachmentAsync, and CallMilevaAsync belonged to the previous backend and no longer exist. Neither context.functions.ai nor context.functions.db exists either — you may see those suggested by AI-generated code, but they throw at runtime.
There is no way to attach a file to a prompt. Extract the text first and include it in the message:
const file = await context.functions.GetFileAsync(context.record.DocumentId);
if (!file) throw new Error("Document is missing");
const text = new TextDecoder().decode(file.bytes);
const answer = await context.functions.promptAgent("document-reader", "Extract the total amount from:\n\n" + text);This one throws
Unlike http, which reports failure through its return value, promptAgent raises an error when the agent is missing, misconfigured, or the model call fails. An unhandled error aborts the process — and inside a Before* process, aborts the write that started it.
/** @event beforeCreate @entity complaints */
try {
context.record.AISummary = await context.functions.promptAgent("support-summarizer", context.record.Text);
} catch (e) {
console.error("Summarization unavailable: " + e.message);
context.record.AISummary = null; // save the complaint regardless
}Decide deliberately whether an AI failure should block the business operation. Usually it should not.
Keeping it predictable
Model responses are free-form text. When you need structure, ask for it and validate what comes back:
const raw = await context.functions.promptAgent(
"invoice-extractor",
'Return only JSON of the form {"vendor":"","total":0} for:\n\n' + text
);
let parsed;
try {
parsed = JSON.parse(raw);
} catch {
throw new Error("Agent did not return valid JSON: " + raw.slice(0, 200));
}
await context.functions.update("invoices", context.record.ID, {
Vendor: parsed.vendor,
Total: parsed.total
});A prompt can take many seconds, and the default process timeout is 30. Raise it with @timeout when calling an agent, and avoid prompting inside a loop over records — a Before* process that calls an agent adds that latency to every single save.
/** @trigger Summarize complaint @event afterCreate @entity complaints @timeout 120 */