Sub-agents
Sub-agents are specialized Claude instances that work on specific tasks in parallel with your main session.
What Are Sub-agents?
Think of sub-agents as team members you can delegate to:
- Main agent (you): Architects and coordinates
- Sub-agents: Handle specific tasks
Spawning Sub-agents
Use the Task tool to spawn sub-agents:
> Use a sub-agent to write tests for the auth module
> while I continue working on the API
Claude will:
- Spawn a specialized agent for testing
- Let you continue other work
- Report back when tests are complete
Sub-agent Types
Code Review Agent
> Spawn a sub-agent to review the PR in #123
> Focus on security issues and performance
Testing Agent
> Use a sub-agent to generate comprehensive tests
> for all functions in src/utils/
Documentation Agent
> Have a sub-agent update the README and API docs
> based on recent changes
Research Agent
> Spawn a sub-agent to research best practices
> for implementing rate limiting
Parallel Execution
The real power is parallelization:
> In parallel:
> 1. Sub-agent A: Write unit tests for user service
> 2. Sub-agent B: Write integration tests for API
> 3. Main: I'll work on the new feature
Best Practices
Be Specific
# Less effective
> Have a sub-agent help with tests
# More effective
> Spawn a sub-agent to write Jest unit tests
> for src/services/user.ts covering:
> - createUser with valid/invalid input
> - updateUser authorization checks
> - deleteUser cascade behavior
Set Boundaries
> The sub-agent should only modify files in
> src/__tests__/ and not touch production code
Review Results
Always review sub-agent output:
> Show me what the testing sub-agent produced
When to Use Sub-agents
| Task | Main Agent | Sub-agent |
|---|---|---|
| Core feature | ||
| Tests for feature | ||
| Documentation | ||
| Code review |
Next Steps
In the next lesson, you'll learn about MCPs for integrating external tools.