Skip to main content
A sub-agent is a PluginSubAgent the runtime auto-wraps as a tool (e.g. weather_planner_agent). Use one when a task needs more than one tool call in sequence or its own prompt.
1

Build the inner tools the sub-agent will call

Sub-agents own a private tool list. Reuse plugin tools or define new ones in the same file.
Canonical source: weather-sub-agent.ts.
2

Define the PluginSubAgent

Give the sub-agent a name (the tool the main agent sees), a description that reads like a tool description, a systemPrompt, and its tools.
systemPrompt and tools can both be functions of PluginContext for late binding. See buildWeatherPlannerSubAgent in weather-sub-agent.ts.
3

Return it from getSubAgents(ctx)

The runtime wraps each sub-agent as a tool named <name> and exposes it to the main agent.
See getSubAgents in weather.plugin.ts.
4

Set forwardTools to surface inner work

By default, the main agent only sees the sub-agent’s final string. Forward inner tool calls into the main chat so the UI renders them.
Runtime-injected passthrough tools (e.g. memory CRUD) are never forwarded.
5

Use getRequestSubAgents when live state matters

Use the request-time variant when whether or how to build the sub-agent depends on the user, session, or state.
state.agActions (live AG-UI actions) is the canonical request-time field — reading it through rtCtx.history.state returns unknown, so validate with Array.isArray(...) before use. Boot-time getSubAgents and request-time getRequestSubAgents outputs merge.

What to know before shipping

  • The sub-agent’s name must be unique across all loaded plugins — same namespace as regular tools. Prefix with the agent role (e.g. weather_planner_agent).
  • model defaults to 'subagent'. Use 'main' for a top-tier model, 'utility' for a cheaper one.
  • Sub-agent middleware (the middlewares field) only runs inside the sub-agent’s loop, not the main agent’s chain.
  • onComplete(result, ctx) fires after the last turn — use it for follow-up events.
  • If sub-agent init throws, the runtime logs and skips that contribution for that request; the rest of the agent build continues.

Add a tool

For single-call work that doesn’t need a sub-agent.

Add a middleware

Main-agent vs sub-agent middleware.