Skip to main content

Overview

The Agents API lets Lua runtime code — tools, webhooks, jobs, preprocessors, and postprocessors — invoke the current agent or another agent through the full chat pipeline. The invocation goes through billing, message persistence, skills and tools, preprocessors, postprocessors, and governance on the target agent.
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.
Use this inside agent primitives. /chat/generate and /chat/stream are external consumption APIs for apps and services. Agents.invoke is the runtime API for starting an agent turn from Lua code, so it does not require an API base URL or user bearer token.

Run Your Own Agent on a Schedule

Self-invocation is supported: pass the current agent’s ID as targetAgentId. This is useful when a LuaJob needs the agent itself to run a scheduled prompt with its normal skills and tools.
Agents.invoke currently requires an explicit targetAgentId; there is no self sentinel or ambient current-agent ID in the runtime API. A reusable template that self-invokes must still receive its deployed agent ID as configuration. It does not need an API URL or bearer token.

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.

Execution and delivery semantics

Agents.invoke waits for a complete agent turn and returns that turn’s response to the calling code. During the turn, the target agent’s tools execute normally and their side effects are real. The returned response is not automatically sent to a user or channel. Delivery only occurs if the invoked agent calls a delivery tool, or if the calling code sends result.text through a runtime API such as User or Channels.
  • In a user-authenticated tool, dynamic job, or processor, the ambient user is used automatically.
  • In a pre-defined LuaJob or userless webhook, pass userId when the invoked tools or later delivery need a user. Without it, the turn runs in system scope with no user profile or conversation history.
  • channel sets the target turn’s channel context. It does not, by itself, deliver the returned text to that channel.

Methods

Simplified: Agents.invoke(targetAgentId, prompt)

Invoke an agent with a plain text prompt. Returns the assistant’s response as a plain string.
string
required
The target agent’s identifier (e.g. 'sales-agent', 'support-agent'). Pass the current agent’s own ID to self-invoke. There is currently no self sentinel or ambient self-ID.
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.
string
required
The target agent’s identifier. Pass the current agent’s own ID to self-invoke.
AgentInvocationInput
required
Full invocation options — see below.
Returns: Promise<AgentInvocationOutput>

AgentInvocationInput

string
Plain-text user message. Mutually exclusive with messages.
UserContent
AI SDK UserContent (array of TextPart, ImagePart, FilePart). Mutually exclusive with prompt.
string
Override the target agent’s system prompt for this invocation only.
string
Additional runtime context string attached to the request (e.g. serialised metadata).
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.
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.
string
Channel context for the invoked turn. Defaults to 'agent-invocation'. This does not automatically deliver the returned response to a channel.
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.
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.
If the invoked agent uses User.get(), User.send(), or another user-scoped tool, include a real userId in the invocation. A system-scoped invocation cannot infer the template installer or a delivery recipient.

Multi-modal input (image analysis)

Limitations

  • No recursion guard — avoid infinite agent-invoke loops in your code.
  • Explicit target required — self-invocation works, but you must pass the current agent’s ID. There is no self sentinel or ambient self-ID yet.
  • 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

  • HTTP API — external /chat/generate and /chat/stream endpoints for apps and services consuming an agent
  • AI API — isolated text generation without the full agent pipeline
  • LuaWebhook — triggering agents from external events
  • LuaJob — scheduled agent invocations