Skip to main content

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

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.
targetAgentId
string
required
The target agent’s identifier (e.g. 'sales-agent', 'support-agent').
prompt
string
required
Plain-text message to send to the target agent.
Returns: Promise<string>

Full options: Agents.invoke(targetAgentId, input)

Invoke an agent with full control over the request. Returns a structured output object.
targetAgentId
string
required
The target agent’s identifier.
input
AgentInvocationInput
required
Full invocation options — see below.
Returns: Promise<AgentInvocationOutput>

AgentInvocationInput

prompt
string
Plain-text user message. Mutually exclusive with messages.
messages
UserContent
AI SDK UserContent (array of TextPart, ImagePart, FilePart). Mutually exclusive with prompt.
systemPrompt
string
Override the target agent’s system prompt for this invocation only.
runtimeContext
string
Additional runtime context string attached to the request (e.g. serialised metadata).
clientContext
object
Client-side context for the invocation. Supports timezone — an IANA timezone string (e.g. 'Africa/Nairobi') used as the user’s local timezone for date/time-aware responses. When omitted, the target agent falls back to the user’s stored profile, country, or UTC.
threadId
string
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
string
Channel identifier. Defaults to 'agent-invocation'.
identifier
string
Free-form request tag persisted on the stored message record (e.g. a UUID or external trace ID for correlation). Not a user identifier.
userId
string
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

Error Handling

Agents.invoke throws an Error for any non-success response. Wrap calls in try/catch and inspect the message:
Common error causes:

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 via userId so the invocation runs in that user’s context:

Pre-defined job with no user context

When no userId 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-forgetAgents.invoke is always awaited. To run in the background, use the Jobs API to schedule a dynamic job.
  • skillOverride / preprocessorOverride not exposed — the target agent always runs with its configured skills and preprocessors.
  • Persona override not exposed — use systemPrompt to influence behaviour without replacing the full persona.
  • 120 s timeout — invocations that take longer than 120 seconds will throw a timeout error.

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