Claude Code Subagents - Specialized AI Assistants

Subagents are specialized AI assistants that Claude Code can delegate tasks to. Each subagent has a specific purpose, uses its own context window, and can be configured with specific tools.

What Are Subagents?

Subagents enable delegated task execution by:

  • Creating isolated AI assistants with separate context windows
  • Providing customized system prompts for specialized expertise
  • Enforcing tool access control to limit capabilities
  • Preventing context pollution from complex tasks
  • Enabling parallel execution of multiple tasks

Subagents Architecture Overview

Subagents Architecture

The main Claude Code agent delegates tasks to specialized subagents (Architect, Engineer, Reviewer, Tester) which return aggregated results for the completed project.

Key Benefits

BenefitDescription
Context preservationSeparate context prevents pollution
Specialized expertiseFine-tuned for specific domains
ReusabilityUse across different projects
Flexible permissionsDifferent tool access levels
ScalabilityMultiple agents work simultaneously

File Locations

PriorityTypeLocationScope
1CLI-defined--agents flagSession only
2Project.claude/agents/Current project
3User~/.claude/agents/All projects
4PluginPlugin agents/Via plugins

Configuration

File Format

---
name: your-sub-agent-name
description: When this subagent should be invoked
tools: tool1, tool2, tool3
model: sonnet
---

Your subagent's system prompt goes here.

Configuration Fields

FieldRequiredDescription
nameYesUnique identifier (lowercase, hyphens)
descriptionYesWhen to invoke this agent
toolsNoComma-separated tool list
disallowedToolsNoTools the agent must not use
modelNosonnet, opus, haiku, or inherit
permissionModeNoPermission mode for operations
maxTurnsNoMaximum agentic turns
skillsNoSkills to preload
mcpServersNoMCP servers to make available
memoryNoMemory scope (user, project, local)
backgroundNoRun as background task
effortNoReasoning effort level
isolationNoGit worktree isolation

Built-in Subagents

Claude Code comes with several built-in subagents:

AgentPurpose
ExploreCodebase exploration and understanding
PlanPlanning and architecture
Code ReviewerCode quality and security review
Test EngineerTest generation and execution
DocumentationDocumentation creation

Creating Custom Subagents

Code Reviewer Agent

---
name: code-reviewer
description: Expert code reviewer. Use proactively after code changes.
tools: Read, Grep, Glob
model: sonnet
---

You are a senior code reviewer with 15 years of experience.

## Focus Areas

1. **Security**
   - SQL injection
   - XSS vulnerabilities
   - Authentication issues
   - Data validation

2. **Quality**
   - Code complexity
   - Naming conventions
   - Error handling
   - Test coverage

3. **Performance**
   - Algorithm efficiency
   - Memory usage
   - Database queries

## Output Format

Provide structured feedback:
- Critical issues (must fix)
- Suggestions (should fix)
- Observations (nice to have)

Test Engineer Agent

---
name: test-engineer
description: Generate and run tests. Use when creating tests.
tools: Read, Write, Bash(npm test)
model: sonnet
---

You are a test engineer specializing in comprehensive test coverage.

## Test Types

1. **Unit Tests**
   - Test individual functions
   - Mock external dependencies
   - Cover edge cases

2. **Integration Tests**
   - Test component interactions
   - Use real dependencies where appropriate
   - Test API endpoints

3. **E2E Tests**
   - Test critical user flows
   - Use realistic test data
   - Verify complete workflows

## Process

1. Analyze code structure
2. Identify test cases
3. Generate tests
4. Run tests
5. Fix failures

Documentation Agent

---
name: doc-writer
description: Create comprehensive documentation. Use when documenting code.
tools: Read, Write
model: sonnet
---

You are a technical writer specializing in developer documentation.

## Documentation Types

1. **API Documentation**
   - Endpoints
   - Parameters
   - Responses
   - Examples

2. **Code Documentation**
   - Function descriptions
   - Parameter types
   - Return values
   - Usage examples

3. **Architecture Docs**
   - System overview
   - Component relationships
   - Data flow
   - Deployment

Tool Configuration Options

Option 1: Inherit All Tools

---
name: full-access-agent
description: Agent with all available tools
---

Option 2: Specify Individual Tools

---
name: limited-agent
description: Agent with specific tools only
tools: Read, Grep, Glob, Bash
---

Option 3: Conditional Tool Access

---
name: conditional-agent
description: Agent with filtered tool access
tools: Read, Bash(npm:*), Bash(test:*)
---

Using Subagents

Via CLI

claude --agents '{
  "code-reviewer": {
    "description": "Expert code reviewer",
    "prompt": "You are a senior code reviewer...",
    "tools": ["Read", "Grep", "Glob"],
    "model": "sonnet"
  }
}'

Via /agents Command

/agents

Opens an interactive menu to create, view, edit, and manage subagents.

Background Subagents

Run subagents as background tasks:

---
name: background-analysis
description: Analyze codebase in background
background: true
---

Perform comprehensive codebase analysis...

Worktree Isolation

Give subagents their own git worktree:

---
name: isolated-work
description: Work in isolated environment
isolation: worktree
---

Make changes in isolation...

Best Practices

  1. Clear descriptions: Include when to use the agent
  2. Limit tools: Only grant necessary tools
  3. Focused purpose: One agent, one job
  4. Test thoroughly: Verify agent behavior
  5. Document well: Clear system prompts