Subagents
Subagents let a parent agent delegate focused tasks to independent child agents. Each subagent runs in isolation with its own context, tools, and budget — then returns a result to the parent.Why Subagents
A single agent working on a complex task accumulates context: tool outputs, intermediate reasoning, file contents. This leads to problems:- Context pollution — irrelevant details from one subtask confuse the LLM during another
- Tool overload — the LLM sees tools it doesn’t need, increasing error rates
- Budget waste — all tokens go against one shared limit with no per-task control
- Context isolation — each subagent starts with a clean conversation
- Specialized tools — each subagent sees only the tools it needs
- Budget propagation — parent budget flows down, preventing runaway children
- Result aggregation — the parent synthesizes subagent outputs into a final answer
Quick Start
spawn_subagent tool with the available subagent names and descriptions. It decides which subagent to call and formulates the prompt for each.
How It Works
The spawn_subagent Tool
UseSubagents installs a SpawnSubagentTool that the LLM calls with two arguments:
subagent— name of the subagent to spawn (from the registry)prompt— the task description for the subagent
Execution Flow
What the Parent Receives
TheSpawnSubagentTool returns the child’s complete AgentState. The ToolExecutionFormatter extracts the final response text and feeds it back to the parent LLM as the tool result.
Defining Subagents
Each subagent is described by anAgentDefinition. You can register them programmatically or load from files.
Programmatic Registration
File-Based Definitions
Load from markdown, YAML, or JSON files:Tool Filtering
Subagents don’t automatically get all parent tools. You control access via allow-lists and deny-lists on the definition.Default: Inherit All Tools
Whentools is omitted, the subagent inherits all parent tools:
Allow-List
Specify exactly which tools the subagent can use:Deny-List
Block specific tools while inheriting the rest:Combined Filtering
When both are specified, the allow-list is applied first, then the deny-list filters out any remaining denied tools:Budget Propagation
Budgets flow from parent to child, preventing subagents from consuming more resources than the parent has remaining.How It Works
- Parent has a budget (e.g., 100 steps, 10000 tokens)
- Parent uses some resources before spawning a subagent
- Child’s effective budget = min(definition budget, parent’s remaining budget)
- Child runs within its effective budget
Multi-Level Propagation
Budget constraints cascade through nesting levels:Setting Budgets
On the definition:Depth Control
Subagents can themselves spawn subagents (recursive delegation).SubagentPolicy controls the maximum nesting depth:
SpawnSubagentTool throws SubagentDepthExceededException, which the parent sees as a tool error.
LLM Configuration
Each subagent can use a different LLM. Specify viallmConfig on the definition:
llmConfig is omitted, the subagent inherits the parent’s LLM provider.
Events
Subagent lifecycle emits events for monitoring and debugging:| Event | When |
|---|---|
SubagentSpawning | Before subagent execution starts |
SubagentCompleted | After subagent execution finishes |
parentAgentId, parentExecutionId, parentStepNumber, toolCallId, depth, and subagentName. This enables tracing the full delegation chain.
AgentConsoleLogger to see parent/child agent IDs in console output:
Error Handling
| Exception | When |
|---|---|
SubagentNotFoundException | Subagent name not found in registry |
SubagentDepthExceededException | Nesting depth limit reached |
SubagentExecutionException | Subagent execution failed (wraps child errors) |
AgentException. When a subagent fails, the parent sees a tool error with the exception details and can decide how to proceed.
ResearchSubagentTool
For simpler use cases,ResearchSubagentTool provides a pre-built subagent that reads files and returns a summary — without requiring a registry or definitions:
task and optional files list. The tool spawns a subagent with read-only file access, runs the research, and returns the findings.
Testing Subagents
UseFakeAgentDriver with withChildSteps() to script subagent behavior:
Typical Patterns
Fan-Out Review
One parent spawns multiple subagents for independent subtasks, then synthesizes:Specialized Roles
Different subagents handle different aspects of a task:Hierarchical Delegation
Subagents spawn their own subagents for further decomposition:SubagentPolicy to prevent unbounded recursion.