Claude Code subagents: when to split work off
What Claude Code subagents are, how to define one in a Markdown file, and when a separate context window keeps your main conversation clean.


Claude Code subagents are worth using when a side task would flood your main conversation with search results, logs, or file contents you won’t reference again. The subagent does that work in its own context and returns only the summary.
This guide covers what a subagent is, the built-in ones, how to write your own as a Markdown file, where to save it, and the limits that apply. It follows the Claude Code subagents documentation and uses the built-in Explore, Plan and general-purpose subagents plus one custom reviewer.
1. Decide if the task needs its own context window
A subagent is a specialized AI assistant that handles one type of task. Each subagent runs in its own context window with a custom system prompt, specific tool access, and independent permissions.
Ask one question: will you read the raw output again? If the answer is no, as with a codebase search or a long log, split it off. If you will keep working with the output, keep it in the main conversation.
Splitting does not save quota. A subagent sends its own requests, which count toward the same usage limits as your main conversation. What you buy is a cleaner main context, not a cheaper session. For what spends your limits, see Claude Code usage limits.
Subagents work within a single session, so other shapes of work need other tools:
| Shape of work | Tool |
|---|---|
| One side task inside a session | A subagent |
| Many parallel sessions monitored from one place | Background agents |
| Sessions that talk to each other | Cross-session messaging |
| A team Claude spawns and supervises | Agent teams |
2. Know the built-in subagents
You may not need to write anything. Claude Code includes built-in subagents that Claude automatically uses when appropriate.
| Subagent | Tools | Use it for |
|---|---|---|
| Explore | Read-only, Write and Edit are denied | File discovery, code search, codebase exploration |
| Plan | Read-only, Write and Edit denied | Codebase research for planning in plan mode |
| General-purpose | Every tool available to subagents | Complex research, multi-step operations, code modifications |
Explore and Plan start without your CLAUDE.md files and the git status snapshot to keep research fast and inexpensive. Other subagents load both. When Claude calls Explore, it picks a thoroughness level:
| Level | Use it for |
|---|---|
| quick | Targeted lookups |
| medium | Balanced exploration |
| very thorough | Comprehensive analysis |
3. Create your first subagent
Define a custom subagent when you keep spawning the same kind of worker with the same instructions. Subagents are Markdown files with YAML frontmatter, and you can ask Claude to write the file for you. In Claude Code, describe it and say where to save it:
# A prompt typed into Claude Code, from the Claude Code docs quickstartCreate a personal code-improver subagent in ~/.claude/agents/ that scansfiles and suggests improvements for readability, performance, and bestpractices. It should explain each issue, show the current code, andprovide an improved version. Make it read-only and have it use Sonnet.Then open the saved file and check that it matches what you asked for. The docs show this result:
---name: code-improverdescription: Scans files and suggests improvements for readability, performance, and best practices. Use after writing or modifying code.tools: Read, Grep, Globmodel: sonnet---You are a code improvement specialist. For each issue you find, explainthe problem, show the current code, and provide an improved version.Try it by asking Claude to use the code-improver agent on your project. The delegation appears as a tool call row showing the subagent’s name followed by a short task description. If Claude can’t find the new subagent, restart Claude Code. This happens only when ~/.claude/agents/ didn’t exist before the session started.
4. Read the frontmatter fields
Only two fields are required, name and description. The rest narrow what the subagent can do.
| Field | Required | What it sets |
|---|---|---|
name | Yes | A unique identifier of at most 256 characters |
description | Yes | When Claude should delegate to this subagent |
tools | No | The tools it can use, as a comma-separated string or a YAML list; every tool is inherited if omitted |
disallowedTools | No | Tools to deny, removed from the inherited or listed set |
model | No | sonnet, opus, haiku, fable, a full model ID or inherit |
permissionMode | No | default, acceptEdits, auto, dontAsk, bypassPermissions, plan or manual |
background | No | true keeps it in the background even when Claude asks to wait |
To keep a worker read-only without listing every allowed tool, deny the writers instead:
disallowedTools: Write, Edit5. Pick where the file lives
The file location decides who can use the subagent. When multiple subagents share the same name, Claude Code uses the one from the higher-priority location.
| Location | Scope | Priority |
|---|---|---|
| Managed settings | Organization-wide | 1 (highest) |
--agents flag | Current session | 2 |
.claude/agents/ | Current project | 3 |
~/.claude/agents/ | All your projects | 4 |
Plugin agents/ directory | Where the plugin is enabled | 5 (lowest) |
Project subagents suit one codebase, and checking them into version control lets your team use and improve them collaboratively. User subagents in ~/.claude/agents/ follow you across projects. If you already keep shared skills in one place, sharing one skills repo between Claude Code and Codex covers that side.
The --agents flag takes the same fields as JSON, for a subagent that lasts one session and writes no file:
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" }}'6. Keep descriptions short and tools narrow
Claude uses each subagent’s description to decide when to delegate tasks, so write a clear one. Descriptions also take up context. When the combined descriptions of your subagents, except the built-in ones, exceed 15,000 tokens, Claude Code shows a warning at startup with the total token count. Trim the description and move detail into the system prompt, which only loads when that subagent runs.
Give your build a human voice
Eleven v4 is ElevenLabs' most emotive voice model yet, for text to speech, voice agents and an API.
Hear Eleven v4Sponsored
Make AI video from inside Claude
Higgsfield has an MCP for Claude, plus Seedance 2.5 and Cinema Studio 4.0. New accounts get up to 50% off for their first 3 hours.
Get up to 50% offSponsored
Two more levers matter:
- Tool limits. You can enforce constraints by limiting which tools a subagent can use, as the read-only reviewer above does.
- Model choice. You can control costs by routing tasks to faster, lower-cost models like Haiku.
To force one model on every subagent, set two environment variables in a settings file:
{ "env": { "CLAUDE_CODE_SUBAGENT_MODEL": "haiku", "CLAUDE_CODE_SUBAGENT_MODEL_FORCE": "1" }}7. Know foreground and background runs
With nothing configured, a subagent that Claude spawns from your main conversation starts in the background. The two modes differ in what the main conversation sees.
| Foreground | Background | |
|---|---|---|
| Main conversation | Blocked until it completes | Keeps running while you work |
| Permission prompts | Passed through to you | Surfaced in the main session, naming the subagent |
| Built-in tools | The full set | A smaller fixed set; every MCP tool is kept |
| Result | Returned in the same turn | A completion notification in a later turn |
8. Stay inside the limits
Four numbers decide how far a set of subagents can grow:
| Limit | Value | Setting that changes it |
|---|---|---|
| Subagents running at once | 20 | CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS |
| Nesting depth | Three layers below the main conversation | CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH |
| Combined description size before a warning | 15,000 tokens | Trim the descriptions |
| Transcript retention | 30 days | cleanupPeriodDays |
At the concurrency limit, spawning another subagent fails with Concurrent subagent limit reached. A subagent can also set its own autoCompactWindow, a token count from 100000 to 1000000.
9. Repeat it for each recurring task
Each time you notice the same side task flooding your main conversation, run the same loop:
- Name the task and confirm you won’t reread its raw output.
- Check whether a built-in subagent already covers it.
- If not, ask Claude to write a subagent with a short description, the fewest tools it needs and a model sized to the job.
- Save it in the project folder if the team shares it, or in your user folder if only you do.
- Ask Claude to use it by name and watch for the tool call row.
Verdict
Split work off when the output is bulky and you won’t reference it again, and keep it inline when you will. Expect a cleaner main context, not a lighter bill, because subagent requests count toward the same usage limits. Start with the built-in Explore and Plan subagents, then write your own once you repeat the same instructions.
Sources:Claude Code docs: Create custom subagents.
Responses
Responses
Community guidelines
Write about the post. Be kind and be specific.
Responses are plain text. Spam, abuse and anything unlawful are removed, and the person who posted them may be blocked.
A response may wait for the owner to read it before it appears. Use Report on anything that should not be here.
Give your build a human voice
Eleven v4 is ElevenLabs' most emotive voice model yet, for text to speech, voice agents and an API.
Hear Eleven v4Sponsored
Make AI video from inside Claude
Higgsfield has an MCP for Claude, plus Seedance 2.5 and Cinema Studio 4.0. New accounts get up to 50% off for their first 3 hours.
Get up to 50% offSponsored
Get the next one
New posts by email, when they are ready. Confirm once and you are on the list.
Check your inbox
We sent a link to . Click it and you are on the list. Nothing is sent to an address that has not confirmed itself.
Keep reading


Claude Code context window: what fills it, how to free it
The Claude Code context window fills with memory files, history and tool output. /clear starts a new session, and a shorter CLAUDE.md cuts the fixed load.


Claude model training data: what the help article says
Anthropic's help article says commercial inputs are not used for training by default, but feedback can be. Here is what to check in Claude Code.


Generative UI agent instructions: 2,326 vs 12,720 tokens
Generative UI needs instructions that teach an agent to build cards. A short block cost 2326 tokens, a full guide 12,720, so keep the short block.

