← Back to daily report
+242 lines added
-247 lines removed
# SCreate custom subagents¶
¶
> Create and use specialized AI subagents in Claude Code for task-specific workflows and improved context management.¶
¶
Custom subagents in Claude Code are specialized AI assistants that can be invoked to handle specific types of tasks. They enable more efficient problem-solving by providing task-specific configurations with customized system prompts, tools and a separate context window.¶
¶
## What are subagents?¶
¶
Subagents are pre-configured AI personalities that Claude Code can delegate tasks to. Each subagent:¶
¶
* Has a specific purpose and expertise area¶
* Uses its own context window separate from the main conversation¶
* Can be configured with specific tools it's allowed to use¶
* Includes a custom system prompt that guides its behavior¶
¶
When Claude Code encounters a task that matches a subagent's expertise, it can delegate that task to the specialized subagent, which works independently and returns results.¶
¶
## Key benefits¶
¶
<CardGroup cols={2}>¶
<Card title="Context preservation" icon="layer-group">¶
Each subagent operates in its own context, preventing pollution of the main conversation and keeping it focused on high-level objectives.¶
</Card>¶
¶
<Card title="Specialized expertise" icon="brain">¶
Subagents can be fine-tuned with detailed instructions for specific domains, leading to higher success rates on designated tasks.¶
</Card>¶
¶
<Card title="Reusability" icon="rotate">¶
Once created, you can use subagents across different projects and share them with your team for consistent workflows.¶
</Card>¶
¶
<Card title="Flexible permissions" icon="shield-check">¶
Each subagent can have different tool access levels, allowing you to limit powerful tools to specific subagent types.¶
</Card>¶
</CardGroup>¶
¶
## Quick start¶
¶
To create your first subagent:Subagents are specialized AI assistants that handle specific types of tasks. Each subagent runs in its own context window with a custom system prompt, specific tool access, and independent permissions. When Claude encounters a task that matches a subagent's description, it delegates to that subagent, which works independently and returns results.¶
¶
Subagents help you:¶
¶
* **Preserve context** by keeping exploration and implementation out of your main conversation¶
* **Enforce constraints** by limiting which tools a subagent can use¶
* **Reuse configurations** across projects with user-level subagents¶
* **Specialize behavior** with focused system prompts for specific domains¶
* **Control costs** by routing tasks to faster, cheaper models like Haiku¶
¶
Claude uses each subagent's description to decide when to delegate tasks. When you create a subagent, write a clear description so Claude knows when to use it.¶
¶
Claude Code includes several built-in subagents like **Explore**, **Plan**, and **general-purpose**. You can also create custom subagents to handle specific tasks. This page covers the [built-in subagents](#built-in-subagents), [how to create your own](#quickstart-create-your-first-subagent), [full configuration options](#configure-subagents), [patterns for working with subagents](#work-with-subagents), and [example subagents](#example-subagents).¶
¶
## Built-in subagents¶
¶
Claude Code includes built-in subagents that Claude automatically uses when appropriate. Each inherits the parent conversation's permissions with additional tool restrictions.¶
¶
<Tabs>¶
<Tab title="Explore">¶
A fast, read-only agent optimized for searching and analyzing codebases.¶
¶
* **Model**: Haiku (fast, low-latency)¶
* **Tools**: Read-only tools (denied access to Write and Edit tools)¶
* **Purpose**: File discovery, code search, codebase exploration¶
¶
Claude delegates to Explore when it needs to search or understand a codebase without making changes. This keeps exploration results out of your main conversation context.¶
¶
When invoking Explore, Claude specifies a thoroughness level: **quick** for targeted lookups, **medium** for balanced exploration, or **very thorough** for comprehensive analysis.¶
</Tab>¶
¶
<Tab title="Plan">¶
A research agent used during [plan mode](/en/common-workflows#use-plan-mode-for-safe-code-analysis) to gather context before presenting a plan.¶
¶
* **Model**: Inherits from main conversation¶
* **Tools**: Read-only tools (denied access to Write and Edit tools)¶
* **Purpose**: Codebase research for planning¶
¶
When you're in plan mode and Claude needs to understand your codebase, it delegates research to the Plan subagent. This prevents infinite nesting (subagents cannot spawn other subagents) while still gathering necessary context.¶
</Tab>¶
¶
<Tab title="General-purpose">¶
A capable agent for complex, multi-step tasks that require both exploration and action.¶
¶
* **Model**: Inherits from main conversation¶
* **Tools**: All tools¶
* **Purpose**: Complex research, multi-step operations, code modifications¶
¶
Claude delegates to general-purpose when the task requires both exploration and modification, complex reasoning to interpret results, or multiple dependent steps.¶
</Tab>¶
</Tabs>¶
¶
Beyond these built-in subagents, you can create your own with custom prompts, tool restrictions, permission modes, hooks, and skills. The following sections show how to get started and customize subagents.¶
¶
## Quickstart: create your first subagent¶
¶
Subagents are defined in Markdown files with YAML frontmatter. You can [create them manually](#write-subagent-files) or use the `/agents` slash command.¶
¶
This walkthrough guides you through creating a user-level subagent with the `/agent` command. The subagent reviews code and suggests improvements for the codebase.¶
¶
<Steps>¶
<Step title="Open the subagents interface">¶
Run the following commandIn Claude Code, run:¶
¶
```¶
/agents¶
```¶
</Step>¶
¶
<Step title="Select 'Create New Agent'">¶
Choose whether to create a project-level or user-level subagent¶
</Step>¶
¶
<Step title="Define the subagent">¶
* **Recommended**: generate with Claude first, then customize to make it yours¶
* Describe your subagent in detail, including when Claude should use it¶
* Select the tools you want to grant access to, or leave this blank to inherit all tools¶
* The interface shows all available tools¶
* If you're generating with Claude, you can also edit the system prompt in your own editor by pressing `e`¶
</Step>¶
¶
<Step title="Save and use">¶
Your subagent is now available. Claude uses it automatically when appropriate, or you can invoke it explicitly:¶
¶
```¶
> Use the code-reviewer subagent to check my recent changes¶
```¶
</Step>¶
</Steps>¶
¶
## Subagent configuration¶
¶
### File locations¶
¶
Subagents are stored as Markdown files with YAML frontmatter in two possible locations:¶
¶
| Type | Location | Scope | Priority |¶
| :-------------------- | :------------------ | :---------------------------- | :------- |¶
| **Project subagents** | `.claude/agents/` | Available in current project | Highest |¶
| **User subagents** | `~/.claude/agents/` | Available across all projects | Lower |¶
¶
When subagent names conflict, project-level subagents take precedence over user-level subagents.¶
¶
### Plugin agents¶
¶
[Plugins](/en/plugins) can provide custom subagents that integrate seamlessly with Claude Code. Plugin agents work identically to user-defined agents and appear in the `/agents` interface.¶
¶
**Plugin agent locations**: plugins include agents in their `agents/` directory (or custom paths specified in the plugin manifest).¶
¶
**Using plugin agents**:¶
¶
* Plugin agents appear in `/agents` alongside your custom agents¶
* Can be invoked explicitly: "Use the code-reviewer agent from the security-plugin"¶
* Can be invoked automatically by Claude when appropriate¶
* Can be managed (viewed, inspected) through `/agents` interface¶
¶
See the [plugin components reference](/en/plugins-reference#agents) for details on creating plugin agents.¶
¶
### CLI-based configuration¶
¶
You can also define subagents dynamically using the `--agents` CLI flag, which accepts a JSON object:¶
¶
```bash theme={null}¶
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"¶
}¶
}'¶
```¶
¶
**Priority**: CLI-defined subagents have lower priority than project-level subagents but higher priority than user-level subagents.¶
¶
**Use case**: This approach is useful for:¶
¶
* Quick testing of subagent configurations¶
* Session-specific subagents that don't need to be saved¶
* Automation scripts that need custom subagents¶
* Sharing subagent definitions in documentation or scripts¶
¶
For detailed information about the JSON format and all available options, see the [CLI reference documentation](/en/cli-reference#agents-flag-format).¶
¶
### File format¶
¶
Each subagent is defined in a Markdown file with this structure:¶
¶
```markdown theme={null}¶
---¶
name: your-sub-agent-name¶
description: Description of when this subagent should be invoked¶
tools: tool1, tool2, tool3 # Optional - inherits all tools if omitted¶
model: sonnet # Optional - specify model alias or 'inherit'¶
permissionMode: default # Optional - permission mode for the subagent¶
skills: skill1, skill2 # Optional - skills to auto-load¶
---¶
¶
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.¶
¶
Include specific instructions, best practices, and any constraints¶
the subagent should follow.¶
```¶
¶
#### Configuration fields¶
¶
| Field | Required | Description |¶
| :--------------- | :------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |¶
| `name` | Yes | Unique identifier using lowercase letters and hyphens |¶
| `description` | Yes | Natural language description of the subagent's purpose |¶
| `tools` | No | Comma-separated list of specific tools. If omitted, inherits all tools from the main thread |¶
| `model` | No | Model to use for this subagent. Can be a model alias (`sonnet`, `opus`, `haiku`) or `'inherit'` to use the main conversation's model. If omitted, defaults to the [configured subagent model](/en/model-config) |¶
| `permissionMode` | No | Permission mode for the subagent. Valid values: `default`, `acceptEdits`, `dontAsk`, `bypassPermissions`, `plan`, `ignore`. Controls how the subagent handles permission requests |¶
| `skills` | No | Comma-separated list of skill names to auto-load when the subagent starts. Subagents do not inherit Skills from the parent conversation. If omitted, no Skills are preloaded. |¶
| `hooks` | No | Define hooks scoped to this subagent's lifecycle. Supports `PreToolUse`, `PostToolUse`, and `Stop` events. See [Define hooks for subagents](#define-hooks-for-subagents). |¶
¶
### Model selection¶
¶
The `model` field allows you to control which [AI model](/en/model-config) the subagent uses:¶
¶
* **Model alias**: Use one of the available aliases: `sonnet`, `opus`, or `haiku`¶
* **`'inherit'`**: Use the same model as the main conversation (useful for consistency)¶
* **Omitted**: If not specified, uses the default model configured for subagents (`sonnet`)¶
¶
<Note>¶
Using `'inherit'` is particularly useful when you want your subagents to adapt to the model choice of the main conversation, ensuring consistent capabilities and response style throughout your session.¶
</Note>¶
¶
### Available tools¶
¶
Subagents can be granted access to any of Claude Code's internal tools. See the [tools documentation](/en/settings#tools-available-to-claude) for a complete list of available tools.¶
¶
<Tip>¶
**Recommended:** Use the `/agents` command to modify tool access - it provides an interactive interface that lists all available tools, including any connected MCP server tools, making it easier to select the ones you need.¶
</Tip>¶
¶
You have two options for configuring tools:¶
¶
* **Omit the `tools` field** to inherit all tools from the main thread (default), including MCP tools¶
* **Specify individual tools** as a comma-separated list for more granular control (can be edited manually or via `/agents`)¶
¶
**MCP Tools**: Subagents can access MCP tools from configured MCP servers. When the `tools` field is omitted, subagents inherit all MCP tools available to the main thread.¶
¶
### Define hooks for subagents¶
¶
Subagents can define hooks that run during the subagent's lifecycle. Use the `hooks` field to specify `PreToolUse`, `PostToolUse`, or `Stop` handlers:¶
¶
```yaml theme={null}¶
---¶
name: code-reviewer¶
description: Review code changes with automatic linting¶
hooks:¶
PostToolUse:¶
- matcher: "Edit|Write"¶
hooks:¶
- type: command¶
command: "./scripts/run-linter.sh"¶
---¶
```¶
¶
Hooks defined in a subagent are scoped to that subagent's execution and are automatically cleaned up when the subagent finishes.¶
¶
See [Hooks](/en/hooks) for the complete hook configuration format.¶
¶
## Managing subagents¶
¶
### Using the /agents command (Recommended)¶
¶
The `/agents` command provides a comprehensive interface for subagent management:¶
¶
```¶
/agents¶
```¶
¶
This opens an interactive menu where you can:¶
¶
* View all available subagents (built-in, user, and project)¶
* Create new subagents with guided setup¶
* Edit existing custom subagents, including their tool access¶
* Delete custom subagents¶
* See which subagents are active when duplicates exist¶
* **Manage tool permissions** with a complete list of available tools¶
¶
### Direct file management¶
¶
You can also manage subagents by working directly with their files:¶
¶
```bash theme={null}¶
# Create a project subagent¶
mkdir -p .claude/agents¶
echo '---¶
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.' > .claude/agents/test-runner.md¶
¶
# Create a user subagent¶
mkdir -p ~/.claude/agents¶
# ... create subagent file¶
```¶
¶
<Note>¶
Subagents created by manually adding files will be loaded the next time you start a Claude Code session. To create and use a subagent immediately without restarting, use the `/agents` command instead.¶
</Note>¶
¶
### Disabling specific subagents¶
¶
You can disable specific built-in or custom subagents using the `Task(AgentName)` permission rule syntax. Add these rules to the `deny` array in your [settings](/en/settings#permission-settings) or use the `--disallowedTools` CLI flag.¶
¶
**Example settings.json configuration:**¶
¶
```json theme={null}¶
{¶
"permissions": {¶
"deny": ["Task(Explore)", "Task(Plan)"]¶
}¶
}¶
```¶
¶
**Example CLI usage:**¶
¶
```bash theme={null}¶
claude --disallowedTools "Task(Explore)"¶
```¶
¶
This is useful when you want to prevent Claude from delegating tasks to specific subagents, either for security reasons or to enforce a particular workflow.¶
¶
See [IAM documentation](/en/iam#tool-specific-permission-rules) for more details on permission rules.¶
¶
## Using subagents effectively¶
¶
### Automatic delegation¶
¶
Claude Code proactively delegates tasks based on:¶
¶
* The task description in your request¶
* The `description` field in subagent configurations¶
* Current context and available tools¶
¶
<Tip>¶
To encourage more proactive subagent use, include phrases like "use PROACTIVELY" or "MUST BE USED" in your `description` field.¶
</Tip>¶
¶
### Explicit invocation¶
¶
Request a specific subagent by mentioning it in your command:¶
¶
```¶
> 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¶
```¶
¶
## Built-in subagents¶
¶
Claude Code includes built-in subagents that are available out of the box:¶
¶
### General-purpose subagent¶
¶
The general-purpose subagent is a capable agent for complex, multi-step tasks that require both exploration and action. Unlike the Explore subagent, it can modify files and execute a wider range of operations.¶
¶
**Key characteristics:**¶
¶
* **Model**: Uses Sonnet for more capable reasoning¶
* **Tools**: Has access to all tools¶
* **Mode**: Can read and write files, execute commands, make changes¶
* **Purpose**: Complex research tasks, multi-step operations, code modifications¶
¶
**When Claude uses it:**¶
¶
Claude delegates to the general-purpose subagent when:¶
¶
* The task requires both exploration and modification¶
* Complex reasoning is needed to interpret search results¶
* Multiple strategies may be needed if initial searches fail¶
* The task has multiple steps that depend on each other¶
¶
**Example scenario:**¶
¶
```¶
User: Find all the places where we handle authentication and update them to use the new token format¶
¶
Claude: [Invokes general-purpose subagent]¶
[Agent searches for auth-related code across codebase]¶
[Agent reads and analyzes multiple files]¶
[Agent makes necessary edits]¶
[Returns detailed writeup of changes made]¶
```¶
¶
### Plan subagent¶
¶
The Plan subagent is a specialized built-in agent designed for use during plan mode. When Claude is operating in plan mode (non-execution mode), it uses the Plan subagent to conduct research and gather information about your codebase before presenting a plan.¶
¶
**Key characteristics:**¶
¶
* **Model**: Uses Sonnet for more capable analysis¶
* **Tools**: Has access to Read, Glob, Grep, and Bash tools for codebase exploration¶
* **Purpose**: Searches files, analyzes code structure, and gathers context¶
* **Automatic invocation**: Claude automatically uses this agent when in plan mode and needs to research the codebase¶
¶
**How it works:**¶
When you're in plan mode and Claude needs to understand your codebase to create a plan, it delegates research tasks to the Plan subagent. This prevents infinite nesting of agents (subagents cannot spawn other subagents) while still allowing Claude to gather the necessary context.¶
¶
**Example scenario:**¶
¶
```¶
User: [In plan mode] Help me refactor the authentication module¶
¶
Claude: Let me research your authentication implementation first...¶
[Internally invokes Plan subagent to explore auth-related files]¶
[Plan subagent searches codebase and returns findings]¶
Claude: Based on my research, here's my proposed plan...¶
```¶
¶
<Tip>¶
The Plan subagent is only used in plan mode. In normal execution mode, Claude uses the general-purpose agent or other custom subagents you've created.¶
</Tip>¶
¶
### Explore subagent¶
¶
The Explore subagent is a fast, lightweight agent optimized for searching and analyzing codebases. It operates in strict read-only mode and is designed for rapid file discovery and code exploration.¶
¶
**Key characteristics:**¶
¶
* **Model**: Uses Haiku for fast, low-latency searches¶
* **Mode**: Strictly read-only - cannot create, modify, or delete files¶
* **Tools available**:¶
* Glob - File pattern matching¶
* Grep - Content searching with regular expressions¶
* Read - Reading file contents¶
* Bash - Read-only commands only (ls, git status, git log, git diff, find, cat, head, tail)¶
¶
**When Claude uses it:**¶
¶
Claude will delegate to the Explore subagent when it needs to search or understand a codebase but doesn't need to make changes. This is more efficient than the main agent running multiple search commands directly, as content found during the exploration process doesn't bloat the main conversation.¶
¶
**Thoroughness levels:**¶
¶
When invoking the Explore subagent, Claude specifies a thoroughness level:¶
¶
* **Quick** - Fast searches with minimal exploration. Good for targeted lookups.¶
* **Medium** - Moderate exploration. Balances speed and thoroughness.¶
* **Very thorough** - Comprehensive analysis across multiple locations and naming conventions. Used when the target might be in unexpected places.¶
¶
**Example scenarios:**¶
¶
```¶
User: Where are errors from the client handled?¶
¶
Claude: [Invokes Explore subagent with "medium" thoroughness]¶
[Explore uses Grep to search for error handling patterns]¶
[Explore uses Read to examine promising files]¶
[Returns findings with absolute file paths]¶
Claude: Client errors are handled in src/services/process.ts:712...¶
```¶
¶
```¶
User: What's the codebase structure?¶
¶
Claude: [Invokes Explore subagent with "quick" thoroughness]¶
[Explore uses Glob and ls to map directory structure]¶
[Returns overview of key directories and their purposes]¶
```¶
¶
## Example subagents¶
¶
### Code reviewerCreate a new user-level agent">¶
Select **Create new agent**, then choose **User-level**. This saves the subagent to `~/.claude/agents/` so it's available in all your projects.¶
</Step>¶
¶
<Step title="Generate with Claude">¶
Select **Generate with Claude**. When prompted, describe the subagent:¶
¶
```¶
A code improvement agent that scans files and suggests improvements¶
for readability, performance, and best practices. It should explain¶
each issue, show the current code, and provide an improved version.¶
```¶
¶
Claude generates the system prompt and configuration. Press `e` to open it in your editor if you want to customize it.¶
</Step>¶
¶
<Step title="Select tools">¶
For a read-only reviewer, deselect everything except **Read-only tools**. If you keep all tools selected, the subagent inherits all tools available to the main conversation.¶
</Step>¶
¶
<Step title="Select model">¶
Choose which model the subagent uses. For this example agent, select **Sonnet**, which balances capability and speed for analyzing code patterns.¶
</Step>¶
¶
<Step title="Choose a color">¶
Pick a background color for the subagent. This helps you identify which subagent is running in the UI.¶
</Step>¶
¶
<Step title="Save and try it out">¶
Save the subagent. It's available immediately (no restart needed). Try it:¶
¶
```¶
Use the code-improver agent to suggest improvements in this project¶
```¶
¶
Claude delegates to your new subagent, which scans the codebase and returns improvement suggestions.¶
</Step>¶
</Steps>¶
¶
You now have a subagent you can use in any project on your machine to analyze codebases and suggest improvements.¶
¶
You can also create subagents manually as Markdown files, define them via CLI flags, or distribute them through plugins. The following sections cover all configuration options.¶
¶
## Configure subagents¶
¶
### Use the /agents command¶
¶
The `/agents` command provides an interactive interface for managing subagents. Run `/agents` to:¶
¶
* View all available subagents (built-in, user, project, and plugin)¶
* Create new subagents with guided setup or Claude generation¶
* Edit existing subagent configuration and tool access¶
* Delete custom subagents¶
* See which subagents are active when duplicates exist¶
¶
This is the recommended way to create and manage subagents. For manual creation or automation, you can also add subagent files directly.¶
¶
### Choose the subagent scope¶
¶
Subagents are Markdown files with YAML frontmatter. Store them in different locations depending on scope. When multiple subagents share the same name, the higher-priority location wins.¶
¶
| Location | Scope | Priority | How to create |¶
| :--------------------------- | :---------------------- | :---------- | :------------------------------------ |¶
| `--agents` CLI flag | Current session | 1 (highest) | Pass JSON when launching Claude Code |¶
| `.claude/agents/` | Current project | 2 | Interactive or manual |¶
| `~/.claude/agents/` | All your projects | 3 | Interactive or manual |¶
| Plugin's `agents/` directory | Where plugin is enabled | 4 (lowest) | Installed with [plugins](/en/plugins) |¶
¶
**Project subagents** (`.claude/agents/`) are ideal for subagents specific to a codebase. Check them into version control so your team can use and improve them collaboratively.¶
¶
**User subagents** (`~/.claude/agents/`) are personal subagents available in all your projects.¶
¶
**CLI-defined subagents** are passed as JSON when launching Claude Code. They exist only for that session and aren't saved to disk, making them useful for quick testing or automation scripts:¶
¶
```bash theme={null}¶
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"¶
}¶
}'¶
```¶
¶
The `--agents` flag accepts JSON with the same fields as [frontmatter](#supported-frontmatter-fields). Use `prompt` for the system prompt (equivalent to the markdown body in file-based subagents). See the [CLI reference](/en/cli-reference#agents-flag-format) for the full JSON format.¶
¶
**Plugin subagents** come from [plugins](/en/plugins) you've installed. They appear in `/agents` alongside your custom subagents. See the [plugin components reference](/en/plugins-reference#agents) for details on creating plugin subagents.¶
¶
### Write subagent files¶
¶
Subagent files use YAML frontmatter for configuration, followed by the system prompt in Markdown:¶
¶
<Note>¶
Subagents are loaded at session start. If you create a subagent by manually adding a file, restart your session or use `/agents` to load it immediately.¶
</Note>¶
¶
```markdown theme={null}¶
---¶
name: code-reviewer¶
description: Reviews code for quality and best practices¶
tools: Read, Glob, Grep¶
model: sonnet¶
---¶
¶
You are a code reviewer. When invoked, analyze the code and provide¶
specific, actionable feedback on quality, security, and best practices.¶
```¶
¶
The frontmatter defines the subagent's metadata and configuration. The body becomes the system prompt that guides the subagent's behavior. Subagents receive only this system prompt (plus basic environment details like working directory), not the full Claude Code system prompt.¶
¶
#### Supported frontmatter fields¶
¶
The following fields can be used in the YAML frontmatter. Only `name` and `description` are required.¶
¶
| Field | Required | Description |¶
| :---------------- | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |¶
| `name` | Yes | Unique identifier using lowercase letters and hyphens |¶
| `description` | Yes | When Claude should delegate to this subagent |¶
| `tools` | No | [Tools](#available-tools) the subagent can use. Inherits all tools if omitted |¶
| `disallowedTools` | No | Tools to deny, removed from inherited or specified list |¶
| `model` | No | [Model](#choose-a-model) to use: `sonnet`, `opus`, `haiku`, or `inherit`. Defaults to `sonnet` |¶
| `permissionMode` | No | [Permission mode](#permission-modes): `default`, `acceptEdits`, `dontAsk`, `bypassPermissions`, or `plan` |¶
| `skills` | No | [Skills](/en/skills) to load into the subagent's context at startup. The full skill content is injected, not just made available for invocation. Subagents don't inherit skills from the parent conversation |¶
| `hooks` | No | [Lifecycle hooks](#define-hooks-for-subagents) scoped to this subagent |¶
¶
### Choose a model¶
¶
The `model` field controls which [AI model](/en/model-config) the subagent uses:¶
¶
* **Model alias**: Use one of the available aliases: `sonnet`, `opus`, or `haiku`¶
* **inherit**: Use the same model as the main conversation (useful for consistency)¶
* **Omitted**: If not specified, uses the default model configured for subagents (`sonnet`)¶
¶
### Control subagent capabilities¶
¶
You can control what subagents can do through tool access, permission modes, and conditional rules.¶
¶
#### Available tools¶
¶
Subagents can use any of Claude Code's [internal tools](/en/settings#tools-available-to-claude). By default, subagents inherit all tools from the main conversation, including MCP tools.¶
¶
To restrict tools, use the `tools` field (allowlist) or `disallowedTools` field (denylist):¶
¶
```yaml theme={null}¶
---¶
name: safe-researcher¶
description: Research agent with restricted capabilities¶
tools: Read, Grep, Glob, Bash¶
disallowedTools: Write, Edit¶
---¶
```¶
¶
#### Permission modes¶
¶
The `permissionMode` field controls how the subagent handles permission prompts. Subagents inherit the permission context from the main conversation but can override the mode.¶
¶
| Mode | Behavior |¶
| :------------------ | :----------------------------------------------------------------- |¶
| `default` | Standard permission checking with prompts |¶
| `acceptEdits` | Auto-accept file edits |¶
| `dontAsk` | Auto-deny permission prompts (explicitly allowed tools still work) |¶
| `bypassPermissions` | Skip all permission checks |¶
| `plan` | Plan mode (read-only exploration) |¶
¶
<Warning>¶
Use `bypassPermissions` with caution. It skips all permission checks, allowing the subagent to execute any operation without approval.¶
</Warning>¶
¶
If the parent uses `bypassPermissions`, this takes precedence and cannot be overridden.¶
¶
#### Conditional rules with hooks¶
¶
For more dynamic control over tool usage, use `PreToolUse` hooks to validate operations before they execute. This is useful when you need to allow some operations of a tool while blocking others.¶
¶
This example creates a subagent that only allows read-only database queries by validating commands before execution:¶
¶
```yaml theme={null}¶
---¶
name: db-reader¶
description: Execute read-only database queries¶
tools: Bash¶
hooks:¶
PreToolUse:¶
- matcher: "Bash"¶
hooks:¶
- type: command¶
command: "./scripts/validate-readonly-query.sh"¶
---¶
```¶
¶
The validation script inspects `$TOOL_INPUT` and exits with a non-zero code to block write operations. See [Define hooks for subagents](#define-hooks-for-subagents) for more hook configuration options.¶
¶
#### Disable specific subagents¶
¶
You can prevent Claude from using specific subagents by adding them to the `deny` array in your [settings](/en/settings#permission-settings). Use the format `Task(subagent-name)` where `subagent-name` matches the subagent's name field.¶
¶
```json theme={null}¶
{¶
"permissions": {¶
"deny": ["Task(Explore)", "Task(my-custom-agent)"]¶
}¶
}¶
```¶
¶
This works for both built-in and custom subagents. You can also use the `--disallowedTools` CLI flag:¶
¶
```bash theme={null}¶
claude --disallowedTools "Task(Explore)"¶
```¶
¶
See [IAM documentation](/en/iam#tool-specific-permission-rules) for more details on permission rules.¶
¶
### Define hooks for subagents¶
¶
Subagents can define [hooks](/en/hooks) that run during the subagent's lifecycle. There are two ways to configure hooks:¶
¶
1. **In the subagent's frontmatter**: Define hooks that run only while that subagent is active¶
2. **In `settings.json`**: Define hooks that run in the main session when subagents start or stop¶
¶
#### Hooks in subagent frontmatter¶
¶
Define hooks directly in the subagent's markdown file. These hooks only run while that specific subagent is active and are cleaned up when it finishes.¶
¶
| Event | Matcher input | When it fires |¶
| :------------ | :------------ | :------------------------------ |¶
| `PreToolUse` | Tool name | Before the subagent uses a tool |¶
| `PostToolUse` | Tool name | After the subagent uses a tool |¶
| `Stop` | (none) | When the subagent finishes |¶
¶
This example validates Bash commands with the `PreToolUse` hook and runs a linter after file edits with `PostToolUse`:¶
¶
```yaml theme={null}¶
---¶
name: code-reviewer¶
description: Review code changes with automatic linting¶
hooks:¶
PreToolUse:¶
- matcher: "Bash"¶
hooks:¶
- type: command¶
command: "./scripts/validate-command.sh $TOOL_INPUT"¶
PostToolUse:¶
- matcher: "Edit|Write"¶
hooks:¶
- type: command¶
command: "./scripts/run-linter.sh"¶
---¶
```¶
¶
`Stop` hooks in frontmatter are automatically converted to `SubagentStop` events.¶
¶
#### Project-level hooks for subagent events¶
¶
Configure hooks in `settings.json` that respond to subagent lifecycle events in the main session. Use the `matcher` field to target specific agent types by name.¶
¶
| Event | Matcher input | When it fires |¶
| :-------------- | :-------------- | :------------------------------- |¶
| `SubagentStart` | Agent type name | When a subagent begins execution |¶
| `SubagentStop` | Agent type name | When a subagent completes |¶
¶
This example runs setup and cleanup scripts only when the `db-agent` subagent starts and stops:¶
¶
```json theme={null}¶
{¶
"hooks": {¶
"SubagentStart": [¶
{¶
"matcher": "db-agent",¶
"hooks": [¶
{ "type": "command", "command": "./scripts/setup-db-connection.sh" }¶
]¶
}¶
],¶
"SubagentStop": [¶
{¶
"matcher": "db-agent",¶
"hooks": [¶
{ "type": "command", "command": "./scripts/cleanup-db-connection.sh" }¶
]¶
}¶
]¶
}¶
}¶
```¶
¶
See [Hooks](/en/hooks) for the complete hook configuration format.¶
¶
## Work with subagents¶
¶
### Understand automatic delegation¶
¶
Claude automatically delegates tasks based on the task description in your request, the `description` field in subagent configurations, and current context. To encourage proactive delegation, include phrases like "use proactively" in your subagent's description field.¶
¶
You can also request a specific subagent explicitly:¶
¶
```¶
Use the test-runner subagent to fix failing tests¶
Have the code-reviewer subagent look at my recent changes¶
```¶
¶
### Run subagents in foreground or background¶
¶
Subagents can run in the foreground (blocking) or background (concurrent):¶
¶
* **Foreground subagents** block the main conversation until complete. Permission prompts and clarifying questions (like [`AskUserQuestion`](/en/settings#tools-available-to-claude)) are passed through to you.¶
* **Background subagents** run concurrently while you continue working. They inherit the parent's permissions and auto-deny anything not pre-approved. If a background subagent needs a permission it doesn't have or needs to ask clarifying questions, that tool call fails but the subagent continues. MCP tools are not available in background subagents.¶
¶
If a background subagent fails due to missing permissions, you can [resume it](#resume-subagents) in the foreground to retry with interactive prompts.¶
¶
Claude decides whether to run subagents in the foreground or background based on the task. You can also:¶
¶
* Ask Claude to "run this in the background"¶
* Press **Ctrl+B** to background a running task¶
¶
### Common patterns¶
¶
#### Isolate high-volume operations¶
¶
One of the most effective uses for subagents is isolating operations that produce large amounts of output. Running tests, fetching documentation, or processing log files can consume significant context. By delegating these to a subagent, the verbose output stays in the subagent's context while only the relevant summary returns to your main conversation.¶
¶
```¶
Use a subagent to run the test suite and report only the failing tests with their error messages¶
```¶
¶
#### Run parallel research¶
¶
For independent investigations, spawn multiple subagents to work simultaneously:¶
¶
```¶
Research the authentication, database, and API modules in parallel using separate subagents¶
```¶
¶
Each subagent explores its area independently, then Claude synthesizes the findings. This works best when the research paths don't depend on each other.¶
¶
<Warning>¶
When subagents complete, their results return to your main conversation. Running many subagents that each return detailed results can consume significant context.¶
</Warning>¶
¶
#### Chain subagents¶
¶
For multi-step workflows, ask Claude to use subagents in sequence. Each subagent completes its task and returns results to Claude, which then passes relevant context to the next subagent.¶
¶
```¶
Use the code-reviewer subagent to find performance issues, then use the optimizer subagent to fix them¶
```¶
¶
### Choose between subagents and main conversation¶
¶
Use the **main conversation** when:¶
¶
* The task needs frequent back-and-forth or iterative refinement¶
* Multiple phases share significant context (planning → implementation → testing)¶
* You're making a quick, targeted change¶
* Latency matters. Subagents start fresh and may need time to gather context¶
¶
Use **subagents** when:¶
¶
* The task produces verbose output you don't need in your main context¶
* You want to enforce specific tool restrictions or permissions¶
* The work is self-contained and can return a summary¶
¶
Consider [Skills](/en/skills) instead when you want reusable prompts or workflows that run in the main conversation context rather than isolated subagent context.¶
¶
<Note>¶
Subagents cannot spawn other subagents. If your workflow requires nested delegation, use [Skills](/en/skills) or [chain subagents](#chain-subagents) from the main conversation.¶
</Note>¶
¶
### Manage subagent context¶
¶
#### Resume subagents¶
¶
Each subagent invocation creates a new instance with fresh context. To continue an existing subagent's work instead of starting over, ask Claude to resume it.¶
¶
Resumed subagents retain their full conversation history, including all previous tool calls, results, and reasoning. The subagent picks up exactly where it stopped rather than starting fresh.¶
¶
When a subagent completes, Claude receives its agent ID. To resume a subagent, ask Claude to continue the previous work:¶
¶
```¶
Use the code-reviewer subagent to review the authentication module¶
[Agent completes]¶
¶
Continue that code review and now analyze the authorization logic¶
[Claude resumes the subagent with full context from previous conversation]¶
```¶
¶
You can also ask Claude for the agent ID if you want to reference it explicitly, or find IDs in the transcript files at `~/.claude/projects/{project}/{sessionId}/subagents/`. Each transcript is stored as `agent-{agentId}.jsonl`.¶
¶
For programmatic usage, see [Subagents in the Agent SDK](/en/agent-sdk/subagents).¶
¶
Subagent transcripts persist independently of the main conversation:¶
¶
* **Main conversation compaction**: When the main conversation compacts, subagent transcripts are unaffected. They're stored in separate files.¶
* **Session persistence**: Subagent transcripts persist within their session. You can [resume a subagent](#resume-subagents) after restarting Claude Code by resuming the same session.¶
* **Automatic cleanup**: Transcripts are cleaned up based on the `cleanupPeriodDays` setting (default: 30 days).¶
¶
#### Auto-compaction¶
¶
Subagents support automatic compaction using the same logic as the main conversation. When a subagent's context approaches its limit, Claude Code summarizes older messages to free up space while preserving important context.¶
¶
Compaction events are logged in subagent transcript files:¶
¶
```json theme={null}¶
{¶
"type": "system",¶
"subtype": "compact_boundary",¶
"compactMetadata": {¶
"trigger": "auto",¶
"preTokens": 167189¶
}¶
}¶
```¶
¶
The `preTokens` value shows how many tokens were used before compaction occurred.¶
¶
## Example subagents¶
¶
These examples demonstrate effective patterns for building subagents. Use them as starting points, or generate a customized version with Claude.¶
¶
<Tip>¶
**Best practices:**¶
¶
* **Design focused subagents:** each subagent should excel at one specific task¶
* **Write detailed descriptions:** Claude uses the description to decide when to delegate¶
* **Limit tool access:** grant only necessary permissions for security and focus¶
* **Check into version control:** share project subagents with your team¶
</Tip>¶
¶
### Code reviewer¶
¶
A read-only subagent that reviews code without modifying it. This example shows how to design a focused subagent with limited tool access (no Edit or Write) and a detailed prompt that specifies exactly what to look for and how to format output.¶
¶
```markdown theme={null}¶
---¶
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.¶
```¶
¶
### Debugger¶
¶
A subagent that can both analyze and fix issues. Unlike the code reviewer, this one includes Edit because fixing bugs requires modifying code. The prompt provides a clear workflow from diagnosis to verification.¶
¶
```markdown theme={null}¶
---¶
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.¶
```¶
¶
### Data scientist¶
¶
A domain-specific subagent for data analysis work. This example shows how to create subagents for specialized workflows outside of typical coding tasks. It explicitly sets `model: sonnet` for more capable analysis.¶
¶
```markdown theme={null}¶
---¶
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¶
¶
For each analysis:¶
- Explain the query approach¶
- Document any assumptions¶
- Highlight key findings¶
- Suggest next steps based on data¶
¶
Always ensure queries are efficient and cost-effective.¶
```¶
¶
## Best practices¶
¶
* **Start with Claude-generated agents**: We highly recommend generating your initial subagent with Claude and then iterating on it to make it personally yours. This approach gives you the best results - a solid foundation that you can customize to your specific needs.¶
¶
* **Design focused subagents**: Create subagents with single, clear responsibilities rather than trying to make one subagent do everything. This improves performance and makes subagents more predictable.¶
¶
* **Write detailed prompts**: Include specific instructions, examples, and constraints in your system prompts. The more guidance you provide, the better the subagent will perform.¶
¶
* **Limit tool access**: Only grant tools that are necessary for the subagent's purpose. This improves security and helps the subagent focus on relevant actions.¶
¶
* **Version control**: Check project subagents into version control so your team can benefit from and improve them collaboratively.¶
¶
## Advanced usage¶
¶
### Chaining subagents¶
¶
For complex workflows, you can chain multiple subagents:¶
¶
```¶
> First use the code-analyzer subagent to find performance issues, then use the optimizer subagent to fix them¶
```¶
¶
### Dynamic subagent selection¶
¶
Claude Code intelligently selects subagents based on context. Make your `description` fields specific and action-oriented for best results.¶
¶
### Resumable subagents¶
¶
Subagents can be resumed to continue previous conversations, which is particularly useful for long-running research or analysis tasks that need to be continued across multiple invocations.¶
¶
**How it works:**¶
¶
* Each subagent execution is assigned a unique `agentId`¶
* The agent's conversation is stored in a separate transcript file: `agent-{agentId}.jsonl`¶
* You can resume a previous agent by providing its `agentId` via the `resume` parameter¶
* When resumed, the agent continues with full context from its previous conversation¶
¶
**Example workflow:**¶
¶
Initial invocation:¶
¶
```¶
> Use the code-analyzer agent to start reviewing the authentication module¶
¶
[Agent completes initial analysis and returns agentId: "abc123"]¶
```¶
¶
Resume the agent:¶
¶
```¶
> Resume agent abc123 and now analyze the authorization logic as well¶
¶
[Agent continues with full context from previous conversation]¶
```¶
¶
**Use cases:**¶
¶
* **Long-running research**: Break down large codebase analysis into multiple sessions¶
* **Iterative refinement**: Continue refining a subagent's work without losing context¶
* **Multi-step workflows**: Have a subagent work on related tasks sequentially while maintaining context¶
¶
**Technical details:**¶
¶
* Agent transcripts are stored in your project directory¶
* Recording is disabled during resume to avoid duplicating messages¶
* Both synchronous and asynchronous agents can be resumed¶
* The `resume` parameter accepts the agent ID from a previous execution¶
¶
**Programmatic usage:**¶
¶
If you're using the Agent SDK or interacting with the AgentTool directly, you can pass the `resume` parameter:¶
¶
```typescript theme={null}¶
{¶
"description": "Continue analysis",¶
"prompt": "Now examine the error handling patterns",¶
"subagent_type": "code-analyzer",¶
"resume": "abc123" // Agent ID from previous execution¶
}¶
```¶
¶
<Tip>¶
Keep track of agent IDs for tasks you may want to resume later. Claude Code displays the agent ID when a subagent completes its work.¶
</Tip>¶
¶
## Performance considerations¶
¶
* **Context efficiency**: Agents help preserve main context, enabling longer overall sessions¶
* **Latency**: Subagents start off with a clean slate each time they are invoked and may add latency as they gather context that they require to do their job effectively.¶
¶
## Related documentation¶
¶
* [Plugins](/en/plugins) - Extend Claude Code with custom agents through plugins¶
* [Slash commands](/en/slash-commands) - Learn about other built-in commands¶
* [Settings](/en/settings) - Configure Claude Code behavior¶
* [Hooks](/en/hooks) - Automate workflows with event handlersNext steps¶
¶
Now that you understand subagents, explore these related features:¶
¶
* [Distribute subagents with plugins](/en/plugins) to share subagents across teams or projects¶
* [Run Claude Code programmatically](/en/headless) with the Agent SDK for CI/CD and automation¶
* [Use MCP servers](/en/mcp) to give subagents access to external tools and data¶
¶
¶
---¶
¶
> To find navigation and other pages in this documentation, fetch the llms.txt file at: https://code.claude.com/docs/llms.txt
Unified Diff
--- a/sub-agents.md
+++ b/sub-agents.md
@@ -1,105 +1,149 @@
-# Subagents
+# Create custom subagents
> Create and use specialized AI subagents in Claude Code for task-specific workflows and improved context management.
-Custom subagents in Claude Code are specialized AI assistants that can be invoked to handle specific types of tasks. They enable more efficient problem-solving by providing task-specific configurations with customized system prompts, tools and a separate context window.
-
-## What are subagents?
-
-Subagents are pre-configured AI personalities that Claude Code can delegate tasks to. Each subagent:
-
-* Has a specific purpose and expertise area
-* Uses its own context window separate from the main conversation
-* Can be configured with specific tools it's allowed to use
-* Includes a custom system prompt that guides its behavior
-
-When Claude Code encounters a task that matches a subagent's expertise, it can delegate that task to the specialized subagent, which works independently and returns results.
-
-## Key benefits
-
-<CardGroup cols={2}>
- <Card title="Context preservation" icon="layer-group">
- Each subagent operates in its own context, preventing pollution of the main conversation and keeping it focused on high-level objectives.
- </Card>
-
- <Card title="Specialized expertise" icon="brain">
- Subagents can be fine-tuned with detailed instructions for specific domains, leading to higher success rates on designated tasks.
- </Card>
-
- <Card title="Reusability" icon="rotate">
- Once created, you can use subagents across different projects and share them with your team for consistent workflows.
- </Card>
-
- <Card title="Flexible permissions" icon="shield-check">
- Each subagent can have different tool access levels, allowing you to limit powerful tools to specific subagent types.
- </Card>
-</CardGroup>
-
-## Quick start
-
-To create your first subagent:
+Subagents are specialized AI assistants that handle specific types of tasks. Each subagent runs in its own context window with a custom system prompt, specific tool access, and independent permissions. When Claude encounters a task that matches a subagent's description, it delegates to that subagent, which works independently and returns results.
+
+Subagents help you:
+
+* **Preserve context** by keeping exploration and implementation out of your main conversation
+* **Enforce constraints** by limiting which tools a subagent can use
+* **Reuse configurations** across projects with user-level subagents
+* **Specialize behavior** with focused system prompts for specific domains
+* **Control costs** by routing tasks to faster, cheaper models like Haiku
+
+Claude uses each subagent's description to decide when to delegate tasks. When you create a subagent, write a clear description so Claude knows when to use it.
+
+Claude Code includes several built-in subagents like **Explore**, **Plan**, and **general-purpose**. You can also create custom subagents to handle specific tasks. This page covers the [built-in subagents](#built-in-subagents), [how to create your own](#quickstart-create-your-first-subagent), [full configuration options](#configure-subagents), [patterns for working with subagents](#work-with-subagents), and [example subagents](#example-subagents).
+
+## Built-in subagents
+
+Claude Code includes built-in subagents that Claude automatically uses when appropriate. Each inherits the parent conversation's permissions with additional tool restrictions.
+
+<Tabs>
+ <Tab title="Explore">
+ A fast, read-only agent optimized for searching and analyzing codebases.
+
+ * **Model**: Haiku (fast, low-latency)
+ * **Tools**: Read-only tools (denied access to Write and Edit tools)
+ * **Purpose**: File discovery, code search, codebase exploration
+
+ Claude delegates to Explore when it needs to search or understand a codebase without making changes. This keeps exploration results out of your main conversation context.
+
+ When invoking Explore, Claude specifies a thoroughness level: **quick** for targeted lookups, **medium** for balanced exploration, or **very thorough** for comprehensive analysis.
+ </Tab>
+
+ <Tab title="Plan">
+ A research agent used during [plan mode](/en/common-workflows#use-plan-mode-for-safe-code-analysis) to gather context before presenting a plan.
+
+ * **Model**: Inherits from main conversation
+ * **Tools**: Read-only tools (denied access to Write and Edit tools)
+ * **Purpose**: Codebase research for planning
+
+ When you're in plan mode and Claude needs to understand your codebase, it delegates research to the Plan subagent. This prevents infinite nesting (subagents cannot spawn other subagents) while still gathering necessary context.
+ </Tab>
+
+ <Tab title="General-purpose">
+ A capable agent for complex, multi-step tasks that require both exploration and action.
+
+ * **Model**: Inherits from main conversation
+ * **Tools**: All tools
+ * **Purpose**: Complex research, multi-step operations, code modifications
+
+ Claude delegates to general-purpose when the task requires both exploration and modification, complex reasoning to interpret results, or multiple dependent steps.
+ </Tab>
+</Tabs>
+
+Beyond these built-in subagents, you can create your own with custom prompts, tool restrictions, permission modes, hooks, and skills. The following sections show how to get started and customize subagents.
+
+## Quickstart: create your first subagent
+
+Subagents are defined in Markdown files with YAML frontmatter. You can [create them manually](#write-subagent-files) or use the `/agents` slash command.
+
+This walkthrough guides you through creating a user-level subagent with the `/agent` command. The subagent reviews code and suggests improvements for the codebase.
<Steps>
<Step title="Open the subagents interface">
- Run the following command:
+ In Claude Code, run:
```
/agents
```
</Step>
- <Step title="Select 'Create New Agent'">
- Choose whether to create a project-level or user-level subagent
+ <Step title="Create a new user-level agent">
+ Select **Create new agent**, then choose **User-level**. This saves the subagent to `~/.claude/agents/` so it's available in all your projects.
</Step>
- <Step title="Define the subagent">
- * **Recommended**: generate with Claude first, then customize to make it yours
- * Describe your subagent in detail, including when Claude should use it
- * Select the tools you want to grant access to, or leave this blank to inherit all tools
- * The interface shows all available tools
- * If you're generating with Claude, you can also edit the system prompt in your own editor by pressing `e`
+ <Step title="Generate with Claude">
+ Select **Generate with Claude**. When prompted, describe the subagent:
+
+ ```
+ A code improvement agent that scans files and suggests improvements
+ for readability, performance, and best practices. It should explain
+ each issue, show the current code, and provide an improved version.
+ ```
+
+ Claude generates the system prompt and configuration. Press `e` to open it in your editor if you want to customize it.
</Step>
- <Step title="Save and use">
- Your subagent is now available. Claude uses it automatically when appropriate, or you can invoke it explicitly:
+ <Step title="Select tools">
+ For a read-only reviewer, deselect everything except **Read-only tools**. If you keep all tools selected, the subagent inherits all tools available to the main conversation.
+ </Step>
+
+ <Step title="Select model">
+ Choose which model the subagent uses. For this example agent, select **Sonnet**, which balances capability and speed for analyzing code patterns.
+ </Step>
+
+ <Step title="Choose a color">
+ Pick a background color for the subagent. This helps you identify which subagent is running in the UI.
+ </Step>
+
+ <Step title="Save and try it out">
+ Save the subagent. It's available immediately (no restart needed). Try it:
```
- > Use the code-reviewer subagent to check my recent changes
+ Use the code-improver agent to suggest improvements in this project
```
+
+ Claude delegates to your new subagent, which scans the codebase and returns improvement suggestions.
</Step>
</Steps>
-## Subagent configuration
-
-### File locations
-
-Subagents are stored as Markdown files with YAML frontmatter in two possible locations:
-
-| Type | Location | Scope | Priority |
-| :-------------------- | :------------------ | :---------------------------- | :------- |
-| **Project subagents** | `.claude/agents/` | Available in current project | Highest |
-| **User subagents** | `~/.claude/agents/` | Available across all projects | Lower |
-
-When subagent names conflict, project-level subagents take precedence over user-level subagents.
-
-### Plugin agents
-
-[Plugins](/en/plugins) can provide custom subagents that integrate seamlessly with Claude Code. Plugin agents work identically to user-defined agents and appear in the `/agents` interface.
-
-**Plugin agent locations**: plugins include agents in their `agents/` directory (or custom paths specified in the plugin manifest).
-
-**Using plugin agents**:
-
-* Plugin agents appear in `/agents` alongside your custom agents
-* Can be invoked explicitly: "Use the code-reviewer agent from the security-plugin"
-* Can be invoked automatically by Claude when appropriate
-* Can be managed (viewed, inspected) through `/agents` interface
-
-See the [plugin components reference](/en/plugins-reference#agents) for details on creating plugin agents.
-
-### CLI-based configuration
-
-You can also define subagents dynamically using the `--agents` CLI flag, which accepts a JSON object:
+You now have a subagent you can use in any project on your machine to analyze codebases and suggest improvements.
+
+You can also create subagents manually as Markdown files, define them via CLI flags, or distribute them through plugins. The following sections cover all configuration options.
+
+## Configure subagents
+
+### Use the /agents command
+
+The `/agents` command provides an interactive interface for managing subagents. Run `/agents` to:
+
+* View all available subagents (built-in, user, project, and plugin)
+* Create new subagents with guided setup or Claude generation
+* Edit existing subagent configuration and tool access
+* Delete custom subagents
+* See which subagents are active when duplicates exist
+
+This is the recommended way to create and manage subagents. For manual creation or automation, you can also add subagent files directly.
+
+### Choose the subagent scope
+
+Subagents are Markdown files with YAML frontmatter. Store them in different locations depending on scope. When multiple subagents share the same name, the higher-priority location wins.
+
+| Location | Scope | Priority | How to create |
+| :--------------------------- | :---------------------- | :---------- | :------------------------------------ |
+| `--agents` CLI flag | Current session | 1 (highest) | Pass JSON when launching Claude Code |
+| `.claude/agents/` | Current project | 2 | Interactive or manual |
+| `~/.claude/agents/` | All your projects | 3 | Interactive or manual |
+| Plugin's `agents/` directory | Where plugin is enabled | 4 (lowest) | Installed with [plugins](/en/plugins) |
+
+**Project subagents** (`.claude/agents/`) are ideal for subagents specific to a codebase. Check them into version control so your team can use and improve them collaboratively.
+
+**User subagents** (`~/.claude/agents/`) are personal subagents available in all your projects.
+
+**CLI-defined subagents** are passed as JSON when launching Claude Code. They exist only for that session and aren't saved to disk, making them useful for quick testing or automation scripts:
```bash theme={null}
claude --agents '{
@@ -112,87 +156,163 @@
}'
```
-**Priority**: CLI-defined subagents have lower priority than project-level subagents but higher priority than user-level subagents.
-
-**Use case**: This approach is useful for:
-
-* Quick testing of subagent configurations
-* Session-specific subagents that don't need to be saved
-* Automation scripts that need custom subagents
-* Sharing subagent definitions in documentation or scripts
-
-For detailed information about the JSON format and all available options, see the [CLI reference documentation](/en/cli-reference#agents-flag-format).
-
-### File format
-
-Each subagent is defined in a Markdown file with this structure:
+The `--agents` flag accepts JSON with the same fields as [frontmatter](#supported-frontmatter-fields). Use `prompt` for the system prompt (equivalent to the markdown body in file-based subagents). See the [CLI reference](/en/cli-reference#agents-flag-format) for the full JSON format.
+
+**Plugin subagents** come from [plugins](/en/plugins) you've installed. They appear in `/agents` alongside your custom subagents. See the [plugin components reference](/en/plugins-reference#agents) for details on creating plugin subagents.
+
+### Write subagent files
+
+Subagent files use YAML frontmatter for configuration, followed by the system prompt in Markdown:
+
+<Note>
+ Subagents are loaded at session start. If you create a subagent by manually adding a file, restart your session or use `/agents` to load it immediately.
+</Note>
```markdown theme={null}
---
-name: your-sub-agent-name
-description: Description of when this subagent should be invoked
-tools: tool1, tool2, tool3 # Optional - inherits all tools if omitted
-model: sonnet # Optional - specify model alias or 'inherit'
-permissionMode: default # Optional - permission mode for the subagent
-skills: skill1, skill2 # Optional - skills to auto-load
----
-
-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.
-
-Include specific instructions, best practices, and any constraints
-the subagent should follow.
-```
-
-#### Configuration fields
-
-| Field | Required | Description |
-| :--------------- | :------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `name` | Yes | Unique identifier using lowercase letters and hyphens |
-| `description` | Yes | Natural language description of the subagent's purpose |
-| `tools` | No | Comma-separated list of specific tools. If omitted, inherits all tools from the main thread |
-| `model` | No | Model to use for this subagent. Can be a model alias (`sonnet`, `opus`, `haiku`) or `'inherit'` to use the main conversation's model. If omitted, defaults to the [configured subagent model](/en/model-config) |
-| `permissionMode` | No | Permission mode for the subagent. Valid values: `default`, `acceptEdits`, `dontAsk`, `bypassPermissions`, `plan`, `ignore`. Controls how the subagent handles permission requests |
-| `skills` | No | Comma-separated list of skill names to auto-load when the subagent starts. Subagents do not inherit Skills from the parent conversation. If omitted, no Skills are preloaded. |
-| `hooks` | No | Define hooks scoped to this subagent's lifecycle. Supports `PreToolUse`, `PostToolUse`, and `Stop` events. See [Define hooks for subagents](#define-hooks-for-subagents). |
-
-### Model selection
-
-The `model` field allows you to control which [AI model](/en/model-config) the subagent uses:
+name: code-reviewer
+description: Reviews code for quality and best practices
+tools: Read, Glob, Grep
+model: sonnet
+---
+
+You are a code reviewer. When invoked, analyze the code and provide
+specific, actionable feedback on quality, security, and best practices.
+```
+
+The frontmatter defines the subagent's metadata and configuration. The body becomes the system prompt that guides the subagent's behavior. Subagents receive only this system prompt (plus basic environment details like working directory), not the full Claude Code system prompt.
+
+#### Supported frontmatter fields
+
+The following fields can be used in the YAML frontmatter. Only `name` and `description` are required.
+
+| Field | Required | Description |
+| :---------------- | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `name` | Yes | Unique identifier using lowercase letters and hyphens |
+| `description` | Yes | When Claude should delegate to this subagent |
+| `tools` | No | [Tools](#available-tools) the subagent can use. Inherits all tools if omitted |
+| `disallowedTools` | No | Tools to deny, removed from inherited or specified list |
+| `model` | No | [Model](#choose-a-model) to use: `sonnet`, `opus`, `haiku`, or `inherit`. Defaults to `sonnet` |
+| `permissionMode` | No | [Permission mode](#permission-modes): `default`, `acceptEdits`, `dontAsk`, `bypassPermissions`, or `plan` |
+| `skills` | No | [Skills](/en/skills) to load into the subagent's context at startup. The full skill content is injected, not just made available for invocation. Subagents don't inherit skills from the parent conversation |
+| `hooks` | No | [Lifecycle hooks](#define-hooks-for-subagents) scoped to this subagent |
+
+### Choose a model
+
+The `model` field controls which [AI model](/en/model-config) the subagent uses:
* **Model alias**: Use one of the available aliases: `sonnet`, `opus`, or `haiku`
-* **`'inherit'`**: Use the same model as the main conversation (useful for consistency)
+* **inherit**: Use the same model as the main conversation (useful for consistency)
* **Omitted**: If not specified, uses the default model configured for subagents (`sonnet`)
-<Note>
- Using `'inherit'` is particularly useful when you want your subagents to adapt to the model choice of the main conversation, ensuring consistent capabilities and response style throughout your session.
-</Note>
-
-### Available tools
-
-Subagents can be granted access to any of Claude Code's internal tools. See the [tools documentation](/en/settings#tools-available-to-claude) for a complete list of available tools.
-
-<Tip>
- **Recommended:** Use the `/agents` command to modify tool access - it provides an interactive interface that lists all available tools, including any connected MCP server tools, making it easier to select the ones you need.
-</Tip>
-
-You have two options for configuring tools:
-
-* **Omit the `tools` field** to inherit all tools from the main thread (default), including MCP tools
-* **Specify individual tools** as a comma-separated list for more granular control (can be edited manually or via `/agents`)
-
-**MCP Tools**: Subagents can access MCP tools from configured MCP servers. When the `tools` field is omitted, subagents inherit all MCP tools available to the main thread.
+### Control subagent capabilities
+
+You can control what subagents can do through tool access, permission modes, and conditional rules.
+
+#### Available tools
+
+Subagents can use any of Claude Code's [internal tools](/en/settings#tools-available-to-claude). By default, subagents inherit all tools from the main conversation, including MCP tools.
+
+To restrict tools, use the `tools` field (allowlist) or `disallowedTools` field (denylist):
+
+```yaml theme={null}
+---
+name: safe-researcher
+description: Research agent with restricted capabilities
+tools: Read, Grep, Glob, Bash
+disallowedTools: Write, Edit
+---
+```
+
+#### Permission modes
+
+The `permissionMode` field controls how the subagent handles permission prompts. Subagents inherit the permission context from the main conversation but can override the mode.
+
+| Mode | Behavior |
+| :------------------ | :----------------------------------------------------------------- |
+| `default` | Standard permission checking with prompts |
+| `acceptEdits` | Auto-accept file edits |
+| `dontAsk` | Auto-deny permission prompts (explicitly allowed tools still work) |
+| `bypassPermissions` | Skip all permission checks |
+| `plan` | Plan mode (read-only exploration) |
+
+<Warning>
+ Use `bypassPermissions` with caution. It skips all permission checks, allowing the subagent to execute any operation without approval.
+</Warning>
+
+If the parent uses `bypassPermissions`, this takes precedence and cannot be overridden.
+
+#### Conditional rules with hooks
+
+For more dynamic control over tool usage, use `PreToolUse` hooks to validate operations before they execute. This is useful when you need to allow some operations of a tool while blocking others.
+
+This example creates a subagent that only allows read-only database queries by validating commands before execution:
+
+```yaml theme={null}
+---
+name: db-reader
+description: Execute read-only database queries
+tools: Bash
+hooks:
+ PreToolUse:
+ - matcher: "Bash"
+ hooks:
+ - type: command
+ command: "./scripts/validate-readonly-query.sh"
+---
+```
+
+The validation script inspects `$TOOL_INPUT` and exits with a non-zero code to block write operations. See [Define hooks for subagents](#define-hooks-for-subagents) for more hook configuration options.
+
+#### Disable specific subagents
+
+You can prevent Claude from using specific subagents by adding them to the `deny` array in your [settings](/en/settings#permission-settings). Use the format `Task(subagent-name)` where `subagent-name` matches the subagent's name field.
+
+```json theme={null}
+{
+ "permissions": {
+ "deny": ["Task(Explore)", "Task(my-custom-agent)"]
+ }
+}
+```
+
+This works for both built-in and custom subagents. You can also use the `--disallowedTools` CLI flag:
+
+```bash theme={null}
+claude --disallowedTools "Task(Explore)"
+```
+
+See [IAM documentation](/en/iam#tool-specific-permission-rules) for more details on permission rules.
### Define hooks for subagents
-Subagents can define hooks that run during the subagent's lifecycle. Use the `hooks` field to specify `PreToolUse`, `PostToolUse`, or `Stop` handlers:
+Subagents can define [hooks](/en/hooks) that run during the subagent's lifecycle. There are two ways to configure hooks:
+
+1. **In the subagent's frontmatter**: Define hooks that run only while that subagent is active
+2. **In `settings.json`**: Define hooks that run in the main session when subagents start or stop
+
+#### Hooks in subagent frontmatter
+
+Define hooks directly in the subagent's markdown file. These hooks only run while that specific subagent is active and are cleaned up when it finishes.
+
+| Event | Matcher input | When it fires |
+| :------------ | :------------ | :------------------------------ |
+| `PreToolUse` | Tool name | Before the subagent uses a tool |
+| `PostToolUse` | Tool name | After the subagent uses a tool |
+| `Stop` | (none) | When the subagent finishes |
+
+This example validates Bash commands with the `PreToolUse` hook and runs a linter after file edits with `PostToolUse`:
```yaml theme={null}
---
name: code-reviewer
description: Review code changes with automatic linting
hooks:
+ PreToolUse:
+ - matcher: "Bash"
+ hooks:
+ - type: command
+ command: "./scripts/validate-command.sh $TOOL_INPUT"
PostToolUse:
- matcher: "Edit|Write"
hooks:
@@ -201,214 +321,187 @@
---
```
-Hooks defined in a subagent are scoped to that subagent's execution and are automatically cleaned up when the subagent finishes.
-
-See [Hooks](/en/hooks) for the complete hook configuration format.
-
-## Managing subagents
-
-### Using the /agents command (Recommended)
-
-The `/agents` command provides a comprehensive interface for subagent management:
-
-```
-/agents
-```
-
-This opens an interactive menu where you can:
-
-* View all available subagents (built-in, user, and project)
-* Create new subagents with guided setup
-* Edit existing custom subagents, including their tool access
-* Delete custom subagents
-* See which subagents are active when duplicates exist
-* **Manage tool permissions** with a complete list of available tools
-
-### Direct file management
-
-You can also manage subagents by working directly with their files:
-
-```bash theme={null}
-# Create a project subagent
-mkdir -p .claude/agents
-echo '---
-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.' > .claude/agents/test-runner.md
-
-# Create a user subagent
-mkdir -p ~/.claude/agents
-# ... create subagent file
-```
-
-<Note>
- Subagents created by manually adding files will be loaded the next time you start a Claude Code session. To create and use a subagent immediately without restarting, use the `/agents` command instead.
-</Note>
-
-### Disabling specific subagents
-
-You can disable specific built-in or custom subagents using the `Task(AgentName)` permission rule syntax. Add these rules to the `deny` array in your [settings](/en/settings#permission-settings) or use the `--disallowedTools` CLI flag.
-
-**Example settings.json configuration:**
+`Stop` hooks in frontmatter are automatically converted to `SubagentStop` events.
+
+#### Project-level hooks for subagent events
+
+Configure hooks in `settings.json` that respond to subagent lifecycle events in the main session. Use the `matcher` field to target specific agent types by name.
+
+| Event | Matcher input | When it fires |
+| :-------------- | :-------------- | :------------------------------- |
+| `SubagentStart` | Agent type name | When a subagent begins execution |
+| `SubagentStop` | Agent type name | When a subagent completes |
+
+This example runs setup and cleanup scripts only when the `db-agent` subagent starts and stops:
```json theme={null}
{
- "permissions": {
- "deny": ["Task(Explore)", "Task(Plan)"]
+ "hooks": {
+ "SubagentStart": [
+ {
+ "matcher": "db-agent",
+ "hooks": [
+ { "type": "command", "command": "./scripts/setup-db-connection.sh" }
+ ]
+ }
+ ],
+ "SubagentStop": [
+ {
+ "matcher": "db-agent",
+ "hooks": [
+ { "type": "command", "command": "./scripts/cleanup-db-connection.sh" }
+ ]
+ }
+ ]
}
}
```
-**Example CLI usage:**
-
-```bash theme={null}
-claude --disallowedTools "Task(Explore)"
-```
-
-This is useful when you want to prevent Claude from delegating tasks to specific subagents, either for security reasons or to enforce a particular workflow.
-
-See [IAM documentation](/en/iam#tool-specific-permission-rules) for more details on permission rules.
-
-## Using subagents effectively
-
-### Automatic delegation
-
-Claude Code proactively delegates tasks based on:
-
-* The task description in your request
-* The `description` field in subagent configurations
-* Current context and available tools
+See [Hooks](/en/hooks) for the complete hook configuration format.
+
+## Work with subagents
+
+### Understand automatic delegation
+
+Claude automatically delegates tasks based on the task description in your request, the `description` field in subagent configurations, and current context. To encourage proactive delegation, include phrases like "use proactively" in your subagent's description field.
+
+You can also request a specific subagent explicitly:
+
+```
+Use the test-runner subagent to fix failing tests
+Have the code-reviewer subagent look at my recent changes
+```
+
+### Run subagents in foreground or background
+
+Subagents can run in the foreground (blocking) or background (concurrent):
+
+* **Foreground subagents** block the main conversation until complete. Permission prompts and clarifying questions (like [`AskUserQuestion`](/en/settings#tools-available-to-claude)) are passed through to you.
+* **Background subagents** run concurrently while you continue working. They inherit the parent's permissions and auto-deny anything not pre-approved. If a background subagent needs a permission it doesn't have or needs to ask clarifying questions, that tool call fails but the subagent continues. MCP tools are not available in background subagents.
+
+If a background subagent fails due to missing permissions, you can [resume it](#resume-subagents) in the foreground to retry with interactive prompts.
+
+Claude decides whether to run subagents in the foreground or background based on the task. You can also:
+
+* Ask Claude to "run this in the background"
+* Press **Ctrl+B** to background a running task
+
+### Common patterns
+
+#### Isolate high-volume operations
+
+One of the most effective uses for subagents is isolating operations that produce large amounts of output. Running tests, fetching documentation, or processing log files can consume significant context. By delegating these to a subagent, the verbose output stays in the subagent's context while only the relevant summary returns to your main conversation.
+
+```
+Use a subagent to run the test suite and report only the failing tests with their error messages
+```
+
+#### Run parallel research
+
+For independent investigations, spawn multiple subagents to work simultaneously:
+
+```
+Research the authentication, database, and API modules in parallel using separate subagents
+```
+
+Each subagent explores its area independently, then Claude synthesizes the findings. This works best when the research paths don't depend on each other.
+
+<Warning>
+ When subagents complete, their results return to your main conversation. Running many subagents that each return detailed results can consume significant context.
+</Warning>
+
+#### Chain subagents
+
+For multi-step workflows, ask Claude to use subagents in sequence. Each subagent completes its task and returns results to Claude, which then passes relevant context to the next subagent.
+
+```
+Use the code-reviewer subagent to find performance issues, then use the optimizer subagent to fix them
+```
+
+### Choose between subagents and main conversation
+
+Use the **main conversation** when:
+
+* The task needs frequent back-and-forth or iterative refinement
+* Multiple phases share significant context (planning → implementation → testing)
+* You're making a quick, targeted change
+* Latency matters. Subagents start fresh and may need time to gather context
+
+Use **subagents** when:
+
+* The task produces verbose output you don't need in your main context
+* You want to enforce specific tool restrictions or permissions
+* The work is self-contained and can return a summary
+
+Consider [Skills](/en/skills) instead when you want reusable prompts or workflows that run in the main conversation context rather than isolated subagent context.
+
+<Note>
+ Subagents cannot spawn other subagents. If your workflow requires nested delegation, use [Skills](/en/skills) or [chain subagents](#chain-subagents) from the main conversation.
+</Note>
+
+### Manage subagent context
+
+#### Resume subagents
+
+Each subagent invocation creates a new instance with fresh context. To continue an existing subagent's work instead of starting over, ask Claude to resume it.
+
+Resumed subagents retain their full conversation history, including all previous tool calls, results, and reasoning. The subagent picks up exactly where it stopped rather than starting fresh.
+
+When a subagent completes, Claude receives its agent ID. To resume a subagent, ask Claude to continue the previous work:
+
+```
+Use the code-reviewer subagent to review the authentication module
+[Agent completes]
+
+Continue that code review and now analyze the authorization logic
+[Claude resumes the subagent with full context from previous conversation]
+```
+
+You can also ask Claude for the agent ID if you want to reference it explicitly, or find IDs in the transcript files at `~/.claude/projects/{project}/{sessionId}/subagents/`. Each transcript is stored as `agent-{agentId}.jsonl`.
+
+For programmatic usage, see [Subagents in the Agent SDK](/en/agent-sdk/subagents).
+
+Subagent transcripts persist independently of the main conversation:
+
+* **Main conversation compaction**: When the main conversation compacts, subagent transcripts are unaffected. They're stored in separate files.
+* **Session persistence**: Subagent transcripts persist within their session. You can [resume a subagent](#resume-subagents) after restarting Claude Code by resuming the same session.
+* **Automatic cleanup**: Transcripts are cleaned up based on the `cleanupPeriodDays` setting (default: 30 days).
+
+#### Auto-compaction
+
+Subagents support automatic compaction using the same logic as the main conversation. When a subagent's context approaches its limit, Claude Code summarizes older messages to free up space while preserving important context.
+
+Compaction events are logged in subagent transcript files:
+
+```json theme={null}
+{
+ "type": "system",
+ "subtype": "compact_boundary",
+ "compactMetadata": {
+ "trigger": "auto",
+ "preTokens": 167189
+ }
+}
+```
+
+The `preTokens` value shows how many tokens were used before compaction occurred.
+
+## Example subagents
+
+These examples demonstrate effective patterns for building subagents. Use them as starting points, or generate a customized version with Claude.
<Tip>
- To encourage more proactive subagent use, include phrases like "use PROACTIVELY" or "MUST BE USED" in your `description` field.
+ **Best practices:**
+
+ * **Design focused subagents:** each subagent should excel at one specific task
+ * **Write detailed descriptions:** Claude uses the description to decide when to delegate
+ * **Limit tool access:** grant only necessary permissions for security and focus
+ * **Check into version control:** share project subagents with your team
</Tip>
-### Explicit invocation
-
-Request a specific subagent by mentioning it in your command:
-
-```
-> 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
-```
-
-## Built-in subagents
-
-Claude Code includes built-in subagents that are available out of the box:
-
-### General-purpose subagent
-
-The general-purpose subagent is a capable agent for complex, multi-step tasks that require both exploration and action. Unlike the Explore subagent, it can modify files and execute a wider range of operations.
-
-**Key characteristics:**
-
-* **Model**: Uses Sonnet for more capable reasoning
-* **Tools**: Has access to all tools
-* **Mode**: Can read and write files, execute commands, make changes
-* **Purpose**: Complex research tasks, multi-step operations, code modifications
-
-**When Claude uses it:**
-
-Claude delegates to the general-purpose subagent when:
-
-* The task requires both exploration and modification
-* Complex reasoning is needed to interpret search results
-* Multiple strategies may be needed if initial searches fail
-* The task has multiple steps that depend on each other
-
-**Example scenario:**
-
-```
-User: Find all the places where we handle authentication and update them to use the new token format
-
-Claude: [Invokes general-purpose subagent]
-[Agent searches for auth-related code across codebase]
-[Agent reads and analyzes multiple files]
-[Agent makes necessary edits]
-[Returns detailed writeup of changes made]
-```
-
-### Plan subagent
-
-The Plan subagent is a specialized built-in agent designed for use during plan mode. When Claude is operating in plan mode (non-execution mode), it uses the Plan subagent to conduct research and gather information about your codebase before presenting a plan.
-
-**Key characteristics:**
-
-* **Model**: Uses Sonnet for more capable analysis
-* **Tools**: Has access to Read, Glob, Grep, and Bash tools for codebase exploration
-* **Purpose**: Searches files, analyzes code structure, and gathers context
-* **Automatic invocation**: Claude automatically uses this agent when in plan mode and needs to research the codebase
-
-**How it works:**
-When you're in plan mode and Claude needs to understand your codebase to create a plan, it delegates research tasks to the Plan subagent. This prevents infinite nesting of agents (subagents cannot spawn other subagents) while still allowing Claude to gather the necessary context.
-
-**Example scenario:**
-
-```
-User: [In plan mode] Help me refactor the authentication module
-
-Claude: Let me research your authentication implementation first...
-[Internally invokes Plan subagent to explore auth-related files]
-[Plan subagent searches codebase and returns findings]
-Claude: Based on my research, here's my proposed plan...
-```
-
-<Tip>
- The Plan subagent is only used in plan mode. In normal execution mode, Claude uses the general-purpose agent or other custom subagents you've created.
-</Tip>
-
-### Explore subagent
-
-The Explore subagent is a fast, lightweight agent optimized for searching and analyzing codebases. It operates in strict read-only mode and is designed for rapid file discovery and code exploration.
-
-**Key characteristics:**
-
-* **Model**: Uses Haiku for fast, low-latency searches
-* **Mode**: Strictly read-only - cannot create, modify, or delete files
-* **Tools available**:
- * Glob - File pattern matching
- * Grep - Content searching with regular expressions
- * Read - Reading file contents
- * Bash - Read-only commands only (ls, git status, git log, git diff, find, cat, head, tail)
-
-**When Claude uses it:**
-
-Claude will delegate to the Explore subagent when it needs to search or understand a codebase but doesn't need to make changes. This is more efficient than the main agent running multiple search commands directly, as content found during the exploration process doesn't bloat the main conversation.
-
-**Thoroughness levels:**
-
-When invoking the Explore subagent, Claude specifies a thoroughness level:
-
-* **Quick** - Fast searches with minimal exploration. Good for targeted lookups.
-* **Medium** - Moderate exploration. Balances speed and thoroughness.
-* **Very thorough** - Comprehensive analysis across multiple locations and naming conventions. Used when the target might be in unexpected places.
-
-**Example scenarios:**
-
-```
-User: Where are errors from the client handled?
-
-Claude: [Invokes Explore subagent with "medium" thoroughness]
-[Explore uses Grep to search for error handling patterns]
-[Explore uses Read to examine promising files]
-[Returns findings with absolute file paths]
-Claude: Client errors are handled in src/services/process.ts:712...
-```
-
-```
-User: What's the codebase structure?
-
-Claude: [Invokes Explore subagent with "quick" thoroughness]
-[Explore uses Glob and ls to map directory structure]
-[Returns overview of key directories and their purposes]
-```
-
-## Example subagents
-
### Code reviewer
+
+A read-only subagent that reviews code without modifying it. This example shows how to design a focused subagent with limited tool access (no Edit or Write) and a detailed prompt that specifies exactly what to look for and how to format output.
```markdown theme={null}
---
@@ -445,6 +538,8 @@
### Debugger
+A subagent that can both analyze and fix issues. Unlike the code reviewer, this one includes Edit because fixing bugs requires modifying code. The prompt provides a clear workflow from diagnosis to verification.
+
```markdown theme={null}
---
name: debugger
@@ -480,6 +575,8 @@
### Data scientist
+A domain-specific subagent for data analysis work. This example shows how to create subagents for specialized workflows outside of typical coding tasks. It explicitly sets `model: sonnet` for more capable analysis.
+
```markdown theme={null}
---
name: data-scientist
@@ -513,102 +610,13 @@
Always ensure queries are efficient and cost-effective.
```
-## Best practices
-
-* **Start with Claude-generated agents**: We highly recommend generating your initial subagent with Claude and then iterating on it to make it personally yours. This approach gives you the best results - a solid foundation that you can customize to your specific needs.
-
-* **Design focused subagents**: Create subagents with single, clear responsibilities rather than trying to make one subagent do everything. This improves performance and makes subagents more predictable.
-
-* **Write detailed prompts**: Include specific instructions, examples, and constraints in your system prompts. The more guidance you provide, the better the subagent will perform.
-
-* **Limit tool access**: Only grant tools that are necessary for the subagent's purpose. This improves security and helps the subagent focus on relevant actions.
-
-* **Version control**: Check project subagents into version control so your team can benefit from and improve them collaboratively.
-
-## Advanced usage
-
-### Chaining subagents
-
-For complex workflows, you can chain multiple subagents:
-
-```
-> First use the code-analyzer subagent to find performance issues, then use the optimizer subagent to fix them
-```
-
-### Dynamic subagent selection
-
-Claude Code intelligently selects subagents based on context. Make your `description` fields specific and action-oriented for best results.
-
-### Resumable subagents
-
-Subagents can be resumed to continue previous conversations, which is particularly useful for long-running research or analysis tasks that need to be continued across multiple invocations.
-
-**How it works:**
-
-* Each subagent execution is assigned a unique `agentId`
-* The agent's conversation is stored in a separate transcript file: `agent-{agentId}.jsonl`
-* You can resume a previous agent by providing its `agentId` via the `resume` parameter
-* When resumed, the agent continues with full context from its previous conversation
-
-**Example workflow:**
-
-Initial invocation:
-
-```
-> Use the code-analyzer agent to start reviewing the authentication module
-
-[Agent completes initial analysis and returns agentId: "abc123"]
-```
-
-Resume the agent:
-
-```
-> Resume agent abc123 and now analyze the authorization logic as well
-
-[Agent continues with full context from previous conversation]
-```
-
-**Use cases:**
-
-* **Long-running research**: Break down large codebase analysis into multiple sessions
-* **Iterative refinement**: Continue refining a subagent's work without losing context
-* **Multi-step workflows**: Have a subagent work on related tasks sequentially while maintaining context
-
-**Technical details:**
-
-* Agent transcripts are stored in your project directory
-* Recording is disabled during resume to avoid duplicating messages
-* Both synchronous and asynchronous agents can be resumed
-* The `resume` parameter accepts the agent ID from a previous execution
-
-**Programmatic usage:**
-
-If you're using the Agent SDK or interacting with the AgentTool directly, you can pass the `resume` parameter:
-
-```typescript theme={null}
-{
- "description": "Continue analysis",
- "prompt": "Now examine the error handling patterns",
- "subagent_type": "code-analyzer",
- "resume": "abc123" // Agent ID from previous execution
-}
-```
-
-<Tip>
- Keep track of agent IDs for tasks you may want to resume later. Claude Code displays the agent ID when a subagent completes its work.
-</Tip>
-
-## Performance considerations
-
-* **Context efficiency**: Agents help preserve main context, enabling longer overall sessions
-* **Latency**: Subagents start off with a clean slate each time they are invoked and may add latency as they gather context that they require to do their job effectively.
-
-## Related documentation
-
-* [Plugins](/en/plugins) - Extend Claude Code with custom agents through plugins
-* [Slash commands](/en/slash-commands) - Learn about other built-in commands
-* [Settings](/en/settings) - Configure Claude Code behavior
-* [Hooks](/en/hooks) - Automate workflows with event handlers
+## Next steps
+
+Now that you understand subagents, explore these related features:
+
+* [Distribute subagents with plugins](/en/plugins) to share subagents across teams or projects
+* [Run Claude Code programmatically](/en/headless) with the Agent SDK for CI/CD and automation
+* [Use MCP servers](/en/mcp) to give subagents access to external tools and data
---