A subagent in Claude Code is a separate Claude instance that your session starts for one focused task. It works in its own context window, with its own instructions and tools. Only its result comes back to you. You define one in a Markdown file with YAML frontmatter, saved under .claude/agents/.
Everything below applies to Claude Code 2.1.330 and is based on the official subagents docs.
What does a subagent get when it starts?
A subagent starts with an empty context window. It doesnât see your conversation history or the files Claude already read. Claude writes it a delegation message that sums up the task. For a custom agent, the body of its file becomes its system prompt. It also loads your CLAUDE.md files, a git status snapshot and the skills named in its skills field.
Only its final report lands in your conversation. Its requests count toward the same usage limits as your session. Since v2.1.198, a subagent started from your main conversation runs in the background by default.
Which subagents are built into Claude Code?
| Agent | Tools | Used for |
|---|
| Explore | Read-only, no Write or Edit | Searching and understanding code |
| Plan | Read-only | Research while in plan mode |
| general-purpose | Every tool available to subagents | Tasks that explore and then change code |
Explore and Plan start without your CLAUDE.md files or the git snapshot, which keeps them fast. Set CLAUDE_CODE_DISABLE_EXPLORE_PLAN_AGENTS=1 (v2.1.198) to remove them, and Claude reads files directly instead.
Where do subagent files go?
| Location | Scope | Priority |
|---|
| Managed settings | Whole organization | 1 |
--agents flag at launch | Current session | 2 |
.claude/agents/ | Current project | 3 |
~/.claude/agents/ | All your projects | 4 |
A pluginâs agents/ directory | Where the plugin is enabled | 5 |
When two subagents share a name, the higher-priority one wins. Claude Code scans .claude/agents/ and ~/.claude/agents/ recursively, so you can sort files into folders like agents/review/. Only the name field identifies the agent, whatever the filename.
Claude Code also watches both folders. A new or edited file is picked up on the next delegation, with no restart. The --agents flag takes JSON definitions that last for one session. With -p, it also accepts a path to a JSON file since v2.1.281.
What does a subagent file look like?
---
name: security-reviewer
description: Reviews changed code for security issues. Use after any change to authentication code.
tools: Read, Grep, Glob
model: sonnet
effort: high
maxTurns: 25
---
You review code for security vulnerabilities.
For each issue, give the file, the line, the risk and a fix.
Don't modify any file.
The frontmatter configures the subagent, and the body becomes its system prompt. Only name and description are required.
Claude decides whether to delegate by reading the description. Write down when the agent should be used. To get Claude to delegate on its own, the docs suggest a phrase like âuse proactivelyâ. Keep it brief, since Claude Code warns at startup once your subagentsâ descriptions together pass 15,000 tokens.
Which frontmatter fields should you know?
| Field | What it does |
|---|
name | Unique ID, up to 256 characters, no : |
description | When Claude should delegate to this subagent |
tools, disallowedTools | Tools allowed, tools removed |
model | sonnet, opus, haiku, fable, a full model ID, or inherit |
effort | low, medium, high, xhigh or max, depending on the model |
permissionMode | default, acceptEdits, auto, dontAsk, bypassPermissions or plan |
maxTurns | Maximum number of turns before the subagent stops |
skills | Skills loaded in full at startup |
mcpServers | MCP servers scoped to this subagent |
hooks | Hooks that run only while this subagent is active |
memory | Persistent memory: user, project or local |
isolation | worktree to work in an isolated copy of the repo |
omitClaudeMd | true to start without your CLAUDE.md files (v2.1.271) |
autoCompactWindow | Compact earlier, from 100,000 tokens up (v2.1.296) |
Multi-word fields use camelCase. A misspelled field is ignored without any error. Since v2.1.296, run claude --debug and search ~/.claude/debug/<session-id>.txt for does not recognize or apply. The log names the fields it didnât recognize.
Without tools, a subagent gets every tool available to subagents, MCP tools included. tools is an allowlist and disallowedTools removes entries. If you set both, Claude Code removes first, then applies the allowlist to what remains.
---
name: local-read-only
description: Analyzes local code without writing files or calling GitHub
disallowedTools: Write, Edit, mcp__github
---
- With
Bash in its tools, a subagent can still change files. Leave it out of a read-only reviewer.
Bash(git push *) in disallowedTools removes the whole Bash tool. To block one command, use permissions.deny in your settings.
- A background subagent keeps its MCP tools and a fixed list of built-in ones, such as Read, Edit and Bash. Most others are dropped without an error.
To stop Claude from using a subagent at all, deny it like a tool.
{
"permissions": {
"deny": ["Agent(Explore)", "Bash(git push *)"]
}
}
How do you pick a subagentâs model?
Without a model field, a subagent runs on your conversationâs model. CLAUDE_CODE_SUBAGENT_MODEL changes that default, and Claude can also pick a model when it starts the subagent. Since v2.1.316, CLAUDE_CODE_SUBAGENT_EFFORT sets the effort level when nothing else does. To match Haiku, Sonnet or Opus to each role, read the post on subagent models.
How do you call a specific subagent?
Claude delegates on its own when your request matches a description. You can also name the agent or @-mention it.
Use the security-reviewer subagent to check my changes
@agent-security-reviewer review the authentication module
To run a whole session as that subagent, with its tools and model, launch claude --agent security-reviewer. Its system prompt then replaces Claude Codeâs own. To make it the default for a project, add "agent": "security-reviewer" to .claude/settings.json.
When should you delegate to a subagent?
Delegate when a task produces a lot of output and you only need the conclusion. Running a test suite or digging through a large log file are good examples. A subagent is also the way to enforce a narrower set of tools or permissions.
Stay in the main conversation when the task needs back-and-forth, or when several steps share a lot of context. Small fixes belong there too, because a subagent starts from zero and has to read the code first. If the side task depends on what you just discussed, start a fork instead, since it inherits your conversation.
What pitfalls should you watch for?
- The file doesnât load. Claude Code skips it with no
name, or with a name but no description. Same if the opening --- isnât on the first line or the YAML doesnât parse. claude plugin validate .claude/agents flags frontmatter that doesnât parse.
- A rule has to reach the subagent. It never sees your conversation. Explore, Plan and agents with
omitClaudeMd donât load your CLAUDE.md files at startup. Repeat the rule that matters in your request.
permissionMode has no effect. If your session runs in bypassPermissions, acceptEdits or auto mode, the subagent uses that mode instead.
- Count limits. By default, Claude canât start another subagent while 20 are already running (
CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS). A subagent can spawn its own, up to three levels below your conversation. CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH=1 turns nesting off.
What changed in recent versions?
- v2.1.63: the Task tool was renamed Agent. Existing
Task(...) rules still work.
- v2.1.198: subagents run in the background by default, and the
/agents wizard is gone. Ask Claude to write the file, or write it yourself.
- v2.1.271: the
omitClaudeMd field.
- v2.1.296: the
autoCompactWindow field, and --debug names unknown fields.
- v2.1.319: in
/tasks, an âEarlier in this sessionâ row reopens finished subagents.
- v2.1.327: a subagent with no
model resumes on the model it started on, even after /model.
Each release is detailed in the Claude Code changelog.
A file in .claude/agents/ can also define a teammateâs role in an agent team. Module 14, tmux and Agent Teams covers when to move from isolated subagents to agents that coordinate with each other.