Updated: November 30, 2026.
A Claude Code subagent is a worker Claude spawns with its own context window, its own system prompt, and its own tool permissions. The main conversation delegates a task, the subagent does the digging in isolation, and only the summary comes back. I run three of these in every serious project: a code reviewer, a test writer, and a research agent that reads docs so my main thread never has to. Setup is one markdown file per agent.
Why the main thread needs them
Every search result, log dump, and file read in the main conversation stays there for the rest of the session. Ask Claude to trace a bug through fifteen files and your context is full of intermediate output you will never reference again. A subagent takes that hit instead. It burns its own context window on the exploration and returns a few paragraphs of findings.
The official subagents documentation lists what this buys you: preserved context, enforced tool constraints, reusable configuration across projects, and cost control by routing grunt work to a cheaper model. One more fact people miss: subagent requests count toward the same usage limits as your main conversation. A subagent saves context, not tokens.
The file format: markdown with YAML frontmatter
A subagent is a markdown file. The frontmatter configures it, the body becomes its system prompt. Create .claude/agents/code-reviewer.md in your project:
---
name: code-reviewer
description: Reviews code for quality and best practices
tools: Read, Glob, Grep
model: sonnet
---
You are a code reviewer. When invoked, analyze the code and provide
specific, actionable feedback on quality, security, and best practices.
That is the whole format. Only name and description are required. tools is an allowlist, so this reviewer physically cannot edit files, which is exactly what you want from a reviewer. model takes sonnet, opus, haiku, inherit, or a full model ID.
Where you put the file decides who gets it:
| Location | Scope | Use it for |
|---|---|---|
.claude/agents/ | This project, all teammates | Project-specific agents, committed to git |
~/.claude/agents/ | Every project on your machine | Personal agents like a doc reader |
| Managed settings | Whole organization | Company-enforced agents |
Files in watched directories hot-reload within a few seconds, no restart needed. Run /agents to see what loaded. If a file silently fails to appear, the usual cause is a missing description or YAML that does not parse; claude --debug writes the reason to the debug log.
A test-writer agent I actually use
Second example, because the pattern repeats:
---
name: test-writer
description: Writes unit tests for a given file or function. Use after implementing new code.
tools: Read, Glob, Grep, Bash, Write, Edit
model: sonnet
---
You write tests for the code you are given. Match the existing test
style of the repository. Run the tests with the project's test command
and iterate until they pass. Do not modify source files, only test files.
The description is the routing mechanism. Claude reads every subagent's description to decide when to delegate, so write it like a trigger condition, not a job title. "Use after implementing new code" gets the agent invoked automatically; "handles testing" does not.
Keep descriptions short. When the combined descriptions of your custom subagents exceed 15,000 tokens, Claude Code warns you at startup and every one of those tokens is paid on every request. Put the detail in the system prompt body, which only loads when that subagent actually runs.
You already have three built-ins
Before you write anything, know that Claude Code ships with subagents. Explore is a fast read-only agent for codebase searches, Plan does the research behind plan mode, and general-purpose handles multi-step tasks that need both reading and writing. When you ask for a codebase search, Claude is usually delegating to Explore already, and the results never touch your main context.
The built-ins are also your baseline for scope. Explore and Plan start without your CLAUDE.md files to keep them fast and cheap. Your custom agents get the working directory and environment details, plus the system prompt you wrote, not the main conversation's prompt.
When to use a subagent vs the main thread
Use the main thread for anything whose intermediate output you might reference later: implementing the feature you are designing, refactoring code you are actively discussing. Use a subagent for side quests whose output you only need as a summary: research, review, test generation, log triage, dependency audits.
My rule after months of running this: if a task would produce more than a screenful of output that I will not quote back, it belongs in a subagent. Code review is the canonical example. The reviewer reads the diff, checks patterns, and returns findings. If it ran in the main thread, the diff and every file it opened would sit in my context for the rest of the session.
Model routing is the underrated lever
Each subagent can run its own model. Reviewer on sonnet, doc researcher on haiku, everything inherited from the main conversation by default. You can also force one model onto every subagent with the CLAUDE_CODE_SUBAGENT_MODEL environment variable, which you can set permanently in settings.json. I keep that variable wired to a cheap model in my permanent config setup, which means background agents run on subscription-friendly models without me thinking about it. If your goal is cutting cost per request, subagent model routing is the second thing to do after picking a cheaper model.
Invoke it
Ask Claude to delegate, or @-mention the agent by name:
Use the code-reviewer agent on my current changes
In the transcript, the delegation shows as a tool call row with the agent's name, like code-reviewer(Review my changes). If Claude cannot find a new subagent, restart Claude Code once; hot-reload covers edits, but some first-load edge cases need it.
FAQ
What are Claude Code subagents?
Specialized AI assistants that Claude Code spawns with their own context window, system prompt, and tool access. The main conversation delegates a matching task based on the subagent's description, the subagent works in isolation, and only a summary returns to the main context. This is the mechanism that keeps research and review output from filling your main session.
Where do I put Claude Code subagent files?
In .claude/agents/ for a single project (commit it so teammates get the same agents) or ~/.claude/agents/ for all your projects. Both are scanned recursively, and files hot-reload within seconds. Organization-wide agents go through managed settings.
How do I choose the model for a Claude Code subagent?
Set the model field in the agent's frontmatter to sonnet, opus, haiku, inherit, or a full model ID. Omitted, the subagent inherits the main conversation's model. For fleet-wide control, set CLAUDE_CODE_SUBAGENT_MODEL in your settings; subagent model routing is the cheapest way to keep background work off your expensive slot.
What is the difference between a subagent and a skill in Claude Code?
A skill is a set of instructions loaded into the current conversation's context when relevant. A subagent is a separate conversation with its own context window and tool permissions. Use a skill to change how Claude does something in place; use a subagent to keep a task's intermediate output out of your context entirely.
How do I stop Claude Code from using a subagent?
Deny it in permissions: add the Agent tool to permissions.deny to block all delegation, or block a specific built-in by name. To remove only the built-in Explore and Plan agents, set CLAUDE_CODE_DISABLE_EXPLORE_PLAN_AGENTS=1 in your environment. Claude then reads and explores files directly instead of delegating.
Sources
- Claude Code docs: Create custom subagents, read November 30, 2026
- Claude Code docs: Skills, read November 30, 2026
- Claude Code docs: Model configuration (CLAUDE_CODE_SUBAGENT_MODEL), read November 30, 2026



