A subagent is a Markdown file with YAML frontmatter in .claude/agents/ or ~/.claude/agents/. It defines a system prompt plus optional tool allowlist, model, permission mode, memory scope, and worktree isolation. Claude Code delegates matching work to it in a fresh context window and receives back only its summary. Only name and description are required, and model defaults to inherit, so a subagent saves nothing on model cost unless you say otherwise.
- Each subagent gets a fresh context window. It cannot see your conversation.
- Markdown plus YAML frontmatter in
.claude/agents/. Onlynameanddescriptionare required. modeldefaults toinherit, so delegation is not a saving until you set it.- You already use subagents: Explore, Plan, and general-purpose are built in.
- The strongest reasons to define one are a narrow tool list and
isolation: worktree.
The built-in ones you already use
Before you write a custom subagent, know what is already delegating. Claude Code ships built-in subagents it uses automatically, and most of the context saving people attribute to their own configuration is coming from these.
Built-in subagents as of August 2026.
| Name | Model | Used when |
|---|---|---|
Explore | Inherits the main conversation, capped at Opus on the Claude API | Searching or understanding a codebase without changing it |
Plan | Inherits | Research during plan mode, before a plan is presented |
general-purpose | Inherits | Tasks needing exploration and modification, or several dependent steps |
claude | Inherits | Catch-all with every tool available to subagents |
Explore used to run on Haiku unconditionally. As of Claude Code v2.1.198 it inherits the main conversation’s model instead, capped at Opus on the Claude API, so a session on a higher tier does not run exploration on a more expensive model than you chose. If you want exploration cheap again, define your own.
---
name: Explore
description: Searches and explains a codebase without changing it.
tools: Read, Grep, Glob
model: haiku
---
You answer questions about a codebase by reading it. Never edit a file.
Return a short summary with exact file paths and line references, not
a transcript of what you looked at.
{
"permissions": {
"deny": ["Agent(Explore)", "Agent(my-custom-agent)"]
}
}
Defining one
Create the file
Put it in .claude/agents/<name>.md for the project or ~/.claude/agents/<name>.md for yourself. Subdirectories are fine; identity comes from the name field, not the path.
Write the description as a trigger
Describe when to delegate, not what the subagent is. Claude routes on this text alone.
Narrow the tools, then set the model
Omitting tools inherits everything, which defeats the main reason to define one. Set model explicitly, because it defaults to inherit.
Verify it took effect
Files are watched, so an edit applies within a few seconds. If Claude cannot find a brand new user subagent, restart: a running session does not detect a ~/.claude/agents/ directory that did not exist at launch.
---
name: test-writer
description: Writes unit tests for existing code. Use when asked to add test coverage for a file that already works.
tools: Read, Write, Bash
model: haiku
maxTurns: 20
---
You write unit tests for code that already exists and works.
Rules:
- Match the existing test style in the repository. Read a neighbouring
test file first; do not invent a new convention.
- Test behaviour through the public interface, never private internals.
- Cover the error paths, not only the happy path.
- Never modify the implementation. If it looks wrong, say so and stop.
- Run the tests you wrote before reporting done.
The frontmatter fields worth knowing. Only name and description are required.
| Field | Purpose |
|---|---|
name | Identifier. Lowercase and hyphens; a : is rejected because it is the plugin namespace separator |
description | How Claude decides to delegate. This is the routing logic, so write it as a precise trigger |
tools | Allowlist. Omit to inherit every tool available to subagents, which is usually wrong |
disallowedTools | Denylist, applied after the inherited or specified list |
model | sonnet, opus, haiku, a full model ID, or inherit. Defaults to inherit |
permissionMode | default, acceptEdits, auto, dontAsk, bypassPermissions, or plan |
maxTurns | Hard cap on agentic turns before it stops |
effort | low to max, overriding the session effort level |
skills | Skills preloaded into its context at startup, in full, not just the description |
memory | user, project, or local. Gives this subagent its own persistent notes |
isolation | Set to worktree to run it in a temporary git worktree |
background | true to always run detached. Claude already backgrounds subagents by default |
mcpServers | MCP servers available to this subagent only |
hooks | Lifecycle hooks scoped to this subagent |
Scope, precedence, and where files live
Definition sources, highest precedence first. As of August 2026.
| Source | Available in | Commit? |
|---|---|---|
Managed settings .claude/agents/ | Whole organisation | Deployed by IT |
--agents CLI flag (JSON) | That session only | No |
.claude/agents/ | The project | Yes |
~/.claude/agents/ | All your projects | No |
- Both directories are scanned recursively, so
agents/review/andagents/research/are fine. Identity comes only from thenamefield, never the path. - Project subagents are discovered by walking up from the working directory, so every
.claude/agents/between there and the repo root is scanned. When two nested directories define the same name, the one closest to the working directory wins. - Two files in the same directory sharing a name is undefined: Claude Code loads one by filesystem read order.
/doctorreports the clash. - Files are watched, so an edit takes effect within a few seconds with no restart. The exception: if
~/.claude/agents/did not exist when the session started, you have to restart.
What isolation actually costs
The pitch for subagents is that they keep your main context clean. That is true and it is only half the picture.
| Isolation gives you | Isolation costs you |
|---|---|
| A clean main conversation | The subagent cannot see your discussion |
| A separate context budget | Context has to be re-established from scratch |
| Narrow tool permissions | More configuration to maintain |
| A cheaper model for cheap work | A handoff summary that can lose nuance |
| A worktree the main branch cannot see | A worktree the main session cannot see either |
A subagent receives its own system prompt plus basic environment details such as the working directory. It does not receive the full Claude Code system prompt, and it does not receive the main conversation’s auto memory. The one exception is a fork, which inherits the parent conversation and system prompt.
Where they earn their keep
| Task | Why it suits a subagent |
|---|---|
| Writing tests for a finished file | Stands alone; the file is the whole context |
| Reviewing a diff | A fresh reader is genuinely better than a tired one |
| Mechanical migration across many files | Repetitive, and a cheaper model handles it |
| Searching a large codebase | Returns a summary instead of filling your context |
| A risky refactor you want quarantined | isolation: worktree keeps it off your checkout |
| Generating documentation from code | Self-contained input |
| Task | Why it does not |
|---|---|
| Debugging something you are mid-way through | All the value is in the context it will not have |
| Anything needing your earlier decisions | Same reason |
| Small edits | The handoff costs more than the edit |
| Exploratory work | The point is accumulating context, not discarding it |
The review subagent is still the best one
A reviewer with read-only tools, a fresh context, and an instruction to be sceptical is the highest-value subagent most repositories can have. It catches things the agent that wrote the code cannot, because it has none of the investment in the approach.
---
name: reviewer
description: Reviews a diff for bugs before it is committed. Use after changes are complete.
tools: Read, Grep, Glob, Bash
model: opus
effort: high
---
You review diffs for defects. You did not write this code and you have
no attachment to the approach.
Report only:
- Bugs that will actually occur, with the input that triggers them.
- Security issues with a concrete exploit path.
- Broken error handling.
Do not report style, naming, or preferences. If you find nothing real,
say so plainly. A short honest review is worth more than a long one.
Worktree isolation, for anything you would not run on your checkout
Setting isolation: worktree gives the subagent a temporary git worktree, branched by default from your default branch rather than the parent session’s HEAD. Its Bash commands run inside that worktree, and Claude Code blocks commands that try to redirect git back at your main checkout. The worktree is cleaned up automatically if the subagent made no changes.
---
name: migrator
description: Applies a mechanical codemod across many files. Use for framework or API migrations.
tools: Read, Edit, Write, Grep, Glob, Bash
model: haiku
isolation: worktree
permissionMode: acceptEdits
maxTurns: 60
---
Apply the requested migration across every matching file.
Do not change behaviour. If a file needs a judgement call, skip it and
list it at the end rather than guessing. Run the build before reporting
done and report the file count you changed.
The cost model
A subagent runs a full agent loop with its own context, so it is not free. Whether it saves money depends almost entirely on the model field.
- Inherited model, small task: more expensive. You paid to re-establish context and gained nothing.
- Cheaper model, mechanical task: substantially cheaper. This is the real win, and it requires the
modelfield. - Expensive model, review of a finished diff: worth it. Catching one real bug pays for a lot of reviews.
- Search that would have filled your main context: cheaper, because context cost compounds across every later turn.
Questions people ask
Named configurations with their own system prompt, tool allowlist, and optionally their own model, permission mode, and git worktree. Claude Code delegates matching tasks to them in a fresh context window and receives back a summary rather than the full transcript.
Markdown files with YAML frontmatter in .claude/agents/ for a project, or ~/.claude/agents/ for all your projects. Both are scanned recursively, and identity comes from the name field rather than the path.
Only when you set the model field. It defaults to inherit as of August 2026, so a subagent on the same model for a small task costs more than doing the work inline, because context has to be re-established. Routing mechanical work to a cheaper model is the reliable saving.
It could not see your conversation. Subagents start with a fresh context and receive only their own system prompt plus basic environment details, so any task whose value lies in the reasoning that preceded it is a poor fit for delegation.
As of Claude Code v2.1.198, running /agents prints a reminder rather than opening the interactive creation wizard. Ask Claude to write the file, or create it in .claude/agents/ yourself. The file format and locations are unchanged.
From the description field. Write it as a precise statement of when the subagent applies, because that text is the routing logic. A vague description produces random delegation.
Yes. Set isolation: worktree in the frontmatter. The subagent gets a temporary worktree branched from your default branch, its shell commands run inside it, and the worktree is cleaned up automatically if it made no changes.
Add Agent(name) to permissions.deny in settings.json to block one, deny the bare Agent tool to block all delegation, or set CLAUDE_CODE_DISABLE_EXPLORE_PLAN_AGENTS=1 to remove only the built-in Explore and Plan agents.
Sources
Every figure above was read from these pages on August 2026. Vendors reprice without notice; if you find a stale number, tell us.