Files
claude-howto/04-subagents/README.md
T
Luong NGUYEN deb7e5c710 docs: sync to Claude Code v2.1.233 (#168)
* docs: sync to Claude Code v2.1.233

Bring the tutorial from the v2.1.220 baseline up to v2.1.233 (13 releases)
per update-plan-2026-08-15.md.

Corrections:
- Remove /ultraplan (removed in v2.1.222); point at plan mode instead
- Drop the 200-subagent-per-session cap and CLAUDE_CODE_MAX_SUBAGENTS_PER_SESSION
  (cap removed in v2.1.224)
- Note TaskCompleted/TaskCreated only fire when todo tools are enabled

Updates:
- /review documented as an alias of /code-review (v2.1.223)
- fork subagents default to interactive (v2.1.232)
- ultrareview v2.1.227 floor, --post/--no-post
- GitLab MR support for -w/--worktree and Agent View

New sections:
- Cross-session messaging (ListAgents, SendMessage, crossSessionInbound)
- Sandbox credential masking (v2.1.221, v2.1.224)
- Trust and permission scoping
- archive and command plugin sources, conditional plugin activation
- claude self-hosted-runner, 7 new env vars

Translations (vi/ja/zh/uk) mirrored where the corresponding pages exist.
Every modified file carries the 2.1.233 metadata footer; files already at
2.1.220 with no content change were left alone.

* docs: address review findings from the v2.1.233 sync

- Mirror the /review alias correction (v2.1.223) into vi/ja/uk/zh
  01-slash-commands: add the live alias row, drop the stale row from the
  deprecated table, and rewrite the history bullet. All four still called
  /review deprecated in favour of a code-review plugin while carrying a
  2.1.233 stamp.
- Rewrite the 10-cli CLAUDE_CODE_FORK_SUBAGENT row to agree with
  04-subagents: fork mode is on by default in interactive sessions on every
  build since v2.1.232; the variable enables it where it is off by default.
- Remove a stray blank line that split the 10-cli environment-variable
  table, leaving the seven new v2.1.221-v2.1.233 rows without a delimiter
  row and rendering them as literal pipe text.
- Add the todo-tools caveat to the TaskCompleted row alongside TaskCreated
  in claude_concepts_guide, uk/ja concepts guides, and the uk/vi/zh/ja
  CATALOGs.
- Fix the ja 07-plugins and 09-advanced-features footers, which paired a
  2.1.233 version stamp with an April 24 last-updated date.

* docs(subagents): correct the stale fork-subagent claim in ja pages (#171)

ja/10-cli and ja/04-subagents both stated that CLAUDE_CODE_FORK_SUBAGENT
only enables forked subagents on external builds (Bedrock, Vertex,
Foundry) with no effect on the Anthropic API. That was true at v2.1.117
but has been wrong since v2.1.232: fork mode is on by default in
interactive sessions on every build, first-party or not, and the variable
now turns it on where it is off by default (claude -p, the Agent SDK, or
Claude Code older than v2.1.232).

Both pages remain stamped 2.1.119 on purpose. Only the individual claim
was verified and corrected; bumping the page stamp would assert a
whole-page verification that has not been done. Do not bump these stamps
without a full resync of the pages.
2026-08-17 12:44:44 +07:00

1325 lines
47 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<picture>
<source media="(prefers-color-scheme: dark)" srcset="../resources/logos/claude-howto-logo-dark.svg">
<img alt="Claude How To" src="../resources/logos/claude-howto-logo.svg">
</picture>
# Subagents - Complete Reference Guide
Subagents are specialized AI assistants that Claude Code can delegate tasks to. Each subagent has a specific purpose, uses its own context window separate from the main conversation, and can be configured with specific tools and a custom system prompt.
## Table of Contents
1. [Overview](#overview)
2. [Key Benefits](#key-benefits)
3. [File Locations](#file-locations)
4. [Configuration](#configuration)
5. [Built-in Subagents](#built-in-subagents)
6. [Managing Subagents](#managing-subagents)
7. [Using Subagents](#using-subagents)
8. [Resumable Agents](#resumable-agents)
9. [Chaining Subagents](#chaining-subagents)
10. [Persistent Memory for Subagents](#persistent-memory-for-subagents)
11. [Background Subagents](#background-subagents)
12. [Worktree Isolation](#worktree-isolation)
13. [Restrict Spawnable Subagents](#restrict-spawnable-subagents)
14. [`claude agents` CLI Command](#claude-agents-cli-command)
15. [Agent Teams (Experimental)](#agent-teams-experimental)
16. [Plugin Subagent Security](#plugin-subagent-security)
17. [Architecture](#architecture)
18. [Context Management](#context-management)
19. [When to Use Subagents](#when-to-use-subagents)
20. [Best Practices](#best-practices)
21. [Example Subagents in This Folder](#example-subagents-in-this-folder)
22. [Installation Instructions](#installation-instructions)
23. [Related Concepts](#related-concepts)
---
## Overview
Subagents enable delegated task execution in Claude Code 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 specialized tasks
Each subagent operates independently with a clean slate, receiving only the specific context necessary for their task, then returning results to the main agent for synthesis.
**Quick Start**: Ask Claude to create a subagent for you ("create a subagent that reviews security"), or add a `.claude/agents/<name>.md` file directly — see [Managing Subagents](#managing-subagents) below.
> **Note**: As of v2.1.198, the `/agents` command no longer opens an interactive creation wizard. Create and manage subagents by asking Claude or editing `.claude/agents/` files directly.
---
## Key Benefits
| Benefit | Description |
|---------|-------------|
| **Context preservation** | Operates in separate context, preventing pollution of main conversation |
| **Specialized expertise** | Fine-tuned for specific domains with higher success rates |
| **Reusability** | Use across different projects and share with teams |
| **Flexible permissions** | Different tool access levels for different subagent types |
| **Scalability** | Multiple agents work on different aspects simultaneously |
---
## File Locations
Subagent files can be stored in multiple locations with different scopes:
| Priority | Type | Location | Scope |
|----------|------|----------|-------|
| 1 (highest) | **CLI-defined** | Via `--agents` flag (JSON) | Session only |
| 2 | **Project subagents** | `.claude/agents/` | Current project |
| 3 | **User subagents** | `~/.claude/agents/` | All projects |
| 4 (lowest) | **Plugin agents** | Plugin `agents/` directory | Via plugins |
When duplicate names exist, higher-priority sources take precedence.
> **Nested `.claude/` precedence (v2.1.178)**: When the same agent name is defined in multiple nested `.claude/agents/` directories (for example, a monorepo with package-level `.claude/` folders), the definition **closest to your current working directory wins**. The same closest-wins rule applies to nested workflow and output-style definitions.
---
## Configuration
### File Format
Subagents are defined in YAML frontmatter followed by the system prompt in markdown:
```yaml
---
name: your-sub-agent-name
description: Description of when this subagent should be invoked
tools: tool1, tool2, tool3 # Optional - inherits all tools if omitted
disallowedTools: tool4 # Optional - explicitly disallowed tools
model: sonnet # Optional - sonnet, opus, haiku, or inherit
permissionMode: default # Optional - permission mode
maxTurns: 20 # Optional - limit agentic turns
skills: skill1, skill2 # Optional - skills to preload into context
mcpServers: server1 # Optional - MCP servers to make available
memory: user # Optional - persistent memory scope (user, project, local)
background: false # Optional - run as background task
effort: high # Optional - reasoning effort (low, medium, high, xhigh, max)
isolation: worktree # Optional - git worktree isolation
initialPrompt: "Start by analyzing the codebase" # Optional - auto-submitted first turn
hooks: # Optional - component-scoped hooks
PreToolUse:
- matcher: "Bash"
hooks:
- type: command
command: "./scripts/security-check.sh"
---
Your subagent's system prompt goes here. This can be multiple paragraphs
and should clearly define the subagent's role, capabilities, and approach
to solving problems.
```
### Configuration Fields
| Field | Required | Description |
|-------|----------|-------------|
| `name` | Yes | Unique identifier (lowercase letters and hyphens). Lookup is normalized (case- and separator-insensitive — see below), but a name containing `:` is **rejected** as of v2.1.218: `:` is reserved for plugin namespacing |
| `description` | Yes | Natural language description of purpose. Include "use PROACTIVELY" to encourage automatic invocation |
| `tools` | No | Comma-separated list of specific tools. Omit to inherit all tools. Supports `Agent(agent_name)` syntax to restrict spawnable subagents |
| `disallowedTools` | No | Comma-separated list of tools the subagent must not use |
| `model` | No | Model to use: `sonnet`, `opus`, `haiku`, full model ID, or `inherit`. Defaults to configured subagent model |
| `permissionMode` | No | `default`, `acceptEdits`, `dontAsk`, `bypassPermissions`, `plan`. As of v2.1.212, the Task tool's `mode` invocation parameter is deprecated and ignored — subagents inherit the parent session's permission mode by default unless overridden here |
| `maxTurns` | No | Maximum number of agentic turns the subagent can take |
| `skills` | No | Comma-separated list of skills to preload. Injects full skill content into the subagent's context at startup. **v2.1.133+:** subagents also discover project, user, and plugin skills via the Skill tool — same catalog as the main session, no longer limited to their own embedded set. |
| `mcpServers` | No | MCP servers to make available to the subagent |
| `hooks` | No | Component-scoped hooks (PreToolUse, PostToolUse, Stop) |
| `memory` | No | Persistent memory directory scope: `user`, `project`, or `local` |
| `background` | No | Subagents already run in the background by default (v2.1.198). Set to `true` to *force* background always and prevent inline execution |
| `effort` | No | Reasoning effort level: `low`, `medium`, `high`, `xhigh`, or `max`. Overrides the session effort level; available levels depend on the model |
| `isolation` | No | Set to `worktree` to give the subagent its own git worktree |
| `initialPrompt` | No | Auto-submitted first turn when the subagent runs as the main agent |
| `color` | No | Display color for the subagent in the task list and transcript. Accepts `red`, `blue`, `green`, `yellow`, `purple`, `orange`, `pink`, or `cyan` |
### Main-Thread Agent Frontmatter Honoring (v2.1.117+/v2.1.119+)
When an agent is invoked as the main-thread agent (via `claude --agent <name>` or `--print` mode), these frontmatter fields are honored:
| Field | Version | Notes |
|-------|---------|-------|
| `mcpServers` | v2.1.117+ | Loaded when agent is invoked as main-thread agent via `claude --agent <name>` |
| `permissionMode` | v2.1.119+ | Honored for built-in agents via `--agent <name>` |
| `tools` / `disallowedTools` | v2.1.119+ | Honored in `--print` mode (non-interactive/scripted usage) |
**Example — agent with `mcpServers` and `permissionMode`:**
```yaml
---
name: secure-researcher
description: Research agent with scoped MCP access and restricted permissions
permissionMode: acceptEdits
mcpServers:
notion:
type: http
url: https://mcp.notion.com/mcp
github:
type: http
url: https://api.github.com/mcp
tools: Read, Grep, Glob
---
You are a research agent. You may query Notion and GitHub through the
configured MCP servers, and read local files, but you cannot write or
execute commands outside of accepted edits.
```
Run with:
```bash
claude --agent secure-researcher
```
### Tool Configuration Options
**Option 1: Inherit All Tools (omit the field)**
```yaml
---
name: full-access-agent
description: Agent with all available tools
---
```
**Option 2: Specify Individual Tools**
```yaml
---
name: limited-agent
description: Agent with specific tools only
tools: Read, Grep, Glob, Bash
---
```
> **Note on Glob/Grep (v2.1.113+):** On native macOS/Linux builds, Glob and Grep are provided as `bfs`/`ugrep` through the Bash tool rather than as separate tools. Windows and npm-JS builds still expose them as standalone tools. Authors can still reference Glob/Grep in `allowedTools`; the backend substitution is transparent.
**Option 3: Conditional Tool Access**
```yaml
---
name: conditional-agent
description: Agent with filtered tool access
tools: Read, Bash(npm:*), Bash(test:*)
---
```
### CLI-Based Configuration
Define subagents for a single session using the `--agents` flag with JSON format:
```bash
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"
}
}'
```
**JSON Format for `--agents` flag:**
```json
{
"agent-name": {
"description": "Required: when to invoke this agent",
"prompt": "Required: system prompt for the agent",
"tools": ["Optional", "array", "of", "tools"],
"model": "optional: sonnet|opus|haiku"
}
}
```
**Priority of Agent Definitions:**
Agent definitions are loaded with this priority order (first match wins):
1. **CLI-defined** - `--agents` flag (session only, JSON)
2. **Project-level** - `.claude/agents/` (current project)
3. **User-level** - `~/.claude/agents/` (all projects)
4. **Plugin-level** - Plugin `agents/` directory
This allows CLI definitions to override all other sources for a single session.
---
## Built-in Subagents
Claude Code includes several built-in subagents that are always available:
| Agent | Model | Purpose |
|-------|-------|---------|
| **general-purpose** | Inherits | Complex, multi-step tasks |
| **Plan** | Inherits | Research for plan mode |
| **Explore** | Inherits (capped at Opus) | Read-only codebase exploration (quick/medium/very thorough) |
| **Bash** | Inherits | Terminal commands in separate context |
| **statusline-setup** | Sonnet | Configure status line |
| **Claude Code Guide** | Haiku | Answer Claude Code feature questions |
### General-Purpose Subagent
| Property | Value |
|----------|-------|
| **Model** | Inherits from parent |
| **Tools** | All tools |
| **Purpose** | Complex research tasks, multi-step operations, code modifications |
**When used**: Tasks requiring both exploration and modification with complex reasoning.
### Plan Subagent
| Property | Value |
|----------|-------|
| **Model** | Inherits from parent |
| **Tools** | Read, Glob, Grep, Bash |
| **Purpose** | Used automatically in plan mode to research codebase |
**When used**: When Claude needs to understand the codebase before presenting a plan.
### Explore Subagent
| Property | Value |
|----------|-------|
| **Model** | Inherits the session model, capped at Opus (v2.1.198). Set `model: haiku` to keep it fast and cheap |
| **Mode** | Strictly read-only |
| **Tools** | Glob, Grep, Read, Bash (read-only commands only) |
| **Purpose** | Fast codebase searching and analysis |
**When used**: When searching/understanding code without making changes.
**Thoroughness Levels** - Specify the depth of exploration:
- **"quick"** - Fast searches with minimal exploration, good for finding specific patterns
- **"medium"** - Moderate exploration, balanced speed and thoroughness, default approach
- **"very thorough"** - Comprehensive analysis across multiple locations and naming conventions, may take longer
### Bash Subagent
| Property | Value |
|----------|-------|
| **Model** | Inherits from parent |
| **Tools** | Bash |
| **Purpose** | Execute terminal commands in a separate context window |
**When used**: When running shell commands that benefit from isolated context.
### Statusline Setup Subagent
| Property | Value |
|----------|-------|
| **Model** | Sonnet |
| **Tools** | Read, Write, Bash |
| **Purpose** | Configure the Claude Code status line display |
**When used**: When setting up or customizing the status line.
### Claude Code Guide Subagent
| Property | Value |
|----------|-------|
| **Model** | Haiku (fast, low-latency) |
| **Tools** | Read-only |
| **Purpose** | Answer questions about Claude Code features and usage |
**When used**: When users ask questions about how Claude Code works or how to use specific features.
---
## Managing Subagents
### Ask Claude (Recommended)
The simplest way to create or manage a subagent is to ask Claude directly:
```text
Create a subagent that reviews code for security vulnerabilities.
```
Claude writes the `.claude/agents/<name>.md` file for you, choosing sensible frontmatter (tools, model, description). You can then refine the file by hand or ask Claude to adjust it.
> **Note**: The `/agents` command no longer opens an interactive creation wizard (removed in v2.1.198). It now points you to ask Claude or edit `.claude/agents/` files directly.
### Direct File Management
```bash
# Create a project subagent
mkdir -p .claude/agents
cat > .claude/agents/test-runner.md << 'EOF'
---
name: test-runner
description: Use proactively to run tests and fix failures
---
You are a test automation expert. When you see code changes, proactively
run the appropriate tests. If tests fail, analyze the failures and fix
them while preserving the original test intent.
EOF
# Create a user subagent (available in all projects)
mkdir -p ~/.claude/agents
```
---
## Using Subagents
### Automatic Delegation
Claude proactively delegates tasks based on:
- Task description in your request
- The `description` field in subagent configurations
- Current context and available tools
To encourage proactive use, include "use PROACTIVELY" or "MUST BE USED" in your `description` field:
```yaml
---
name: code-reviewer
description: Expert code review specialist. Use PROACTIVELY after writing or modifying code.
---
```
### Explicit Invocation
You can explicitly request a specific subagent:
```
> 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
```
> **Case- and separator-insensitive `subagent_type` matching (v2.1.140)**: `subagent_type` (in `Agent` tool calls or `--agent` flags) is matched case-insensitively and ignores separator style — `code-reviewer`, `Code Reviewer`, and `code_reviewer` all resolve to the same agent. This removes a long-standing footgun where minor capitalization differences silently fell back to the default agent.
### @-Mention Invocation
Use the `@` prefix to guarantee a specific subagent is invoked (bypasses automatic delegation heuristics):
```
> @"code-reviewer (agent)" review the auth module
```
### Session-Wide Agent
Run an entire session using a specific agent as the main agent:
```bash
# Via CLI flag
claude --agent code-reviewer
# Via settings.json
{
"agent": "code-reviewer"
}
```
### Listing Available Agents
Use the `claude agents` command to list all configured agents from all sources:
```bash
claude agents
```
---
## Resumable Agents
Subagents can continue previous conversations with full context preserved:
```bash
# Initial invocation
> Use the code-analyzer agent to start reviewing the authentication module
# Returns agentId: "abc123"
# Resume the agent later
> Resume agent abc123 and now analyze the authorization logic as well
```
**Use cases**:
- Long-running research across multiple sessions
- Iterative refinement without losing context
- Multi-step workflows maintaining context
---
## Chaining Subagents
Execute multiple subagents in sequence:
```bash
> First use the code-analyzer subagent to find performance issues,
then use the optimizer subagent to fix them
```
This enables complex workflows where the output of one subagent feeds into another.
---
## Persistent Memory for Subagents
The `memory` field gives subagents a persistent directory that survives across conversations. This allows subagents to build up knowledge over time, storing notes, findings, and context that persist between sessions.
### Memory Scopes
| Scope | Directory | Use Case |
|-------|-----------|----------|
| `user` | `~/.claude/agent-memory/<name>/` | Personal notes and preferences across all projects |
| `project` | `.claude/agent-memory/<name>/` | Project-specific knowledge shared with the team |
| `local` | `.claude/agent-memory-local/<name>/` | Local project knowledge not committed to version control |
### How It Works
- The first 200 lines of `MEMORY.md` in the memory directory are automatically loaded into the subagent's system prompt
- The `Read`, `Write`, and `Edit` tools are automatically enabled for the subagent to manage its memory files
- The subagent can create additional files in its memory directory as needed
### Example Configuration
```yaml
---
name: researcher
memory: user
---
You are a research assistant. Use your memory directory to store findings,
track progress across sessions, and build up knowledge over time.
Check your MEMORY.md file at the start of each session to recall previous context.
```
```mermaid
graph LR
A["Subagent<br/>Session 1"] -->|writes| M["MEMORY.md<br/>(persistent)"]
M -->|loads into| B["Subagent<br/>Session 2"]
B -->|updates| M
M -->|loads into| C["Subagent<br/>Session 3"]
style A fill:#e1f5fe,stroke:#333,color:#333
style B fill:#e1f5fe,stroke:#333,color:#333
style C fill:#e1f5fe,stroke:#333,color:#333
style M fill:#f3e5f5,stroke:#333,color:#333
```
---
## Background Subagents
Subagents run in the background by default (v2.1.198). Claude keeps working on the main conversation while a subagent runs and is notified when it finishes, so you no longer wait on a subagent to return before continuing.
### Configuration
Because background is already the default, `background: true` in the frontmatter *forces* the subagent to always run in the background and prevents it from running inline:
```yaml
---
name: long-runner
background: true
description: Performs long-running analysis tasks in the background
---
```
### Keyboard Shortcuts
| Shortcut | Action |
|----------|--------|
| `Ctrl+B` | Background a currently running subagent task |
| `Ctrl+F` | Kill all background agents (press twice to confirm) |
### Disabling Background Tasks
Set the environment variable to disable background task support entirely:
```bash
export CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1
```
---
## Worktree Isolation
The `isolation: worktree` setting gives a subagent its own git worktree, allowing it to make changes independently without affecting the main working tree.
### Configuration
```yaml
---
name: feature-builder
isolation: worktree
description: Implements features in an isolated git worktree
tools: Read, Write, Edit, Bash, Grep, Glob
---
```
### How It Works
```mermaid
graph TB
Main["Main Working Tree"] -->|spawns| Sub["Subagent with<br/>Isolated Worktree"]
Sub -->|makes changes in| WT["Separate Git<br/>Worktree + Branch"]
WT -->|no changes| Clean["Auto-cleaned"]
WT -->|has changes| Return["Returns worktree<br/>path and branch"]
style Main fill:#e1f5fe,stroke:#333,color:#333
style Sub fill:#f3e5f5,stroke:#333,color:#333
style WT fill:#e8f5e9,stroke:#333,color:#333
style Clean fill:#fff3e0,stroke:#333,color:#333
style Return fill:#fff3e0,stroke:#333,color:#333
```
- The subagent operates in its own git worktree on a separate branch
- If the subagent makes no changes, the worktree is automatically cleaned up
- If changes exist, the worktree path and branch name are returned to the main agent for review or merging
---
## Forked Subagents
Forked subagents (`context: fork`) inherit the parent agent's full conversation context at the moment of forking, rather than starting with a clean slate. This is useful for exploring alternative paths without losing the work done so far.
> **Availability**: GA in v2.1.117. **Since v2.1.232, fork mode is on by default in interactive sessions** — on every build, first-party or not. It stays off by default in non-interactive mode (`claude -p`) and in the Agent SDK. On Claude Code older than v2.1.232, or to turn it on where it is off by default, set `CLAUDE_CODE_FORK_SUBAGENT=1`.
> **Fork-mode subagents run in the background.** Where fork mode is on — as it is by default in an interactive session — Claude Code runs the subagent in the background, forked and non-forked subagents alike.
### Configuration
```yaml
---
name: alternative-explorer
description: Explore an alternative implementation path while preserving parent context
context: fork
tools: Read, Edit, Bash, Grep, Glob
---
You are a forked subagent. You inherit the parent's full conversation and
may explore an alternative approach. Return your findings and the parent
will decide whether to adopt them.
```
### Enabling Fork Mode Explicitly
Interactive sessions on v2.1.232+ need no flag. Use this on older versions, in headless
runs, or in the Agent SDK:
```bash
export CLAUDE_CODE_FORK_SUBAGENT=1
claude
```
### When to Use Fork vs Clean Context
| Scenario | `context: fork` | Clean context (default) |
|----------|-----------------|-------------------------|
| Explore alternative implementations | Yes | No (would lose context) |
| Long research with existing context | Yes | No |
| Independent specialized task | No | Yes |
| Avoiding context pollution | No | Yes |
---
## Restrict Spawnable Subagents
You can control which subagents a given subagent is allowed to spawn by using the `Agent(agent_type)` syntax in the `tools` field. This provides a way to allowlist specific subagents for delegation.
> **Note**: In v2.1.63, the `Task` tool was renamed to `Agent`. Existing `Task(...)` references still work as aliases.
### Example
```yaml
---
name: coordinator
description: Coordinates work between specialized agents
tools: Agent(worker, researcher), Read, Bash
---
You are a coordinator agent. You can delegate work to the "worker" and
"researcher" subagents only. Use Read and Bash for your own exploration.
```
In this example, the `coordinator` subagent can only spawn the `worker` and `researcher` subagents. It cannot spawn any other subagents, even if they are defined elsewhere.
---
## `claude agents` CLI Command
The `claude agents` command lists all configured agents grouped by source (built-in, user-level, project-level):
```bash
claude agents
```
This command:
- Shows all available agents from all sources
- Groups agents by their source location
- Indicates **overrides** when an agent at a higher priority level shadows one at a lower level (e.g., a project-level agent with the same name as a user-level agent)
---
## Agent Teams (Experimental)
Agent Teams coordinate multiple Claude Code instances working together on complex tasks. Unlike subagents (which are delegated subtasks returning results), teammates work independently with their own context windows and can message each other directly through a shared mailbox system.
> **Official Documentation**: [code.claude.com/docs/en/agent-teams](https://code.claude.com/docs/en/agent-teams)
> **Note**: Agent Teams is experimental and disabled by default. Requires Claude Code v2.1.32+. Enable it before use.
### Subagents vs Agent Teams
| Aspect | Subagents | Agent Teams |
|--------|-----------|-------------|
| **Delegation model** | Parent delegates subtask, waits for result | Team lead coordinates work, teammates execute independently |
| **Context** | Fresh context per subtask, results distilled back | Each teammate maintains its own persistent context window |
| **Coordination** | Sequential or parallel, managed by parent | Shared task list with automatic dependency management |
| **Communication** | Results returned to parent only (no inter-agent messaging) | Teammates can message each other directly via mailbox |
| **Session resumption** | Supported | Not supported with in-process teammates |
| **Best for** | Focused, well-defined subtasks | Complex work requiring inter-agent communication and parallel execution |
### Enabling Agent Teams
Set the environment variable or add it to your `settings.json`:
```bash
export CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1
```
Or in `settings.json`:
```json
{
"env": {
"CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS": "1"
}
}
```
### Starting a team
Once enabled, ask Claude to work with teammates in your prompt:
```
User: Build the authentication module. Use a team — one teammate for the API endpoints,
one for the database schema, and one for the test suite.
```
Claude will create the team, assign tasks, and coordinate the work automatically.
### Display modes
Control how teammate activity is displayed:
| Mode | Flag | Description |
|------|------|-------------|
| **Auto** | `--teammate-mode auto` | Automatically chooses the best display mode for your terminal |
| **In-process** (default) | `--teammate-mode in-process` | Shows teammate output inline in the current terminal |
| **Split-panes** | `--teammate-mode tmux` | Opens each teammate in a separate tmux or iTerm2 pane |
| **iTerm2** | `--teammate-mode iterm2` | (v2.1.186+) Spawns teammates in dedicated iTerm2 panes. Requires the `it2` CLI; auto mode warns when it can't be found |
```bash
claude --teammate-mode tmux
```
You can also set the display mode in `settings.json`:
```json
{
"teammateMode": "tmux"
}
```
> **Note**: Split-pane mode requires tmux or iTerm2. It is not available in VS Code terminal, Windows Terminal, or Ghostty.
### Navigation
Use `Shift+Down` to navigate between teammates in split-pane mode.
### Team Configuration
Team configurations are stored at `~/.claude/teams/{team-name}/config.json`.
### Architecture
```mermaid
graph TB
Lead["Team Lead<br/>(Coordinator)"]
TaskList["Shared Task List<br/>(Dependencies)"]
Mailbox["Mailbox<br/>(Messages)"]
T1["Teammate 1<br/>(Own Context)"]
T2["Teammate 2<br/>(Own Context)"]
T3["Teammate 3<br/>(Own Context)"]
Lead -->|assigns tasks| TaskList
Lead -->|sends messages| Mailbox
TaskList -->|picks up work| T1
TaskList -->|picks up work| T2
TaskList -->|picks up work| T3
T1 -->|reads/writes| Mailbox
T2 -->|reads/writes| Mailbox
T3 -->|reads/writes| Mailbox
T1 -->|updates status| TaskList
T2 -->|updates status| TaskList
T3 -->|updates status| TaskList
style Lead fill:#e1f5fe,stroke:#333,color:#333
style TaskList fill:#fff9c4,stroke:#333,color:#333
style Mailbox fill:#f3e5f5,stroke:#333,color:#333
style T1 fill:#e8f5e9,stroke:#333,color:#333
style T2 fill:#e8f5e9,stroke:#333,color:#333
style T3 fill:#e8f5e9,stroke:#333,color:#333
```
**Key components**:
- **Team Lead**: The main Claude Code session that creates the team, assigns tasks, and coordinates
- **Shared Task List**: A synchronized list of tasks with automatic dependency tracking
- **Mailbox**: An inter-agent messaging system for teammates to communicate status and coordinate
- **Teammates**: Independent Claude Code instances, each with their own context window
### Task assignment and messaging
The team lead breaks work into tasks and assigns them to teammates. The shared task list handles:
- **Automatic dependency management** — tasks wait for their dependencies to complete
- **Status tracking** — teammates update task status as they work
- **Inter-agent messaging** — teammates send messages via the mailbox for coordination (e.g., "Database schema is ready, you can start writing queries")
### Plan approval workflow
For complex tasks, the team lead creates an execution plan before teammates begin work. The user reviews and approves the plan, ensuring the team's approach aligns with expectations before any code changes are made.
### Hook events for teams
Agent Teams introduce two additional [hook events](../06-hooks/):
| Event | Fires When | Use Case |
|-------|-----------|----------|
| `TeammateIdle` | A teammate finishes its current task and has no pending work | Trigger notifications, assign follow-up tasks |
| `TaskCompleted` | A task in the shared task list is marked complete | Run validation, update dashboards, chain dependent work |
### Best practices
- **Team size**: Keep teams at 3-5 teammates for optimal coordination
- **Task sizing**: Break work into tasks that take 5-15 minutes each — small enough to parallelize, large enough to be meaningful
- **Avoid file conflicts**: Assign different files or directories to different teammates to prevent merge conflicts
- **Start simple**: Use in-process mode for your first team; switch to split-panes once comfortable
- **Clear task descriptions**: Provide specific, actionable task descriptions so teammates can work independently
### Limitations
- **Experimental**: Feature behavior may change in future releases
- **No session resumption**: In-process teammates cannot be resumed after a session ends
- **One team per session**: Cannot create nested teams or multiple teams in a single session
- **Fixed leadership**: The team lead role cannot be transferred to a teammate
- **Split-pane restrictions**: tmux/iTerm2 required; not available in VS Code terminal, Windows Terminal, or Ghostty
- **No cross-session teams**: Teammates exist only within the current session
> **Warning**: Agent Teams is experimental. Test with non-critical work first and monitor teammate coordination for unexpected behavior.
---
## Plugin Subagent Security
Plugin-provided subagents have restricted frontmatter capabilities for security. The following fields are **not allowed** in plugin subagent definitions:
- `hooks` - Cannot define lifecycle hooks
- `mcpServers` - Cannot configure MCP servers
- `permissionMode` - Cannot override permission settings
This prevents plugins from escalating privileges or executing arbitrary commands through subagent hooks.
### Subagent Output Scanning (v2.1.210+)
As of v2.1.210, Claude Code scans each subagent's final report for text that imitates the harness's own output format — fake `<system-reminder>`-style tags, fabricated `Human:`/`Assistant:` turns, or mentions of permission-bypass flags and settings-file paths. This defends against prompt injection carried in subagent output, such as a subagent that fetched a malicious web page containing fake control tokens designed to manipulate the parent session.
When the scan flags something, Claude Code neutralizes it — inserting a backslash or an inline marker such as `[harness: subagent output matched instruction-shaped pattern(s): ...]` naming what triggered the scan — and the parent session is expected to treat the flagged text as a finding to relay, not an instruction to follow. The scan is on by default with no documented opt-out. It errs toward flagging: a legitimate subagent report that quotes a real flag name (e.g. `--dangerously-skip-permissions`) verbatim can trigger a marker even though nothing malicious occurred — a false positive is preferable to a missed injection.
### Subagent Concurrency and Depth Limits
> **The per-session spawn cap is gone.** Claude Code capped subagent spawns at 200 per session from v2.1.212, but **v2.1.224 removed that cap** — long-running sessions no longer refuse new agents, and the official subagents reference now states plainly that there is no limit on the total number of subagents Claude can spawn over a session. The `CLAUDE_CODE_MAX_SUBAGENTS_PER_SESSION` variable that overrode it is gone with it.
Two limits on subagent fan-out do still apply, both set by environment variable:
- `CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS` (v2.1.217) - Maximum number of subagents running **concurrently** at once. Default: 20.
- `CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH` (v2.1.217) - Maximum **nesting depth** for subagents spawning their own subagents. **Default: 3 since v2.1.219** (was 1 in v2.1.217v2.1.218). Set this to `1` to disable nesting (see [Key Behaviors](#key-behaviors)).
```bash
export CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS=20
export CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH=5
```
---
## Architecture
### High-Level Architecture
```mermaid
graph TB
User["User"]
Main["Main Agent<br/>(Coordinator)"]
Reviewer["Code Reviewer<br/>Subagent"]
Tester["Test Engineer<br/>Subagent"]
Docs["Documentation<br/>Subagent"]
User -->|asks| Main
Main -->|delegates| Reviewer
Main -->|delegates| Tester
Main -->|delegates| Docs
Reviewer -->|returns result| Main
Tester -->|returns result| Main
Docs -->|returns result| Main
Main -->|synthesizes| User
```
### Subagent Lifecycle
```mermaid
sequenceDiagram
participant User
participant MainAgent as Main Agent
participant CodeReviewer as Code Reviewer<br/>Subagent
participant Context as Separate<br/>Context Window
User->>MainAgent: "Build new auth feature"
MainAgent->>MainAgent: Analyze task
MainAgent->>CodeReviewer: "Review this code"
CodeReviewer->>Context: Initialize clean context
Context->>CodeReviewer: Load reviewer instructions
CodeReviewer->>CodeReviewer: Perform review
CodeReviewer-->>MainAgent: Return findings
MainAgent->>MainAgent: Incorporate results
MainAgent-->>User: Provide synthesis
```
---
## Context Management
```mermaid
graph TB
A["Main Agent Context<br/>50,000 tokens"]
B["Subagent 1 Context<br/>20,000 tokens"]
C["Subagent 2 Context<br/>20,000 tokens"]
D["Subagent 3 Context<br/>20,000 tokens"]
A -->|Clean slate| B
A -->|Clean slate| C
A -->|Clean slate| D
B -->|Results only| A
C -->|Results only| A
D -->|Results only| A
style A fill:#e1f5fe
style B fill:#fff9c4
style C fill:#fff9c4
style D fill:#fff9c4
```
### Key Points
- Each subagent gets a **fresh context window** without the main conversation history
- Only the **relevant context** is passed to the subagent for their specific task
- Results are **distilled** back to the main agent
- This prevents **context token exhaustion** on long projects
### Performance Considerations
- **Context efficiency** - Agents preserve main context, enabling longer sessions
- **Latency** - Subagents start with clean slate and may add latency gathering initial context
### Key Behaviors
- **Nested spawning on by default, depth 3 (v2.1.219)** - Subagents can spawn their own subagents up to three layers below the main conversation. Set `CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH` to change the limit, or `1` to turn nesting off. At the depth limit, Claude Code withholds the `Agent` tool from every subagent except a fork. (History: v2.1.172v2.1.216 nested by default up to 5 layers with no way to change it; v2.1.217 made nesting opt-in at depth 1; v2.1.219 set the default to 3.) Use the `Agent(agent_type)` restriction syntax (see [Restrict Spawnable Subagents](#restrict-spawnable-subagents)) to control which subagents a given subagent may spawn
- **Background permissions** - Background subagents auto-deny any permissions that are not pre-approved
- **Backgrounding** - Press `Ctrl+B` to background a currently running task
- **Transcripts** - Subagent transcripts are stored at `~/.claude/projects/{project}/{sessionId}/subagents/agent-{agentId}.jsonl`
- **Auto-compaction** - Subagent context auto-compacts at ~95% capacity (override with `CLAUDE_AUTOCOMPACT_PCT_OVERRIDE` environment variable)
- **Extended thinking inherited (v2.1.198)** - Subagents and context compaction now inherit the session's extended-thinking configuration (previously always disabled). There is no per-subagent thinking field
### Additional Controls
- **Disable built-in Explore/Plan agents** - Set `CLAUDE_CODE_DISABLE_EXPLORE_PLAN_AGENTS=1` to remove the built-in Explore and Plan agents (v2.1.198)
- **Append to every subagent prompt** - In non-interactive / `--print` mode, `--append-subagent-system-prompt "<text>"` appends text to every subagent's system prompt (v2.1.205)
---
## When to Use Subagents
| Scenario | Use Subagent | Why |
|----------|--------------|-----|
| Complex feature with many steps | Yes | Separate concerns, prevent context pollution |
| Quick code review | No | Unnecessary overhead |
| Parallel task execution | Yes | Each subagent has own context |
| Specialized expertise needed | Yes | Custom system prompts |
| Long-running analysis | Yes | Prevents main context exhaustion |
| Single task | No | Adds latency unnecessarily |
---
## Best Practices
### Design Principles
**Do:**
- Start with Claude-generated agents - Generate initial subagent with Claude, then iterate to customize
- Design focused subagents - Single, clear responsibilities rather than one doing everything
- Write detailed prompts - Include specific instructions, examples, and constraints
- Limit tool access - Grant only necessary tools for the subagent's purpose
- Version control - Check project subagents into version control for team collaboration
**Don't:**
- Create overlapping subagents with same roles
- Give subagents unnecessary tool access
- Use subagents for simple, single-step tasks
- Mix concerns in one subagent's prompt
- Forget to pass necessary context
### System Prompt Best Practices
1. **Be Specific About Role**
```
You are an expert code reviewer specializing in [specific areas]
```
2. **Define Priorities Clearly**
```
Review priorities (in order):
1. Security Issues
2. Performance Problems
3. Code Quality
```
3. **Specify Output Format**
```
For each issue provide: Severity, Category, Location, Description, Fix, Impact
```
4. **Include Action Steps**
```
When invoked:
1. Run git diff to see recent changes
2. Focus on modified files
3. Begin review immediately
```
### Tool Access Strategy
1. **Start Restrictive**: Begin with only essential tools
2. **Expand Only When Needed**: Add tools as requirements demand
3. **Read-Only When Possible**: Use Read/Grep for analysis agents
4. **Sandboxed Execution**: Limit Bash commands to specific patterns
---
## Example Subagents in This Folder
This folder contains ready-to-use example subagents:
### 1. Code Reviewer (`code-reviewer.md`)
**Purpose**: Comprehensive code quality and maintainability analysis
**Tools**: Read, Grep, Glob, Bash
**Specialization**:
- Security vulnerability detection
- Performance optimization identification
- Code maintainability assessment
- Test coverage analysis
**Use When**: You need automated code reviews with focus on quality and security
---
### 2. Test Engineer (`test-engineer.md`)
**Purpose**: Test strategy, coverage analysis, and automated testing
**Tools**: Read, Write, Bash, Grep
**Specialization**:
- Unit test creation
- Integration test design
- Edge case identification
- Coverage analysis (>80% target)
**Use When**: You need comprehensive test suite creation or coverage analysis
---
### 3. Documentation Writer (`documentation-writer.md`)
**Purpose**: Technical documentation, API docs, and user guides
**Tools**: Read, Write, Grep
**Specialization**:
- API endpoint documentation
- User guide creation
- Architecture documentation
- Code comment improvement
**Use When**: You need to create or update project documentation
---
### 4. Secure Reviewer (`secure-reviewer.md`)
**Purpose**: Security-focused code review with minimal permissions
**Tools**: Read, Grep
**Specialization**:
- Security vulnerability detection
- Authentication/authorization issues
- Data exposure risks
- Injection attack identification
**Use When**: You need security audits without modification capabilities
---
### 5. Implementation Agent (`implementation-agent.md`)
**Purpose**: Full implementation capabilities for feature development
**Tools**: Read, Write, Edit, Bash, Grep, Glob
**Specialization**:
- Feature implementation
- Code generation
- Build and test execution
- Codebase modification
**Use When**: You need a subagent to implement features end-to-end
---
### 6. Debugger (`debugger.md`)
**Purpose**: Debugging specialist for errors, test failures, and unexpected behavior
**Tools**: Read, Edit, Bash, Grep, Glob
**Specialization**:
- Root cause analysis
- Error investigation
- Test failure resolution
- Minimal fix implementation
**Use When**: You encounter bugs, errors, or unexpected behavior
---
### 7. Data Scientist (`data-scientist.md`)
**Purpose**: Data analysis expert for SQL queries and data insights
**Tools**: Bash, Read, Write
**Specialization**:
- SQL query optimization
- BigQuery operations
- Data analysis and visualization
- Statistical insights
**Use When**: You need data analysis, SQL queries, or BigQuery operations
---
### 8. Clean Code Reviewer (`clean-code-reviewer.md`)
**Purpose**: Readability and maintainability review against clean-code principles
**Tools**: Read, Grep, Glob, Bash
**Specialization**:
- Naming, function length, and argument count
- Duplication and dead code
- Comment quality and intent
- Structural clarity over cleverness
**Use When**: You want a style and maintainability pass distinct from correctness review
---
### 9. Performance Optimizer (`performance-optimizer.md`)
**Purpose**: Identify and remediate performance bottlenecks
**Tools**: Read, Edit, Bash, Grep, Glob
**Specialization**:
- Algorithmic complexity and hot paths
- Memory allocation and leaks
- Caching and query optimization
- Concurrency and I/O bottlenecks
**Use When**: Code is measurably slow and you need targeted optimization
---
## Installation Instructions
### Method 1: Ask Claude (Recommended)
Describe the subagent you want and let Claude create the file:
```text
Create a project-level subagent that runs tests and fixes failures.
Give it access to Bash, Read, Edit, and Grep.
```
Claude writes `.claude/agents/<name>.md` with appropriate frontmatter. Review the generated file, then use it. (The `/agents` interactive creation wizard was removed in v2.1.198 — ask Claude or edit the file directly instead.)
### Method 2: Copy to Project
Copy the agent files to your project's `.claude/agents/` directory:
```bash
# Navigate to your project
cd /path/to/your/project
# Create agents directory if it doesn't exist
mkdir -p .claude/agents
# Copy all agent files from this folder
cp /path/to/04-subagents/*.md .claude/agents/
# Remove the README (not needed in .claude/agents)
rm .claude/agents/README.md
```
### Method 3: Copy to User Directory
For agents available in all your projects:
```bash
# Create user agents directory
mkdir -p ~/.claude/agents
# Copy agents
cp /path/to/04-subagents/code-reviewer.md ~/.claude/agents/
cp /path/to/04-subagents/debugger.md ~/.claude/agents/
# ... copy others as needed
```
### Verification
After installation, verify the agents are recognized by listing the directory:
```bash
ls .claude/agents/
```
You can also ask Claude which subagents are available in the current session, and it will report the built-in and custom agents it can delegate to.
---
## File Structure
```
project/
├── .claude/
│ └── agents/
│ ├── code-reviewer.md
│ ├── test-engineer.md
│ ├── documentation-writer.md
│ ├── secure-reviewer.md
│ ├── implementation-agent.md
│ ├── debugger.md
│ ├── data-scientist.md
│ ├── clean-code-reviewer.md
│ └── performance-optimizer.md
└── ...
```
---
## Related Concepts
### Related Features
- **[Slash Commands](../01-slash-commands/)** - Quick user-invoked shortcuts
- **[Memory](../02-memory/)** - Persistent cross-session context
- **[Skills](../03-skills/)** - Reusable autonomous capabilities
- **[MCP Protocol](../05-mcp/)** - Real-time external data access
- **[Hooks](../06-hooks/)** - Event-driven shell command automation
- **[Plugins](../07-plugins/)** - Bundled extension packages
### Comparison with Other Features
| Feature | User-Invoked | Auto-Invoked | Persistent | External Access | Isolated Context |
|---------|--------------|--------------|-----------|------------------|------------------|
| **Slash Commands** | Yes | No | No | No | No |
| **Subagents** | Yes | Yes | No | No | Yes |
| **Memory** | Auto | Auto | Yes | No | No |
| **MCP** | Auto | Yes | No | Yes | No |
| **Skills** | Yes | Yes | No | No | No |
### Integration Pattern
```mermaid
graph TD
User["User Request"] --> Main["Main Agent"]
Main -->|Uses| Memory["Memory<br/>(Context)"]
Main -->|Queries| MCP["MCP<br/>(Live Data)"]
Main -->|Invokes| Skills["Skills<br/>(Auto Tools)"]
Main -->|Delegates| Subagents["Subagents<br/>(Specialists)"]
Subagents -->|Use| Memory
Subagents -->|Query| MCP
Subagents -->|Isolated| Context["Clean Context<br/>Window"]
```
---
## Observability
> **Added in v2.1.139.**
API requests originating from a subagent carry two extra HTTP headers so traces and logs can be correlated back to the dispatching session:
| Header | Description |
|--------|-------------|
| `x-claude-code-agent-id` | UUID of the subagent making the request. |
| `x-claude-code-parent-agent-id` | UUID of the agent that dispatched this subagent (the main agent, or a higher-level subagent in a chain). |
The same identifiers are exposed on `claude_code.llm_request` OpenTelemetry spans as the attributes `claude.code.agent.id` and `claude.code.agent.parent_id`. Use them to:
- Attribute API spend to a specific subagent type rather than the parent session
- Reconstruct a chain of agent invocations after the fact (parent_id forms a tree)
- Alert on runaway subagents (e.g., one `agent.id` accounting for >50% of session spend)
See the OpenTelemetry section in [Advanced Features → Telemetry](../09-advanced-features/README.md) for end-to-end exporter setup.
## Additional Resources
- [Official Subagents Documentation](https://code.claude.com/docs/en/sub-agents)
- [CLI Reference](https://code.claude.com/docs/en/cli-reference) - `--agents` flag and other CLI options
- [Plugins Guide](../07-plugins/) - For bundling agents with other features
- [Skills Guide](../03-skills/) - For auto-invoked capabilities
- [Memory Guide](../02-memory/) - For persistent context
- [Hooks Guide](../06-hooks/) - For event-driven automation
---
**Last Updated**: August 15, 2026
**Claude Code Version**: 2.1.233
**Sources**:
- https://code.claude.com/docs/en/sub-agents
- https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md
- https://code.claude.com/docs/en/cli-reference
- https://code.claude.com/docs/en/agent-teams
- https://code.claude.com/docs/en/changelog#2-1-172
- https://code.claude.com/docs/en/changelog
- https://github.com/anthropics/claude-code/releases/tag/v2.1.117
- https://github.com/anthropics/claude-code/releases/tag/v2.1.131
- https://github.com/anthropics/claude-code/releases/tag/v2.1.138
- https://github.com/anthropics/claude-code/releases/tag/v2.1.139
- https://github.com/anthropics/claude-code/releases/tag/v2.1.140
- https://code.claude.com/docs/en/model-config
**Compatible Models**: Claude Fable 5, Claude Opus 5, Claude Sonnet 5, Claude Sonnet 4.6, Claude Opus 4.8, Claude Haiku 4.5