Overview
Agents can spawn other agents to delegate specialized tasks. This enables:- Modular design: Each agent focuses on one thing
- Reusable components: Share agents across projects
- Parallel execution: Spawn multiple agents simultaneously
- Complex workflows: Chain agents together
Enabling Subagent Spawning
1. Add spawn_agents Tool
The parent agent needs thespawn_agents tool:
2. Define Spawnable Agents
List which agents can be spawned:Spawning Methods
Method 1: AI-Controlled Spawning
Let the AI decide when to spawn agents:Method 2: Programmatic Spawning
UsehandleSteps to spawn agents from code:
Real Examples from Codebuff
Example 1: Base Orchestrator Agent
Frombase2.ts - Orchestrates file finding, research, editing, and review:
Example 2: General Agent with File Reading
Fromgeneral-agent.ts - Pre-reads files, then spawns helpers:
Example 3: File Picker with File Lister
Fromfile-picker.ts - Spawns file-lister, processes results, reads files:
Agent Communication Patterns
Pattern 1: Sequential Pipeline
Spawn agents one after another:Pattern 2: Parallel Execution
Spawn multiple agents at once:Pattern 3: Conditional Spawning
Spawn agents based on conditions:Pattern 4: Iterative Refinement
Spawn agents in a loop:Passing Data Between Agents
Via Prompt
Pass simple data in the prompt:Via Params
Pass structured data in params:Via Message History
Agents withincludeMessageHistory: true see the full conversation:
Via Shared Context
Read files in parent, child can access them:Processing Subagent Results
Extract Last Message
Extract Structured Output
Handle Errors
Best Practices
1. Design Focused Agents
Each agent should have one clear purpose:- file-picker: Find files
- code-searcher: Search code
- editor: Edit files
- code-reviewer: Review changes
- researcher-web: Search web
2. Use spawnerPrompt
Help parent agents understand when to spawn your agent:3. Design Clear Input Schemas
Make it easy to spawn your agent:4. Spawn Agents in Parallel
Maximize throughput:5. Handle Agent Failures
Always check for errors:6. Use Context Wisely
- Set
includeMessageHistory: trueonly when needed - Pre-read files in parent if multiple children need them
- Use
inheritParentSystemPromptfor prompt caching
Agent Composition Examples
Research → Implement → Review
Multi-Agent Exploration
Next Steps
Generators
Master handleSteps for complex workflows
Publishing
Share your composed agents

