What are Sub-Agents?
Sub-agents allow you to create modular, reusable agents that can be composed together. A parent agent can delegate specific tasks to specialized sub-agents through tool calls.Sub-agents are automatically converted to tools that parent agents can call. Each sub-agent uses the parent entry’s
maxSteps, then the leaf agent’s own maxSteps, then the 100-step default.Basic Usage
Define a Sub-Agent
Createresearcher.agentuse:
The agent name is automatically derived from the filename (e.g.,
researcher.agentuse becomes researcher).Use the Sub-Agent
Createmain.agentuse:
Multiple Sub-Agents
Sub-Agent Configuration
You can customize sub-agent behavior:Path Resolution
All sub-agent paths are resolved relative to the parent agent file’s directory, ensuring portability across different execution contexts.See Agent Syntax - Subagents for complete path resolution examples and directory structure recommendations.
Remote Sub-Agents
Passing Context
Sub-agents accept optional parameters when called as tools:Sub-Agent Tool Schema
Each sub-agent tool accepts:task(optional string): Additional instructions for the sub-agentcontext(optional object): Structured data to pass to the sub-agent
Advanced Patterns
Conditional Delegation
Limitations: No Nested Sub-Agents
This limitation is intentional to:- Prevent infinite recursion (Agent A → Agent B → Agent A)
- Avoid deep nesting complexity that’s hard to debug
- Reduce resource consumption and token usage
- Keep execution traces manageable
Sub-agents only have access to their configured MCP tools, not other sub-agents. Each sub-agent creates its own isolated MCP connections.
Parallel Processing
Sub-Agent Communication
Tool and Resource Inheritance
Sub-agents create their own isolated tool environments:MCP server commands with relative paths are resolved from the agent file’s directory, ensuring consistent behavior across different execution contexts.
Each sub-agent manages its own MCP connections and tools. They don’t automatically inherit parent tools.
System Context
All sub-agents receive consistent system context:- Date and time information
- Autonomous agent behavior prompts
- Model-specific system messages (e.g., “Claude Code” for Anthropic models)
Best Practices
Single Responsibility
Single Responsibility
Each sub-agent should have one clear purpose. This makes them reusable and easier to maintain.
Clear Interfaces
Clear Interfaces
Define clear expectations for what each sub-agent receives and returns.
Error Handling
Error Handling
Parent agents should handle sub-agent failures gracefully.
Performance
Performance
Use lighter models (like Haiku) for simple sub-agents to reduce costs and latency.
Testing
Testing
Test sub-agents independently before composing them.
Example: Customer Support System
Performance Optimization
Model Selection
Model Override Inheritance
When you use--model, it overrides the model for both the parent and ALL sub-agents:
See CLI Commands - Model Override for complete details on model override behavior and sub-agent inheritance.
Step Limits
Control sub-agent execution limits:Default step limit for sub-agents is 50 (compared to 1000 for main agents).
Debugging Sub-Agents
Enable Verbose Logging
Trace Execution
Test Sub-Agents Individually
Security Considerations
Best Practices for Security
- Audit sub-agent configurations carefully
- Use minimal necessary permissions in MCP server configs
- Avoid loading sub-agents from untrusted sources
- Review sub-agent instructions for potentially harmful commands
- Use
disallowedToolsin MCP configs to restrict dangerous tools
Next Steps
Remote Agents
Learn about remote agent execution
Examples
See sub-agent examples