Tutorial
Subagents in Claude Code: delegate without losing control
Define Claude Code subagents with their own tools, model and permissions, keep noisy work out of your main context, and avoid conflicts in parallel runs.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
A subagent is a separate Claude instance that your main session hands a task to. It gets its own context window, its own system prompt, and its own set of tools. When it finishes, only its final summary comes back. That makes subagents good for two things: keeping noisy work (searches, logs, test output) out of your main conversation, and running a specialised role with tighter permissions than the session that called it.
This tutorial defines two subagents, shows the ways to invoke them, and covers the problems that appear when several run at once. Checked against the Claude Code subagents documentation on 20 November 2026.
What you get for free
Claude Code ships with built-in subagents and uses them without being asked:
- Explore: fast and read-only, for searching and analysing a codebase.
- Plan: read-only research used in plan mode before it presents a plan.
- general-purpose: has every tool, for multi-step tasks that need to both explore and change things.
You will see these appear in the transcript as Claude delegates. Your own subagents sit alongside them.
1. A test-runner subagent
Test runs produce a lot of output, almost none of which you need in the main conversation. Create .claude/agents/test-runner.md:
---
name: test-runner
description: Runs the project's test suite and reports failures concisely. Use proactively after code changes, or when asked to run tests.
tools: Bash, Read, Grep, Glob
model: haiku
---
You run tests and report results. You never edit files.
1. Run the tests with `npm test -- --reporter=dot`. If a path or pattern was
given, run only those tests.
2. If everything passes, reply with one line: the number of tests passed.
3. If anything fails, reply with, for each failing test:
- the test name and file
- the first assertion error, verbatim, in at most five lines
- your best guess at the cause in one sentence, labelled as a guess
4. Do not attempt fixes. Do not summarise passing tests.
What each field does:
nameanddescriptionare required. The description is how Claude decides when to delegate, so say what it does and when to use it. “Use proactively” encourages automatic use.toolsis an allowlist. This agent can read and run commands but has no Edit or Write tool, so it cannot change code even if it wanted to.model: haikuruns it on a cheaper, faster model. Running tests and reporting errors does not need your most capable model. Omitmodelto inherit the session’s.- The body is the subagent’s system prompt. It does not see your conversation, so it must be self-contained.
Project agents in .claude/agents/ are shared through Git. Put personal ones in ~/.claude/agents/. Claude Code watches these folders, so changes apply on the next delegation without a restart.
2. A read-only reviewer
A second pair of eyes is more useful when it has not been part of the conversation that produced the code. Create .claude/agents/reviewer.md:
---
name: reviewer
description: Reviews uncommitted changes for bugs, missing tests and risky patterns. Use when asked for a review, or before committing a non-trivial change.
tools: Read, Grep, Glob, Bash
permissionMode: plan
---
You review the current uncommitted changes. Run `git diff` to see them.
Report, in order of severity:
1. Bugs: logic errors, unhandled cases, wrong assumptions about inputs.
2. Tests: behaviour changed without a test, or tests that would pass without the change.
3. Risk: security issues, data loss, breaking API changes.
For each finding give file:line, what is wrong, and why it matters.
If you find nothing significant, say so in one line. Do not restate the diff.
It has no Edit or Write tool, and permissionMode: plan keeps its shell use to read-only commands such as git diff. The fresh context is a feature here: the reviewer reads the diff as a stranger would, without the main session’s assumptions about what the code was meant to do.
3. Invoke them
There are four ways:
- Automatically. Ask “run the tests” and Claude may delegate to
test-runnerbecause the description matches. - By name in plain language: “use the reviewer subagent on my changes”.
- By @-mention: type
@and pick the agent, or write@agent-reviewer. - For the whole session:
claude --agent reviewerstarts a session that runs as that agent.
Run /agents for a reminder of where agent files live. Ask Claude to draft a new subagent for you, then edit the file it writes; it is quicker than starting from blank.
What the subagent sees, and what comes back
This is the part people get wrong. A subagent starts with a fresh context containing its system prompt, the message Claude writes when delegating, your CLAUDE.md files and a Git status snapshot. It does not see your conversation, the files already read, or anything you said earlier.
So the quality of a delegation depends on the delegation message. If you want the reviewer to focus on the payment code, say so in the request, because the reviewer cannot infer it from a conversation it never saw.
On the way back, only the final message returns. The subagent’s tool calls and intermediate output stay in its own transcript. That is what keeps your main context small, and it is also why the output format in the system prompt matters: a vague subagent returns a vague summary.
Running several at once
Claude can run subagents in parallel, in the foreground or in the background. Parallel read-only work is safe and fast: three Explore agents searching different parts of a monorepo do not interfere with each other.
Parallel editing is where it goes wrong. Two subagents changing the same files in the same working tree will overwrite each other’s edits or produce a mix neither intended. Options, from simplest:
- Split by file ownership. Delegate tasks that touch disjoint files, and say so in each delegation message.
- Serialise the writes. Let parallel agents investigate and propose; have one agent (or the main session) apply changes.
- Isolate with worktrees. Set
isolation: worktreein a subagent’s frontmatter and it works in its own Git worktree, on its own copy of the files. You merge the results afterwards, which turns silent conflicts into visible merge conflicts. The parallel refactor with Git worktrees workflow covers the merge side.
Control and cost
Each subagent makes its own model calls, which count towards your usage. Some ways to keep that in check:
- Use
model: haikuorsonnetfor mechanical roles (running tests, searching, formatting reports). - Give
maxTurnsto subagents that could loop, so they stop and return what they have. - Keep system prompts short and specific. Every subagent also loads your
CLAUDE.md; setomitClaudeMd: truefor agents that do not need project rules. - Prefer one well-briefed subagent to five vague ones.
You can also restrict delegation with permission rules: Agent(reviewer) in a deny list stops Claude from using that subagent, and Agent(model:opus) stops subagents from requesting the most expensive model tier.
Subagent, skill or just a prompt?
- Use a skill when you want to give Claude know-how in the current context: how to write a migration in your codebase, your release checklist.
- Use a subagent when the work should happen out of sight with its own tools and permissions, and only the result matters.
- Use neither for a one-off. Just ask.
Skills vs MCP vs subagents compares them in more depth.
Pitfalls
Expecting shared memory. The subagent does not know what you discussed. Put the needed context in the delegation, or in a file it can read.
Over-broad tools. A reviewer with Edit access will eventually “just fix” something. Grant only what the role needs.
Vague descriptions. If Claude never delegates automatically, the description is not matching how you phrase requests. Include the words you actually use.
Parallel writes in one tree. Use file ownership or worktree isolation, never hope.
Next steps
- Keep long sessions lean with Agent memory and compaction.
- Weigh the cost of more agents against the gain in multi-agent systems: when one agent is not enough.
- Try plan-execute-review loops with the subagent-driven-development skill.
- Run reviews on pull requests with Code review with Claude Code.
- New to the tool? Start with the Claude Code tutorial for your first session.
- Enforce rules on what subagents run with Claude Code hooks.
Covers
- Tools
Check your understanding
Subagents in Claude Code quiz
Defining subagents, tool allowlists and models, what a subagent sees and returns, invocation, and parallel work without conflicts.
7 questions · pass mark 70%
Review
Related
- Skills vs subagents vs slash commands vs hooks
Skills vs subagents, slash commands, hooks, MCP servers and AGENTS.md: when to reach for each in a coding agent, with a decision table and a worked example.
- Agent memory and compaction in practice
Keep long Claude Code sessions accurate with compaction, CLAUDE.md, auto memory, subagents and a summarise-and-restart routine, and know what survives each.
- Refactor in parallel with git worktrees and two agents
Use git worktrees to run Claude Code and Codex in parallel on the same refactor from one written plan, compare tests, diff size and readability, then merge one.
- Subagent-driven development
Executes an implementation plan by handing each task to a fresh subagent, then reviewing its work before moving on. Best in agents with subagents.
- Subagent
A separate agent instance with its own context window, launched by a main agent to handle a focused task and report back a short result.