Skip to content

Sub-agents ​

A sub-agent is a second Agent created inside a tool call: its own conversation, its own iteration budget, and — by default — the parent's tools, workspace and display. It keeps large intermediate results out of the parent's context.

The four tools ​

ToolBox().with_subagent_provider() registers:

ToolBehaviour
agent_run(task, name=None)Run a child to completion and return its final message. Blocks the parent.
agent_spawn(task, name=None)Start a child in a background thread, return {"id": …} immediately.
agent_wait(identifier, timeout_seconds=300)Wait for one spawned child; returns its Result, or {"status": "timeout", "id", "notice"} if it is still running (the child keeps going — wait again with the same id).
agent_list(){"id", "status": running|finished} for every tracked child.

Children run in daemon threads and are tracked in an AgentPool stored in the parent's AgentState as a RuntimeEntry, so they are neither serialized nor shared with the child. On before_finalize the pool cancels everything still running — a parent that goes away takes its children with it.

Because the agent's api_call_semaphore is shared, parent and children together make at most DEFAULT_API_CALL_CONCURRENCY (8) concurrent model calls.

Spawn strategies ​

The default strategy is what setup_agent(default_tools=True) and the CLI entrypoints use:

python
Agent.inherit(parent).system(subagent_prompt)    # + honor param.name

Same tools, workspace and display; blank conversation; a system prompt that tells the worker to report its result in its final message.

Provide your own getter to change that — e.g. a worker with a narrow tool set:

python
import math
from xun import Agent, ToolBox, ToolCallContext as Context, AgentGetterParam

def sqrt(ctx: Context, a: float) -> float:
    """Square root of a number."""
    print(f"Called by {ctx.agent.name}")
    return math.sqrt(a)

def agent_getter(param: AgentGetterParam) -> Agent[Agent.T.Uninit]:
    agent = Agent(toolbox=ToolBox().register(sqrt))
    agent.name = param.name or "subagent"      # the getter must honor the requested name
    return agent                               # uninitialized: the caller finalizes it

agent = Agent(toolbox=ToolBox().with_subagent_provider(agent_getter)).initialize()
agent.instruct("Ask a sub-agent for the square root of 114514").execute()

AgentGetterParam carries tool_context (the parent's ToolCallContext, hence param.tool_context.agent) and the requested name. The getter returns an uninitialized agent; the runner wraps it in with, instructs it with task, executes with the parent's context value, and captures the result — errors included, since the worker body never raises.

To inherit from the parent, call Agent.inherit(param.tool_context.agent) — that is also what wires up cancellation. A getter that builds a detached agent (inherit_cancel_event=False) gets a child that survives the parent's cancel.

Nesting depth ​

with_subagent_provider(agent_getter=None, max_depth=2) counts nesting through a runtime state entry: a child's depth is its parent's depth + 1, and as soon as depth > max_depth the child is created with disable_subagent() applied, so it can work but cannot spawn further. The default of 2 therefore allows three generations of workers, the last one being a leaf.

Writing good sub-agent tasks ​

  • The child starts with a blank context: everything it needs must be in task.
  • Ask for the answer in the final message; use files for anything large.
  • One self-contained, multi-step objective per child — that is what makes the isolation worthwhile. Running several agent_spawns and then agent_waits is the parallel form.

Xun 1.4.2dev (unreleased; source 51d23f3, 2026-10-16) documentation, generated from the source tree.