Overview
The Agents API lets any piece of sandbox code — tools, webhooks, jobs, preprocessors, postprocessors — invoke a target agent through the full chat pipeline. The invocation goes through billing, message persistence, preprocessors, postprocessors, and governance on the target agent, exactly as if a user had sent a message directly.Full Pipeline Execution: Unlike
AI.generate, Agents.invoke routes through the target agent’s complete processing stack — including its skills, tools, preprocessors, postprocessors, and governance rules.Import
Calling Contexts
| Context | User identity | Recommended pattern |
|---|---|---|
| Tool (user turn) | Caller’s user automatically used | Agents.invoke(id, prompt) — no userId needed |
| Dynamic Job (Jobs API) | Caller’s user automatically used | Agents.invoke(id, prompt) — no userId needed |
| Webhook | No ambient user | Pass userId from event payload, or omit to run without a user identity |
| Pre-defined LuaJob | No ambient user | Pass userId from metadata, or omit to run without a user identity |
| PreProcessor / PostProcessor | Caller’s user automatically used | Agents.invoke(id, prompt) — no userId needed |
No user identity — when no
userId is available (e.g. a webhook with no user context), the invocation runs without a user identity and no conversation history is stored. Use userId from your event payload whenever you have one.Methods
Simplified: Agents.invoke(targetAgentId, prompt)
Invoke an agent with a plain text prompt. Returns the assistant’s response as a plain string.
The target agent’s identifier (e.g.
'sales-agent', 'support-agent').Plain-text message to send to the target agent.
Promise<string>
Full options: Agents.invoke(targetAgentId, input)
Invoke an agent with full control over the request. Returns a structured output object.
The target agent’s identifier.
Full invocation options — see below.
Promise<AgentInvocationOutput>
AgentInvocationInput
Plain-text user message. Mutually exclusive with
messages.AI SDK
UserContent (array of TextPart, ImagePart, FilePart). Mutually exclusive with prompt.Override the target agent’s system prompt for this invocation only.
Additional runtime context string attached to the request (e.g. serialised metadata).
Thread ID suffix for conversation scoping. When omitted, the invocation flows into the caller-user’s default chat thread with the target agent — the same behaviour as a direct message. Pass a custom value to isolate this invocation in its own thread.
Channel identifier. Defaults to
'agent-invocation'.Free-form request tag persisted on the stored message record (e.g. a UUID or external trace ID for correlation). Not a user identifier.
Explicit user to invoke as. Use from context-less triggers — webhooks and pre-defined jobs — where there is no ambient user. When omitted from a webhook or pre-defined job, the invocation runs without user identity and no conversation history is stored. This field is ignored during a user-authenticated turn; the turn’s user is always used.
AgentInvocationOutput
| Field | Type | Description |
|---|---|---|
text | string | Final response text (after the target agent’s postprocessors). |
finishReason | string? | AI SDK FinishReason — 'stop', 'length', 'content-filter', 'preprocessor_blocked', 'governance_blocked', etc. |
usage | object? | Token usage: { inputTokens, outputTokens, totalTokens, reasoningTokens, cachedInputTokens } |
toolsUsed | string[]? | Names of tools invoked by the target agent during generation. |
threadId | string? | Echoes back the threadId the caller passed in, if any. |
Error Handling
Agents.invoke throws an Error for any non-success response. Wrap calls in try/catch and inspect the message:
| Cause | Description |
|---|---|
| Target agent disabled | The target agent is not available to the invoking user. |
| Insufficient credits | The account does not have enough credits to run the invocation. |
| Timeout | The target agent took longer than 120 s to respond. |
| Service unreachable | The platform could not be reached (may occur during local lua test). |
Complete Examples
Tool routing to a specialist agent
Webhook delegating to an agent
When a webhook fires you typically have a user ID in the event payload — pass it viauserId so the invocation runs in that user’s context:
Pre-defined job with no user context
When nouserId is available, omit it and the invocation runs without user identity. No conversation history is stored.
Multi-modal input (image analysis)
Limitations
- No recursion guard — avoid infinite agent-invoke loops in your code.
- No native fire-and-forget —
Agents.invokeis always awaited. To run in the background, use the Jobs API to schedule a dynamic job. skillOverride/preprocessorOverridenot exposed — the target agent always runs with its configured skills and preprocessors.- Persona override not exposed — use
systemPromptto influence behaviour without replacing the full persona. - 120 s timeout — invocations that take longer than 120 seconds will throw a timeout error.
Related APIs
AI API
Generate text outside the agent pipeline
User API
Get or update user context
LuaWebhook
HTTP endpoints for external events
LuaJob
Pre-defined scheduled tasks
Jobs API
Dynamic job creation
PostProcessor
Format agent responses
See Also
- AI API — isolated text generation without the full agent pipeline
- LuaWebhook — triggering agents from external events
- LuaJob — scheduled agent invocations

