Overview
Subagents are specialized AI agents that can be delegated specific tasks. They operate independently from the main agent, allowing for:- Parallel execution of multiple tasks
- Domain specialization with focused instructions
- Isolated reasoning for complex problems
- Reduced context pollution in the main conversation
How Subagents Work
Task Delegation
The main agent identifies a subtask suitable for delegation and invokes the
task tool.Subagent Initialization
A specialized agent is spawned with:
- The specific task description
- Relevant context from the main conversation
- Access to the same tools as the main agent
- Independent conversation history
Independent Execution
The subagent:
- Reasons about the task independently
- Calls tools as needed
- Builds its own conversation context
- Requests approvals separately (in default mode)
Result Return
When complete, the subagent returns:
- Task results
- Any artifacts created
- Summary of actions taken
- Token usage statistics
Using Subagents
Natural Language
The AI automatically delegates when appropriate:Explicit Delegation
You can also use the/agents command:
- Name: Identifier for this subagent type
- Instructions: Specialized system prompt
- Tools: Which tools this agent can use
- Model: Specific model for this agent (optional)
Subagent Types
Built-in Subagent Patterns
- Code Reviewer
- Test Writer
- Documentation Writer
- Refactoring Specialist
Purpose: Analyze code for issuesInstructions:Tools:
read, grep, glob, web_fetch (read-only)Usage:Managing Subagents
Creating Custom Subagents
Managing Existing Subagents
- View: See subagent configuration
- Edit: Modify instructions or settings
- Delete: Remove a subagent
- Duplicate: Copy and customize an existing subagent
- Export: Save subagent to a file
- Import: Load subagent from a file
Sharing Subagents
Subagent configurations are stored in:Subagent Behavior
Independent Context
Each subagent has its own conversation history:Tool Access
Subagents can use the same tools as the main agent:- File Operations
- Code Analysis
- Execution
- External
read- Read fileswrite_file- Create filesedit- Modify filesglob- Find filesls- List directories
Approval Modes
Subagents respect the global approval mode but can have overrides:Nested Subagents
Subagents can delegate to other subagents:Monitoring Subagents
In Interactive Mode
Subagent activity is displayed with indentation:Stats and Metrics
- Number of subagent invocations
- Average duration per subagent
- Token usage per subagent type
- Success/failure rates
Use Cases
Parallel Feature Development
Parallel Feature Development
Scenario: Building multiple independent features
Multi-Step Refactoring
Multi-Step Refactoring
Scenario: Complex refactoring with multiple phases
Code Review Pipeline
Code Review Pipeline
Scenario: Comprehensive code review
Documentation Generation
Documentation Generation
Scenario: Creating comprehensive docs
Configuration
Project-Level Subagents
Global Settings
Advanced Features
Subagent Communication
Subagents can pass data between each other:Custom Models per Subagent
Resource Limits
Troubleshooting
Subagent not being used
Subagent not being used
Problem: Main agent isn’t delegating to subagents.Solutions:
- Make the task more complex (simple tasks don’t need delegation)
- Explicitly mention using specialized agents
- Check that subagents are enabled in settings
- Ensure the task matches subagent capabilities
Too much delegation
Too much delegation
Problem: Everything is delegated, making things slower.Solutions:
- Adjust delegation threshold in settings
- Be more specific in prompts
- Disable some subagents temporarily
- Use simpler prompts for simple tasks
Subagent conflicts
Subagent conflicts
Problem: Multiple subagents editing the same files.Solutions:
- More specific task delegation
- Use sequential rather than parallel delegation
- Define clear boundaries in subagent instructions
- Let main agent coordinate conflicts
High token usage
High token usage
Problem: Subagents consuming too many tokens.Solutions:
- Set
maxTokensper subagent - Use faster models for simple subagents
- Limit nesting depth
- Reduce
maxConcurrentsubagents
Best Practices
- Clear boundaries: Define what each subagent should (and shouldn’t) do
- Specialized instructions: Make subagents experts in narrow domains
- Limit tools: Only give subagents the tools they need
- Set resource limits: Prevent runaway subagents with timeouts and token limits
- Test subagents: Try them with specific prompts before relying on them
- Monitor usage: Check stats to optimize subagent configuration
- Version control: Keep subagent configs in your repo for team sharing
- Start simple: Begin with basic subagents, add complexity as needed
Next Steps
Skills System
Combine subagents with skills for powerful workflows
Approval Modes
Configure approval for subagent actions
Session Commands
Use /agents commands to manage subagents
Configuration
Advanced subagent configuration options
