Comprehensive guide to sub-agents in Claude Code: built-in agents (Explore, Plan, general-purpose), custom agent creation, configuration, and delegation patterns. Use when: creating custom sub-agents, delegating bulk operations, parallel research, understanding built-in agents, or configuring agent tools/models.
/plugin marketplace add jezweb/claude-skills/plugin install jezweb-tooling-skills@jezweb/claude-skillsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
README.mdStatus: Production Ready ✅ Last Updated: 2026-01-10 Source: https://code.claude.com/docs/en/sub-agents
Sub-agents are specialized AI assistants that Claude Code can delegate tasks to. Each sub-agent has its own context window, configurable tools, and custom system prompt.
The primary value of sub-agents isn't specialization—it's keeping your main context clean.
Without agent (context bloat):
Main context accumulates:
├─ git status output (50 lines)
├─ npm run build output (200 lines)
├─ tsc --noEmit output (100 lines)
├─ wrangler deploy output (100 lines)
├─ curl health check responses
├─ All reasoning about what to do next
└─ Context: 📈 500+ lines consumed
With agent (context hygiene):
Main context:
├─ "Deploy to cloudflare"
├─ [agent summary - 30 lines]
└─ Context: 📊 ~50 lines consumed
Agent context (isolated):
├─ All verbose tool outputs
├─ All intermediate reasoning
└─ Discarded after returning summary
The math: A deploy workflow runs ~10 tool calls. That's 500+ lines in main context vs 30-line summary with an agent. Over a session, this compounds dramatically.
When this matters most:
Key insight: Use agents for workflows you repeat, not just for specialization. The context savings compound over time.
Claude Code includes three built-in sub-agents available out of the box:
Fast, lightweight agent optimized for read-only codebase exploration.
| Property | Value |
|---|---|
| Model | Haiku (fast, low-latency) |
| Mode | Strictly read-only |
| Tools | Glob, Grep, Read, Bash (read-only: ls, git status, git log, git diff, find, cat, head, tail) |
Thoroughness levels (specify when invoking):
quick - Fast searches, targeted lookupsmedium - Balanced speed and thoroughnessvery thorough - Comprehensive analysis across multiple locationsWhen Claude uses it: Searching/understanding codebase without making changes. Findings don't bloat the main conversation.
User: Where are errors from the client handled?
Claude: [Invokes Explore with "medium" thoroughness]
→ Returns: src/services/process.ts:712
Specialized for plan mode research and information gathering.
| Property | Value |
|---|---|
| Model | Sonnet |
| Mode | Read-only research |
| Tools | Read, Glob, Grep, Bash |
| Invocation | Automatic in plan mode |
When Claude uses it: In plan mode when researching codebase to create a plan. Prevents infinite nesting (sub-agents cannot spawn sub-agents).
Capable agent for complex, multi-step tasks requiring both exploration AND action.
| Property | Value |
|---|---|
| Model | Sonnet |
| Mode | Read AND write |
| Tools | All tools |
| Purpose | Complex research, multi-step operations, code modifications |
When Claude uses it:
| Type | Location | Scope | Priority |
|---|---|---|---|
| Project | .claude/agents/ | Current project only | Highest |
| User | ~/.claude/agents/ | All projects | Lower |
| CLI | --agents '{...}' | Current session | Middle |
When names conflict, project-level takes precedence.
⚠️ CRITICAL: Session Restart Required
Agents are loaded at session startup only. If you create new agent files during a session:
/agentsThis is the most common reason custom agents "don't work" - they were created after the session started.
Markdown files with YAML frontmatter:
---
name: code-reviewer
description: Expert code reviewer. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: inherit
permissionMode: default
skills: project-workflow
hooks:
PostToolUse:
- matcher: "Edit|Write"
hooks:
- type: command
command: "./scripts/run-linter.sh"
---
Your sub-agent's system prompt goes here.
Include specific instructions, best practices, and constraints.
| Field | Required | Description |
|---|---|---|
name | Yes | Unique identifier (lowercase, hyphens) |
description | Yes | When Claude should use this agent |
tools | No | Comma-separated list. Omit = inherit all tools |
model | No | sonnet, opus, haiku, or inherit. Default: sonnet |
permissionMode | No | default, acceptEdits, dontAsk, bypassPermissions, plan, ignore |
skills | No | Comma-separated skills to auto-load (sub-agents don't inherit parent skills) |
hooks | No | PreToolUse, PostToolUse, Stop event handlers |
Complete list of tools that can be assigned to sub-agents:
| Tool | Purpose | Type |
|---|---|---|
| Read | Read files (text, images, PDFs, notebooks) | Read-only |
| Write | Create or overwrite files | Write |
| Edit | Exact string replacements in files | Write |
| MultiEdit | Batch edits to single file | Write |
| Glob | File pattern matching (**/*.ts) | Read-only |
| Grep | Content search with regex (ripgrep) | Read-only |
| LS | List directory contents | Read-only |
| Bash | Execute shell commands | Execute |
| BashOutput | Get output from background shells | Execute |
| KillShell | Terminate background shell | Execute |
| Task | Spawn sub-agents | Orchestration |
| WebFetch | Fetch and analyze web content | Web |
| WebSearch | Search the web | Web |
| TodoWrite | Create/manage task lists | Organization |
| TodoRead | Read current task list | Organization |
| NotebookRead | Read Jupyter notebooks | Notebook |
| NotebookEdit | Edit Jupyter notebook cells | Notebook |
| AskUserQuestion | Interactive user questions | UI |
| EnterPlanMode | Enter planning mode | Planning |
| ExitPlanMode | Exit planning mode with plan | Planning |
| Skill | Execute skills in conversation | Skills |
| LSP | Language Server Protocol integration | Advanced |
| MCPSearch | MCP tool discovery | Advanced |
Tool Access Patterns by Agent Type:
| Agent Type | Recommended Tools |
|---|---|
| Read-only reviewers | Read, Grep, Glob, LS |
| Code writers | Read, Write, Edit, Bash, Glob, Grep |
| Research agents | Read, Grep, Glob, WebFetch, WebSearch |
| Documentation | Read, Write, Edit, Glob, Grep, WebFetch |
| Orchestrators | Read, Grep, Glob, Task |
| Full access | Omit tools field (inherits all) |
/agents
Interactive menu to:
claude --agents '{
"code-reviewer": {
"description": "Expert code reviewer. Use proactively after code changes.",
"prompt": "You are a senior code reviewer. Focus on code quality, security, and best practices.",
"tools": ["Read", "Grep", "Glob", "Bash"],
"model": "sonnet"
}
}'
Claude proactively delegates based on:
description field in sub-agent configTip: Include "use PROACTIVELY" or "MUST BE USED" in description for more automatic invocation.
> Use the test-runner subagent to fix failing tests
> Have the code-reviewer subagent look at my recent changes
> Ask the debugger subagent to investigate this error
Sub-agents can be resumed to continue previous conversations:
# Initial invocation
> Use the code-analyzer agent to review the auth module
[Agent completes, returns agentId: "abc123"]
# Resume with full context
> Resume agent abc123 and now analyze the authorization logic
Use cases:
Add to settings.json permissions:
{
"permissions": {
"deny": ["Task(Explore)", "Task(Plan)"]
}
}
Or via CLI:
claude --disallowedTools "Task(Explore)"
Sub-agents can invoke other sub-agents, enabling sophisticated orchestration patterns. This is accomplished by including the Task tool in an agent's tool list.
When a sub-agent has access to the Task tool, it can:
---
name: orchestrator
description: Orchestrator agent that coordinates other specialized agents. Use for complex multi-phase tasks.
tools: Read, Grep, Glob, Task # ← Task tool enables agent spawning
model: sonnet
---
---
name: release-orchestrator
description: Coordinates release preparation by delegating to specialized agents. Use before releases.
tools: Read, Grep, Glob, Bash, Task
---
You are a release orchestrator. When invoked:
1. Use Task tool to spawn code-reviewer agent
→ Review all uncommitted changes
2. Use Task tool to spawn test-runner agent
→ Run full test suite
3. Use Task tool to spawn doc-validator agent
→ Check documentation is current
4. Collect all reports and synthesize:
- Blockers (must fix before release)
- Warnings (should address)
- Ready to release: YES/NO
Spawn agents in parallel when tasks are independent.
Wait for all agents before synthesizing final report.
Multi-Specialist Workflow:
User: "Prepare this codebase for production"
orchestrator agent:
├─ Task(code-reviewer) → Reviews code quality
├─ Task(security-auditor) → Checks for vulnerabilities
├─ Task(performance-analyzer) → Identifies bottlenecks
└─ Synthesizes findings into actionable report
Parallel Research:
User: "Compare these 5 frameworks for our use case"
research-orchestrator:
├─ Task(general-purpose) → Research framework A
├─ Task(general-purpose) → Research framework B
├─ Task(general-purpose) → Research framework C
├─ Task(general-purpose) → Research framework D
├─ Task(general-purpose) → Research framework E
└─ Synthesizes comparison matrix with recommendation
| Level | Example | Status |
|---|---|---|
| 1 | Claude → orchestrator | ✅ Works |
| 2 | orchestrator → code-reviewer | ✅ Works |
| 3 | code-reviewer → sub-task | ⚠️ Works but context gets thin |
| 4+ | Deeper nesting | ❌ Not recommended |
Best practice: Keep orchestration to 2 levels deep. Beyond that, context windows shrink and coordination becomes fragile.
| Use Orchestration | Use Direct Delegation |
|---|---|
| Complex multi-phase workflows | Single specialist task |
| Need to synthesize from multiple sources | Simple audit or review |
| Parallel execution important | Sequential is fine |
| Different specialists required | Same agent type |
Direct (simpler, often sufficient):
User: "Review my code changes"
Claude: [Invokes code-reviewer agent directly]
Orchestrated (when coordination needed):
User: "Prepare release"
Claude: [Invokes release-orchestrator]
orchestrator: [Spawns code-reviewer, test-runner, doc-validator]
orchestrator: [Synthesizes all reports]
[Returns comprehensive release readiness report]
Task can spawn any agent the session has access toinherit)Send agents to the background while continuing work in your main session:
Ctrl+B during agent execution moves it to background.
> Use the research-agent to analyze these 10 frameworks
[Agent starts working...]
[Press Ctrl+B]
→ Agent continues in background
→ Main session free for other work
→ Check results later with: "What did the research agent find?"
Use cases:
Match model to task requirements for cost and speed optimization:
| Model | Best For | Speed | Cost |
|---|---|---|---|
haiku | Simple lookups, format checks, fast reads | 2x faster | 3x cheaper |
sonnet | Complex reasoning, code generation | Balanced | Standard |
opus | Deep analysis, architectural decisions | Slower | Premium |
inherit | Match main conversation | Varies | Varies |
Pattern: Use Haiku for high-volume simple tasks, Sonnet for judgment-heavy work:
---
name: format-checker
model: haiku # Fast, cheap - just checking patterns
tools: Read, Grep, Glob
---
---
name: architecture-reviewer
model: opus # Deep reasoning needed
tools: Read, Grep, Glob, Bash
---
Agent context usage depends heavily on the task:
| Scenario | Context | Tool Calls | Works? |
|---|---|---|---|
| Deep research agent | 130k | 90+ | ✅ Yes |
| Multi-file audit | 80k+ | 50+ | ✅ Yes |
| Simple format check | 3k | 5-10 | ✅ Yes |
| Chained orchestration | Varies | Varies | ✅ Depends on task |
Reality: Agents with 90+ tool calls and 130k context work fine when doing meaningful work. The limiting factor is task complexity, not arbitrary token limits.
What actually matters:
When context becomes a problem:
Prevent agents from drifting into adjacent domains with explicit constraints:
---
name: frontend-specialist
description: Frontend code expert. NEVER writes backend logic.
tools: Read, Write, Edit, Glob, Grep
---
You are a frontend specialist.
BOUNDARIES:
- NEVER write backend logic, API routes, or database queries
- ALWAYS use React patterns consistent with the codebase
- If task requires backend work, STOP and report "Requires backend specialist"
FOCUS:
- React components, hooks, state management
- CSS/Tailwind styling
- Client-side routing
- Browser APIs
This prevents hallucination when agents encounter unfamiliar domains.
Hooks enable automated validation and feedback:
Block-at-commit (enforce quality gates):
hooks:
PreToolUse:
- matcher: "Bash"
hooks:
- type: command
command: |
if [[ "$BASH_COMMAND" == *"git commit"* ]]; then
npm test || exit 1
fi
Hint hooks (non-blocking feedback):
hooks:
PostToolUse:
- matcher: "Edit|Write"
hooks:
- type: command
command: "./scripts/lint-check.sh"
# Exits 0 to continue, non-zero to warn
Best practice: Validate at commit stage, not mid-plan. Let agents work freely, catch issues before permanent changes.
Claude automatically loads CLAUDE.md files from subdirectories when accessing those paths:
project/
├── CLAUDE.md # Root context (always loaded)
├── src/
│ └── CLAUDE.md # Loaded when editing src/**
├── tests/
│ └── CLAUDE.md # Loaded when editing tests/**
└── docs/
└── CLAUDE.md # Loaded when editing docs/**
Use for: Directory-specific coding standards, local patterns, module documentation.
This is lazy-loaded context - sub-agents get relevant context without bloating main prompt.
Two philosophies for delegation:
Custom Subagents (explicit specialists):
Main Claude → task-runner agent → result
Master-Clone (dynamic delegation):
Main Claude → Task(general-purpose) → result
Recommendation: Use custom agents for well-defined, repeated tasks. Use Task(general-purpose) for ad-hoc delegation where main context matters.
Best use case: Tasks that are repetitive but require judgment.
✅ Good fit:
- Audit 70 skills (repetitive) checking versions against docs (judgment)
- Update 50 files (repetitive) deciding what needs changing (judgment)
- Research 10 frameworks (repetitive) evaluating trade-offs (judgment)
❌ Poor fit:
- Simple find-replace (no judgment needed, use sed/grep)
- Single complex task (not repetitive, do it yourself)
- Tasks with cross-item dependencies (agents work independently)
This 5-step structure works consistently:
For each [item]:
1. Read [source file/data]
2. Verify with [external check - npm view, API, docs]
3. Check [authoritative source]
4. Evaluate/score
5. FIX issues found ← Critical: gives agent authority to act
Key elements:
| Batch Size | Use When |
|---|---|
| 3-5 items | Complex tasks (deep research, multi-step fixes) |
| 5-8 items | Standard tasks (audits, updates, validations) |
| 8-12 items | Simple tasks (version checks, format fixes) |
Why not more?
Parallel agents: Launch 2-4 agents simultaneously, each with their own batch.
┌─────────────────────────────────────────────────────────────┐
│ 1. PLAN: Identify items, divide into batches │
│ └─ "58 skills ÷ 10 per batch = 6 agents" │
├─────────────────────────────────────────────────────────────┤
│ 2. LAUNCH: Parallel Task tool calls with identical prompts │
│ └─ Same template, different item lists │
├─────────────────────────────────────────────────────────────┤
│ 3. WAIT: Agents work in parallel │
│ └─ Read → Verify → Check → Edit → Report │
├─────────────────────────────────────────────────────────────┤
│ 4. REVIEW: Check agent reports and file changes │
│ └─ git status, spot-check diffs │
├─────────────────────────────────────────────────────────────┤
│ 5. COMMIT: Batch changes with meaningful changelog │
│ └─ One commit per tier/category, not per agent │
└─────────────────────────────────────────────────────────────┘
Deep audit these [N] [items]. For each:
1. Read the [source file] from [path]
2. Verify [versions/data] with [command or API]
3. Check official [docs/source] for accuracy
4. Score 1-10 and note any issues
5. FIX issues found directly in the file
Items to audit:
- [item-1]
- [item-2]
- [item-3]
For each item, create a summary with:
- Score and status (PASS/NEEDS_UPDATE)
- Issues found
- Fixes applied
- Files modified
Working directory: [absolute path]
Update these [N] [items] to [new standard/format]. For each:
1. Read the current file at [path pattern]
2. Identify what needs changing
3. Apply the update following this pattern:
[show example of correct format]
4. Verify the change is valid
5. Report what was changed
Items to update:
- [item-1]
- [item-2]
- [item-3]
Output format:
| Item | Status | Changes Made |
|------|--------|--------------|
Working directory: [absolute path]
Research these [N] [options/frameworks/tools]. For each:
1. Check official documentation at [URL pattern or search]
2. Find current version and recent changes
3. Identify key features relevant to [use case]
4. Note any gotchas, limitations, or known issues
5. Rate suitability for [specific need] (1-10)
Options to research:
- [option-1]
- [option-2]
- [option-3]
Output format:
## [Option Name]
- **Version**: X.Y.Z
- **Key Features**: ...
- **Limitations**: ...
- **Suitability Score**: X/10
- **Recommendation**: ...
---
name: code-reviewer
description: Expert code review specialist. Proactively reviews code for quality, security, and maintainability. Use immediately after writing or modifying code.
tools: Read, Grep, Glob, Bash
model: inherit
---
You are a senior code reviewer ensuring high standards of code quality and security.
When invoked:
1. Run git diff to see recent changes
2. Focus on modified files
3. Begin review immediately
Review checklist:
- Code is clear and readable
- Functions and variables are well-named
- No duplicated code
- Proper error handling
- No exposed secrets or API keys
- Input validation implemented
- Good test coverage
- Performance considerations addressed
Provide feedback organized by priority:
- Critical issues (must fix)
- Warnings (should fix)
- Suggestions (consider improving)
Include specific examples of how to fix issues.
---
name: debugger
description: Debugging specialist for errors, test failures, and unexpected behavior. Use proactively when encountering any issues.
tools: Read, Edit, Bash, Grep, Glob
---
You are an expert debugger specializing in root cause analysis.
When invoked:
1. Capture error message and stack trace
2. Identify reproduction steps
3. Isolate the failure location
4. Implement minimal fix
5. Verify solution works
Debugging process:
- Analyze error messages and logs
- Check recent code changes
- Form and test hypotheses
- Add strategic debug logging
- Inspect variable states
For each issue, provide:
- Root cause explanation
- Evidence supporting the diagnosis
- Specific code fix
- Testing approach
- Prevention recommendations
Focus on fixing the underlying issue, not the symptoms.
---
name: data-scientist
description: Data analysis expert for SQL queries, BigQuery operations, and data insights. Use proactively for data analysis tasks and queries.
tools: Bash, Read, Write
model: sonnet
---
You are a data scientist specializing in SQL and BigQuery analysis.
When invoked:
1. Understand the data analysis requirement
2. Write efficient SQL queries
3. Use BigQuery command line tools (bq) when appropriate
4. Analyze and summarize results
5. Present findings clearly
Key practices:
- Write optimized SQL queries with proper filters
- Use appropriate aggregations and joins
- Include comments explaining complex logic
- Format results for readability
- Provide data-driven recommendations
Always ensure queries are efficient and cost-effective.
Agents don't commit - they only edit files. This is by design:
| Agent Does | Human Does |
|---|---|
| Research & verify | Review changes |
| Edit files | Spot-check diffs |
| Score & report | git add/commit |
| Create summaries | Write changelog |
Why?
Commit pattern:
git add [files] && git commit -m "$(cat <<'EOF'
[type]([scope]): [summary]
[Batch 1 changes]
[Batch 2 changes]
[Batch 3 changes]
🤖 Generated with [Claude Code](https://claude.com/claude-code)
EOF
)"
git diff [file] to see what changedgit checkout -- [file] to revertRare (agents work on different items), but if it happens:
/agents to generate initial config, then customize.claude/agents/ into git for team sharing| Consideration | Impact |
|---|---|
| Context efficiency | Agents preserve main context, enabling longer sessions |
| Latency | Sub-agents start fresh, may add latency gathering context |
| Thoroughness | Explore agent's thoroughness levels trade speed for completeness |
Built-in agents:
Explore → Haiku, read-only, quick/medium/thorough
Plan → Sonnet, plan mode research
General → Sonnet, all tools, read/write
Custom agents:
Project → .claude/agents/*.md (highest priority)
User → ~/.claude/agents/*.md
CLI → --agents '{...}'
Config fields:
name, description (required)
tools, model, permissionMode, skills, hooks (optional)
Core tools:
Read-only: Read, Grep, Glob, LS, TodoRead
Write: Write, Edit, MultiEdit, NotebookEdit
Execute: Bash, BashOutput, KillShell
Web: WebFetch, WebSearch
Advanced: Task, Skill, LSP, MCPSearch
Delegation:
Batch size: 5-8 items per agent
Parallel: 2-4 agents simultaneously
Prompt: 5-step (read → verify → check → evaluate → FIX)
Orchestration:
Enable: Add "Task" to agent's tools list
Depth: Keep to 2 levels max
Use: Multi-phase workflows, parallel specialists
Advanced:
Background: Ctrl+B during agent execution
Models: haiku (fast), sonnet (balanced), opus (deep)
Context: 130k+ and 90+ tool calls work fine for real work
Hooks: PreToolUse, PostToolUse, Stop events
Resume agents:
> Resume agent [agentId] and continue...
Last Updated: 2026-01-10
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.