Subagents
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
A long Claude Code session gets heavier with everything it reads. Ask it to investigate how authentication works and it may read thirty files, all of which then ride along in your conversation for the rest of the session. A subagent solves this by doing that work in its own separate context window and handing back only a summary. That's the core benefit, and it's also the only thing about subagents that's easy to overrate, so this lesson is careful about what they're actually good for.
What you'll learn
- What a subagent is and what it does and doesn't receive.
- The built-in subagents and how to define your own.
- How Claude decides to delegate, and how you can ask explicitly.
- Tool restrictions, limits, cost, and when not to use one.
Before you start
What a subagent is
Anthropic describes subagents as specialised assistants that each run in their own context window, with their own system prompt, tool access and permissions. When a side task would otherwise flood your conversation with search results, logs or file contents, the subagent does that work in its own window and returns only a summary. The documentation calls this context isolation.
It's the same idea as splitting work between people. If you ask a colleague to find out how token refresh works and they come back with three sentences and a file name, you've learned what you needed without reading their notes.
Built-in subagents
- Explore: a fast, read-only agent for searching and analysing codebases. Claude chooses a thoroughness level of quick, medium or very thorough.
- Plan: a read-only research agent used in plan mode to gather context before presenting a plan.
- General-purpose: a capable agent for complex, multi-step work that needs both exploring and acting, with every tool available to subagents.
Claude also uses a few helper agents on its own (for example a claude-code-guide agent that answers questions about Claude Code itself). You rarely need to ask for any of this: broad codebase searches typically go to Explore automatically.
Defining your own
A subagent is a markdown file with YAML frontmatter. The frontmatter configures it and the body becomes its system prompt. Only name and description are required:
---
name: reviewer
description: Reviews changed code against the task or spec it was written for. Use proactively after finishing a feature and before opening a pull request.
tools: Read, Grep, Glob
---
You are reviewing code you did not write. You are given a description of the task or a spec.
Compare the changed files with it. Check that every requirement is implemented, that edge cases have tests,
and that nothing unrelated was changed. Report only gaps that affect correctness or security, with the file
and line. Do not comment on style. If you find nothing, say so plainly.
The other documented fields are disallowedTools, permissionMode, maxTurns, skills, mcpServers, hooks, memory, effort, isolation, color and model. They're camelCase and must match exactly; unrecognised fields are ignored without an error, so a typo like disallowed_tools silently does nothing. The name must be unique and can't contain a colon. A second example, a cheap helper that only reports failures:
---
name: test-runner
description: Runs the test suite or a named test file and reports only the failures. Use when the full test output would be long.
tools: Bash, Read
model: haiku
---
Run the tests you are asked to run with `vendor/bin/pest`. Return a short summary: how many passed, how many
failed, and for each failure the test name, the assertion message and the file and line. Do not fix anything.
Do not paste passing output.
Where you save the file decides its scope and priority. Higher priority wins when names collide:
| Location | Scope | Priority |
| Managed settings | Organisation-wide | 1 (highest) |
--agents CLI flag | This session | 2 |
.claude/agents/ | This project (commit it) | 3 |
~/.claude/agents/ | All your projects | 4 |
Plugin agents/ folder | Where the plugin is enabled | 5 |
Claude Code watches these folders, so edits usually take effect within seconds. Ask Claude to create one for you, or edit the files directly.
How Claude delegates
Claude delegates based on each subagent's description and the task at hand. So the description is written for matching, just like a skill's. Phrases like "use proactively" encourage automatic delegation. Keep descriptions short: when the combined descriptions of your custom subagents exceed 15,000 tokens, Claude Code shows a startup warning. You can also ask explicitly:
- In plain language:
use the test-runner subagent to fix failing tests
- With an @-mention:
@"code-reviewer (agent)" look at the auth changes
- For a whole session:
claude --agent code-reviewer runs the main thread with that subagent's tool restrictions and model.
What a subagent knows
This is the part that surprises people. A subagent starts with a fresh context. It does not get your conversation history, the skills you've already invoked, or the files Claude has already read. It receives its own system prompt (plus basic environment details), the delegation message Claude writes for it, and your CLAUDE.md files. The built-in Explore and Plan agents skip CLAUDE.md, and custom subagents can opt out with omitClaudeMd: true. Output styles and auto memory from the main conversation don't carry over.
Two consequences. First, the quality of a delegation depends on the message Claude writes, so when you delegate by hand, include what the subagent needs: file names, the goal, what "done" looks like. Second, a subagent can end up slower than doing the task yourself, because it first has to gather context your conversation already had. There's also a way around this: a fork starts with a copy of your conversation so far.
Tools, permissions and limits
- Tools. A subagent inherits the main conversation's tools, built-in and MCP, unless you restrict it with
tools (an allow-list) or disallowedTools (a deny-list; applied first). A reviewer with tools: Read, Grep, Glob can't edit anything, and that limit is enforced by Claude Code, not requested in a prompt.
- Permissions. Subagents inherit the parent's permission rules. If the main conversation runs in
bypassPermissions, acceptEdits or auto mode, the subagent runs in that mode and ignores its own permissionMode.
- Disabling one. Add a deny rule such as
"Agent(Explore)" to permissions.deny.
- Nesting. Subagents can spawn subagents up to three layers below the main conversation by default (
CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH changes it; 1 turns it off). To stop one spawning others, leave Agent out of its tools.
- Concurrency. Up to 20 can run at once by default (
CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS).
When they pay off, and when they don't
Anthropic's best-practices page is candid here. Subagents help when you want to:
- run independent pieces of work at the same time;
- keep long output (a full test run, a wide search) out of your main conversation;
- get a reviewer that hasn't seen how the code was written, which is the one place a "second opinion" really is different;
- give a task fewer tools than the main session has.
What they cost is mostly tokens: each subagent sends its own requests, counted against the same usage as your main conversation, and by default it's the same model or a smaller one on the same task. The page also lists "delegate to a subagent for a better answer" among advice you can stop following, because handing the same task to a subagent doesn't make the answer better. A fresh reviewer helps because it hasn't seen your reasoning, which is a different thing from delegating. To spend less, give simple helpers a small model (model: haiku), ask for short summaries back, and check /usage to see which subagent types earn their cost.
Their review prompt example is worth copying: use a subagent to review the rate limiter diff against SPEC.md, check every requirement is implemented and the listed edge cases have tests, report gaps that affect correctness and skip style. Keep "skip style". A reviewer asked to find problems will always find something, and fixing every finding leaves you with extra abstractions and tests for cases that can't happen.
Testing the files
The linter in lesson 5.11 checks the example agents. It requires name and description, rejects a colon in the name, notes fields outside the documented list (so a snake_case slip is reported) and points out a missing tools list. We tested each of these cases, and both example agents pass.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
A developer has just finished a new coupon feature in a long session. Rather than asking the same session whether it's correct, they run use the reviewer subagent to compare the coupon changes against SPEC.md. The reviewer has read-only tools, never saw the implementation discussion, and returns two gaps: expired coupons aren't tested, and the discount isn't rounded to cents. The developer pastes both back into the main session. A separate test-runner agent on a small model handles the full test run, so the thousand lines of passing output never enter the main conversation.
Mistakes to avoid
- Delegating every small task. Each subagent has start-up cost and token cost.
- Expecting a subagent to know what you discussed earlier.
- A vague description, so it's never chosen (or chosen for everything).
- Giving a reviewer edit tools, so it "helpfully" rewrites the code it should only report on.
- Asking a reviewer for "any problems" without scope. Say what to check, and skip style.
Security notes
A subagent can do whatever its tools allow, with your permissions, so restrict them: read-only agents for review and research, no Bash unless needed. Subagents inherit the parent's permission rules and mode, so don't launch a swarm from a bypassPermissions session outside a container. Project agents are committed instructions, so review changes to them like code.
Cost and speed
Parallel subagents finish sooner but use more total tokens. A subagent's own tool calls stay out of your context, which keeps the main session smaller and cheaper per turn, but its work still counts against your usage. Note that the prompt cache for subagents lasts five minutes unless you change it.
Key takeaways
- Subagents run in their own context window; only a summary returns, which keeps your main context clean.
- They start fresh: no history, no invoked skills, no files already read. They do get
CLAUDE.md (except Explore and Plan).
- Define them in
.claude/agents/*.md with name and description; restrict tools for safety, use a small model for simple jobs.
- Use them for parallel work, noisy output and independent review. Don't expect a better answer from simply delegating.
Lesson recap: delegation in, summary out, and what a subagent can and cannot see.
Quick quiz
- What is the main benefit of a subagent?
- Which fields are required in a subagent's frontmatter?
- Does a subagent see your conversation history?
- How do you make a reviewer that can't edit files?
- Why is a fresh reviewer more useful than delegating the same task to another agent?
Answers and explanations
- Context isolation: noisy work happens in a separate window and only a summary comes back.
name and description. The rest are optional.
- No. It starts fresh, with its own prompt, your delegation message and
CLAUDE.md files. A fork is the exception, because it copies the conversation.
- Give it only read tools, for example
tools: Read, Grep, Glob. Claude Code enforces that limit.
- It hasn't seen your reasoning for how the code was written, so it isn't swayed by it. Delegating the same task adds tokens but not intelligence.
Official references and further reading
Related tutorials