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 runs with a default limit of 50 steps.
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
:
Sub-agents are called as tools using their filename (without
.agentuse
). The researcher.agentuse
becomes available as the researcher
tool.Multiple Sub-Agents
Sub-Agent Configuration
You can customize sub-agent behavior:Path Resolution
Important: All sub-agent paths are resolved relative to the parent agent file’s directory, not the current working directory. This ensures your agent compositions work consistently regardless of where you run them.
How Path Resolution Works
When you define a sub-agent path, it’s always relative to the location of the parent agent file:Directory Structure Example
Remote Sub-Agents
Remote sub-agents are not currently supported. Sub-agents must be local files accessible via the filesystem.
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
Important: Sub-agents cannot have their own sub-agents. Any
subagents
configuration in a sub-agent file will be ignored.- 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-agents run sequentially, not in true parallel. The parent agent calls each sub-agent tool one at a time.
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 the
--model
CLI flag, it overrides the model for both the parent agent
and ALL sub-agents. This allows you to test entire agent hierarchies with different
models without editing any files.- Cost optimization: Use cheaper models for development/testing
- A/B testing: Compare entire workflows with different models
- Environment-specific deployments: Different models for dev/staging/prod
- Quick experiments: Try new models without modifying agent files
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
Sub-agents run with the same system permissions as the parent process. Each sub-agent:
- Creates its own MCP connections with full permissions
- Can access any tools defined in its configuration
- Runs with the same file system and network access
- Has access to environment variables
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
disallowedTools
in MCP configs to restrict dangerous tools