Subagents
Subagents are a powerful feature that lets Claude delegate tasks to separate instances. This preserves your main context while getting work done.
What Are Subagents?
Section titled “What Are Subagents?”When Claude encounters a task that would:
- Fill up context with irrelevant details
- Require isolated execution
- Benefit from parallel processing
It can spawn a subagent - a fresh Claude instance with focused context.
Main Agent (your conversation)├── Subagent 1: Research task├── Subagent 2: Code generation└── Subagent 3: Test writingWhy Use Subagents?
Section titled “Why Use Subagents?”1. Context Preservation
Section titled “1. Context Preservation”Your main conversation stays clean:
# Without subagents> research all the authentication libraries and their tradeoffs[10,000 tokens of research flood your context]> now implement login[Claude has less room for implementation]
# With subagents> research auth libraries using a subagent[Main agent receives summary only]> now implement login[Full context available for implementation]2. Task Isolation
Section titled “2. Task Isolation”Subagents have their own context, so failures don’t pollute your conversation:
> use a subagent to experiment with three different implementations> report back which one is cleanest3. Parallel Work
Section titled “3. Parallel Work”Multiple subagents can work simultaneously:
> spawn subagents to:> - write unit tests for auth module> - write integration tests for API> - update documentationUsing Subagents
Section titled “Using Subagents”Automatic Delegation
Section titled “Automatic Delegation”Claude automatically uses subagents for certain tasks:
> research the best practices for rate limiting in PythonClaude may spawn a subagent to do the research and return a summary.
Explicit Request
Section titled “Explicit Request”Ask Claude to use a subagent:
> use a subagent to refactor the database module> keep my context clean - just give me the summary when doneTask Agent Pattern
Section titled “Task Agent Pattern”For complex tasks:
> create a task agent to:> 1. Read all files in the auth/ directory> 2. Identify security issues> 3. Propose fixes> 4. Report findings to meBest Practices
Section titled “Best Practices”1. Define Clear Tasks
Section titled “1. Define Clear Tasks”# Good: Clear scope> use a subagent to write pytest tests for app/services/user.py> test all public methods> include edge cases
# Bad: Vague scope> use a subagent to help with testing2. Request Summaries
Section titled “2. Request Summaries”> spawn a subagent to analyze the codebase architecture> return a one-paragraph summary, not the full analysis3. Use for Research
Section titled “3. Use for Research”> use a subagent to research:> - What's the current best practice for Python dependency injection?> - Compare at least 3 approaches> - Recommend one for our project
# Main agent gets recommendation without research polluting contextWhen to Use Subagents
Section titled “When to Use Subagents”✅ Good Use Cases
Section titled “✅ Good Use Cases”| Task | Why Subagent |
|---|---|
| Research | Keeps findings out of main context |
| Test writing | Isolated, can be parallelized |
| Refactoring | Complex diffs don’t clutter context |
| Documentation | Generate docs without filling context |
| Code review | Analysis stays separate |
❌ Poor Use Cases
Section titled “❌ Poor Use Cases”| Task | Why Not |
|---|---|
| Quick questions | Overhead not worth it |
| Single-file edits | Main context handles fine |
| Interactive work | Need back-and-forth |
| Debugging | Need full context visibility |
Built-in Subagent Types
Section titled “Built-in Subagent Types”Claude Code includes specialized subagents optimized for different tasks:
| Type | Tools | Best For |
|---|---|---|
| Explore | Read, Grep, Glob | Fast codebase searches, understanding structure |
| Plan | Read, Grep, Glob, WebSearch | Research and implementation planning |
| General | All tools | Complex tasks requiring modification |
# ExamplesUse Explore to find all authentication-related filesUse Plan to design the database migration strategyImplement the user authentication feature end-to-endCustom Subagents
Section titled “Custom Subagents”Create specialized agents in .claude/agents/ using Markdown with YAML frontmatter:
---name: security-reviewerdescription: Reviews code for security vulnerabilitiestools: [Read, Grep, Glob]context: fork # Optional: isolated context---
You are a security-focused code reviewer.
Check for: SQL injection, XSS, auth issues, data exposure.
Report: File:line, Severity, Issue, Fix. Never modify files.Frontmatter options:
name(required): Unique identifierdescription(required): Agent purposemodel(optional): Specific Claude modeltools(optional): Allowed tools (defaults to all)context: fork(optional): Isolated context for research tasks