Overview
Theagent tool enables hierarchical agent architectures by spawning subagents that work autonomously to complete tasks. Subagents have access to the same tool set as their parent and return their final response upon completion.
Tool Definition
The task description for the subagent to perform. Should be clear and specific enough for the subagent to work independently.
Return Value
Returns a string containing the subagent’s final assistant response after completing the task.Behavior
Subagent Lifecycle
- Parent agent calls the
agenttool with a task description - System spawns a new agent instance with the same tool access
- Subagent works autonomously using its tools to complete the task
- Subagent returns its final response
- Parent agent receives the result as the tool’s return value
Context Requirements
The tool requires aspawn function in the tool context. If ctx.spawn is not available, the tool throws an error:
Tool Access
Subagents inherit the same tool permissions and capabilities as their parent, enabling them to:- Execute shell commands via
bash - Read files via
read - Apply patches via
patch - Spawn their own subagents (nested hierarchy)
Usage Examples
Delegating Research Tasks
Complex Multi-Step Operations
Parallel Work Distribution
Nested Subagent Hierarchies
Implementation Details
The tool is defined inpackages/ai/tools/agent.ts and relies on the orchestrator’s spawn mechanism.
Schema Definition
Execute Function
- Validates that
ctx.spawnis available - Calls
ctx.spawn(task)to create and run the subagent - Waits for the subagent to complete
- Returns the subagent’s final response as both context and result
Architecture Considerations
When to Use Subagents
Good use cases:- Complex tasks that can be isolated
- Tasks requiring multiple tool invocations
- Parallel work distribution
- Specialized analysis or research
- Simple single-tool operations
- Tasks requiring shared state with parent
- Operations where coordination overhead exceeds task complexity
Graph Structure
Subagent events are integrated into the conversation graph as described indocs/subagents.md. The graph maintains parent-child relationships through:
- Chunk nodes for subagent boundaries
- Block nodes for tool calls and responses
- Message nodes for conversation history
Error Handling
If a subagent encounters an error:- The error is included in the subagent’s final response
- Parent agent receives the error context and can handle appropriately
- The conversation graph captures the full error trace
Related Tools
- bash - Execute shell commands (available to subagents)
- read - Read files (available to subagents)
- patch - Apply patches (available to subagents)
Related Documentation
- Multi-Agent Guide - Orchestrator and subagent coordination
- Tool Context Types - ToolContext interface and spawn function
