Claude Code subagents: how to create and use them
Zihua Li · · 9 min read
Ask Claude Code to find every caller of a function in a large repository and the answer arrives buried under forty file reads you’ll never look at again. Subagents exist for exactly that: the reading happens somewhere else, and only the answer comes back.
A Claude Code subagent is a specialized assistant that Claude starts to handle a side task in its own context window, with its own system prompt, tool access and model, and that returns only a summary to the main conversation. Claude Code ships built-in subagents such as Explore, Plan and general-purpose, and you can define your own as Markdown files with YAML frontmatter in .claude/agents/ for a project or ~/.claude/agents/ for all your projects.
What a Claude Code subagent is
Every turn in a Claude Code session carries the conversation so far. When Claude reads thirty files to answer one question, those thirty files stay in context for the rest of the session, crowding out what matters and making each later turn more expensive. A subagent breaks that link. Claude writes a task message, a separate assistant does the work in a fresh context window, and only its final report comes back.
A subagent that isn’t a fork starts with:
- Its own system prompt: the body of its definition file, plus environment details such as the working directory, in place of Claude Code’s own system prompt.
- The task message Claude writes when it delegates. The subagent doesn’t see your conversation history, files Claude already read, or skills already invoked.
- Your CLAUDE.md files and a git status snapshot, except for the built-in Explore and Plan, which skip both to stay quick.
- Its own tools and model, as set in its definition.
That last point is what makes subagents more than a context trick. A reviewer can be denied write access, a search agent can run on a cheaper model, and a database agent can get one MCP server and nothing else. One thing doesn’t change: a subagent’s requests count toward the same usage limits as the main conversation.
Built-in subagents
Claude Code uses several subagents without any setup. Each inherits the main conversation’s permission rules, and most run with a restricted tool set:
| Subagent | Tools | What Claude uses it for |
|---|---|---|
| Explore | Read-only (Write and Edit denied) | Finding and understanding code in a codebase without changing it. Claude picks a thoroughness level: quick, medium or very thorough |
| Plan | Read-only (Write and Edit denied) | Research during plan mode, so exploration stays out of the main context while you plan |
| general-purpose | Every tool available to subagents | Multi-step tasks that need both exploration and changes |
| claude, statusline-setup, claude-code-guide and others | Varies | Helpers for specific jobs: a catch-all, /statusline setup, questions about Claude Code itself |
Explore is the one you’ll notice most: when Claude needs to understand code before changing it, it often sends Explore to search and gets back a summary. A user or project subagent named Explore overrides the built-in, which is how you would pin exploration to a lower-cost model with model: haiku.
How to create a subagent
A custom subagent is a Markdown file: YAML frontmatter for configuration, then the system prompt. The quickest way to get one is to ask Claude to write it:
Create a project subagent in .claude/agents/ called test-runner that runs
the test suite and reports only failing tests with their error messages.
Give it Read, Grep, Glob and Bash, and have it use Haiku.The result looks like this, and writing it by hand works just as well:
---
name: test-runner
description: Runs the test suite and reports only the failures. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: haiku
---
You run this project's tests and report what failed.
1. Run the test command from CLAUDE.md (or npm test if none is given).
2. For each failing test, report the test name, the assertion message and the
file and line where it failed.
3. Leave out passing tests and full logs. Do not change any files.Save it as .claude/agents/test-runner.md. Claude Code watches the agents directories and picks up new or edited files within a few seconds, without a restart. The filename doesn’t matter; the name field is the identity, and a file without name and description is skipped. In recent versions the /agents command prints a pointer to these directories rather than opening the editor wizard it had in v2.1.197 and earlier.
Where the file lives decides who can use it. Higher entries win on a name clash:
- Managed settings, for subagents an organization deploys to everyone.
- The
--agentsflag, which takes JSON definitions for one session, handy for scripts and quick tests. .claude/agents/in the project, committed so the team shares and improves it.~/.claude/agents/, your personal subagents in every project.- A plugin’s
agents/directory, available while the plugin is enabled.
Only name and description are required. The other frontmatter fields, as of November 2026:
| Field | What it does |
|---|---|
| name | Required. Unique identifier, such as code-reviewer; no colons |
| description | Required. When Claude should delegate to this subagent |
| tools | Allowlist, such as Read, Grep, Bash. Omit it to inherit every tool available to subagents |
| disallowedTools | Denylist, removed from the inherited or listed tools |
| model | sonnet, opus, haiku, fable, a full model ID, or inherit. Omitted: the parent conversation’s model |
| permissionMode | default, acceptEdits, auto, dontAsk, bypassPermissions or plan |
| maxTurns | Stops the subagent after this many agentic turns |
| skills | Skills to preload into the subagent’s context in full |
| mcpServers | MCP servers this subagent can use |
| hooks | Hooks that run only while this subagent is active |
| memory | Persistent memory scope: user, project or local |
| isolation | worktree runs the subagent in a temporary git worktree |
| effort, color, background | Effort level, display color, always-background |
Field names are camelCase and must match exactly; Claude Code ignores a misspelled field without an error, so allowedTools instead of tools leaves the subagent with every tool. Plugin subagents ignore hooks, mcpServers and permissionMode for security reasons.
Tools, models and permissions
The tools list is an allowlist and disallowedTools a denylist. Restricting tools is the main lever for a focused subagent, with one caveat the docs spell out: a subagent with Bash can still change files through the shell, even without Edit or Write. To keep Bash but forbid specific commands, add a deny rule such as Bash(git push *) to permissions.deny in settings; it applies to the main conversation and every subagent.
For the model, Claude Code takes the first of: a model Claude passes when it starts the subagent, the model field, the CLAUDE_CODE_SUBAGENT_MODEL environment variable, and finally the main conversation’s model. Routing searches and test runs to Haiku and keeping reviews on the main model is a common split.
Permissions follow the session. Subagents inherit the main conversation’s permission rules, and a subagent running in the background surfaces its permission prompts in your main session, naming which subagent is asking. A grant that lasts beyond one call applies to the whole session, main conversation included.
How Claude delegates to subagents
Most delegation is automatic. Claude matches the task in front of it against each subagent’s description, so the description is effectively a routing rule: say what the subagent does and when to use it, and add “use proactively” if Claude should reach for it without being asked. Descriptions load into every session, and Claude Code warns at startup when custom subagents’ descriptions together pass 15,000 tokens, so keep detail in the body.
When you want a specific subagent, ask for it in one of three ways:
Use the test-runner subagent to check the payment module
@"test-runner (agent)" run the tests for src/billing
claude --agent code-reviewerNaming it in plain language lets Claude decide; an @-mention runs it for that task; and claude --agent (or "agent" in .claude/settings.json) makes the whole session run as that subagent, with its system prompt, tools and model. To stop Claude from using a subagent at all, deny it:
{
"permissions": {
"deny": ["Agent(Explore)", "Agent(my-custom-agent)"]
}
}Running subagents in parallel
Subagents that Claude starts from the main conversation run in the background by default, so you keep working while they do, and their results arrive as a notification in a later turn. Independent investigations can run side by side:
Research the authentication, database and API modules in parallel
using separate subagents, and give me one summary of how a request
flows through them.The limits, as of November 2026: up to 20 subagents running at once in a session (changeable with CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS), no cap on the total over a session, and nesting up to three layers below the main conversation (CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH). A /tasks command lists what’s running and lets you open a subagent’s transcript.
A fork is a special case. Instead of starting fresh, it inherits the whole conversation and shares its prompt cache, which makes it cheaper when the task needs everything said so far. Start one with /subtask followed by the task (/fork when agent view is turned off). When subagents edit files in parallel, give them isolation: worktree so each works on its own copy of the repository.
Subagents are one of several ways Claude Code runs work in parallel. Agent view (claude agents) dispatches and monitors whole background sessions, agent teams coordinate several sessions under a lead and are experimental and off by default, and dynamic workflows run many subagents from a script. Subagents are the in-session option: they report back to the conversation that started them.
When subagents help and when they hurt
Subagents pay off when the work is self-contained and its by-products are noise:
- High-volume output: test runs, log digging, documentation lookups. The logs stay in the subagent; the failing tests come back.
- Parallel research across modules that don’t depend on each other.
- Enforced constraints: a reviewer without write tools, a researcher with one MCP server.
- A focused second look at a change, from a context that didn’t write it.
They cost more than they return when the work needs the conversation:
- Iterative work with lots of back-and-forth, or phases that share context, like planning, implementing and testing one feature.
- Small, targeted changes, where a fresh subagent spends longer gathering context than the edit takes.
- Fan-outs that return long reports. Every result lands in the main context, and every subagent spends its own tokens while it runs.
The other failure mode is lost context. The subagent knows only what Claude put in the task message, so a rule that lives only in the conversation (“ignore the vendor directory”) won’t reach it unless it’s in CLAUDE.md or restated in the task.
Subagents vs skills and hooks
The three extension points solve different problems. Skills package instructions and scripts that load into the current conversation when a task calls for them; use one when you want a reusable procedure without leaving the main context. Hooks are commands Claude Code runs at fixed lifecycle points, such as before a tool call, with no model judgment involved; use them for rules that must hold every time. Subagents are separate workers with their own context, tools and model. They also combine: a subagent can preload skills with the skills field and carry its own hooks in frontmatter, and hooks on tool events in settings fire inside subagents too.
Claude Code subagents are Markdown files with YAML frontmatter in .claude/agents/ or ~/.claude/agents/. Each runs in its own context window with its own system prompt, tool allowlist and model, and returns a summary to the main conversation. Claude delegates based on each subagent’s description; up to 20 can run at once in a session by default, as of November 2026.
A code-reviewer subagent that runs HeyDeer
A review is a good fit for a subagent: it produces a lot of reading, the result fits in a short report, and it benefits from a context that didn’t write the code. The docs’ own example reviewer works from git diff and a checklist. This version hands the review to the HeyDeer CLI, which reviews the branch’s commits plus uncommitted changes to tracked files against the default branch, verifies each finding against the code, and with --json prints the finished review as one JSON object on stdout. Save it as .claude/agents/code-reviewer.md:
---
name: code-reviewer
description: Reviews the current branch with HeyDeer and fixes what it finds. Use proactively when a change is finished, before opening a pull request.
tools: Read, Grep, Glob, Bash, Edit
model: inherit
---
You review the current branch with the HeyDeer CLI and fix the findings
you agree with.
1. Go to the repository root (git rev-parse --show-toplevel).
2. Write one or two sentences on what the change is meant to do and the
issue it fixes, from the task you were given and git log for the branch.
3. Run: heydeer review --json "<those sentences>"
The review takes a few minutes. Use a Bash timeout of 600000 ms. If the
command moves to the background, wait for it to finish and read its
output. The finished review is one JSON object on stdout.
4. Read result.publicationFindings. For each finding, open the file at the
reported line and decide whether it is right.
5. Fix the findings you agree with, keeping each fix small. Run the
relevant tests when a fix changes behaviour.
6. If you changed anything, run the same heydeer review command again.
It covers what changed since the last run and rechecks open findings.
7. Report back: each finding with severity, file:line and title; what you
fixed; what you left open and why. Leave committing, pushing and opening
the pull request to the main conversation.A few choices in that file are deliberate. Bash is in the tool list because the subagent has to run the CLI, and Edit lets it fix findings; drop Edit for a reviewer that only reports. Each finding in result.publicationFindings has a severity, a title, a file and line, and a body that explains it, which is what the report step relies on. heydeer review exits 0 whether or not it found anything, so the subagent reads the JSON rather than the exit code. The timeout note matters because a review takes minutes and Claude Code’s Bash tool defaults to two. The second run is worth asking for: later reviews of a branch cover what changed since the last one and recheck the findings still open.
Before the first run, install the CLI with curl -fsSL https://heydeer.ai/install.sh | sh on macOS and Linux, or npm install -g heydeer with Node.js 22 or later, and sign in with heydeer login. Then ask Claude to “have the code-reviewer subagent check this branch,” or let the description route to it when a change is done. To make the review a requirement rather than a habit, pair the subagent with the review gate hook that blocks gh pr create until a review has finished for the current commit. The CLI docs list every flag, and the AI code review CLI page explains what a local review sends. For Claude’s built-in /code-review, see the Claude Code review skill guide.
Sources
Frequently asked questions
Does Claude Code support subagents?
Claude Code supports subagents: Claude delegates side tasks to them, and each runs in its own context window with its own system prompt, tools and model before returning a summary. It includes built-in subagents such as Explore, Plan and general-purpose, and you can add custom ones as Markdown files in .claude/agents/ or ~/.claude/agents/.
Can Claude Code have multiple agents?
Claude Code can run multiple agents. Within one session, Claude can start several subagents at once and combine their results. Across sessions, agent view runs and monitors background sessions, and agent teams, an experimental feature that is off by default, let a lead session coordinate teammates that message each other.
How many subagents can Claude Code run?
Claude Code runs up to 20 subagents at the same time in a session by default, as of November 2026; the CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS environment variable changes the limit. There is no cap on the total number of subagents over a session, and subagents can spawn their own subagents up to three layers deep by default.
What is the difference between agents and subagents in Claude Code?
In Claude Code, a subagent is a worker inside one session: Claude delegates a task to it, and it returns a summary to the conversation that started it. Agents in the broader sense are full sessions, such as background sessions in agent view or teammates in an agent team, which you or a lead session coordinate. A subagent definition can also run as the main agent of a session with claude --agent.
How do I create a subagent in Claude Code?
To create a subagent in Claude Code, ask Claude to write one or add a Markdown file to .claude/agents/ (one project) or ~/.claude/agents/ (all projects). The file starts with YAML frontmatter containing at least name and description, optionally tools and model, and its body is the subagent’s system prompt. Claude Code picks up new files within a few seconds.
Do Claude Code subagents read CLAUDE.md?
Claude Code subagents you define load the same CLAUDE.md files as the main conversation, along with a git status snapshot. The built-in Explore and Plan subagents skip CLAUDE.md to stay fast, and a custom subagent can skip it with omitClaudeMd: true. Subagents never see the main conversation’s history, only the task message Claude writes for them.
Do subagents cost more in Claude Code?
Claude Code subagents send their own model requests, which count toward the same usage limits as the main conversation, so several subagents running at once multiply token use. They can also save cost: a subagent on a smaller model such as Haiku can handle searches or test runs, and keeping verbose output out of the main context makes later turns cheaper.



