Claude Code subagents: create and use them
By Maximo Correa · 2026-11-16
A Claude Code subagent is a specialized assistant defined in a Markdown file with YAML frontmatter, saved in .claude/agents/ or ~/.claude/agents/. It runs in its own context window with its own prompt, tools and permissions, and returns only a summary. Claude delegates to it based on its description, or you invoke it by name.
What a Claude Code subagent is
A subagent is a specialized AI assistant that handles one kind of task in its own context window. The official docs say to use one "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 separately and returns only a summary.
Each subagent has a custom system prompt, specific tool access and independent permissions. Claude delegates to it when a task matches its description. Its requests count toward the same usage limits as your main conversation, so a subagent keeps your main context clean without making the work free.
Subagents are plain Markdown files with YAML frontmatter, which makes them easy to share. In Flowy's index there are 11,517 subagent files across 988 repositories (Flowy's index, 2026-11-16, published listings, out of 4,286 repositories). You can browse them in the agents directory.
The built-in subagents
You get working subagents without writing anything. Claude Code includes built-in ones that Claude uses automatically:
| Subagent | What it is for |
|---|---|
| Explore | A fast, read-only agent for searching and analyzing codebases |
| Plan | Codebase research while you are in plan mode. Write and Edit are denied |
| general-purpose | Complex multi-step tasks that need both exploration and action, with every tool available to subagents |
The docs list a few helpers too, such as statusline-setup and claude-code-guide. Explore and Plan start without your CLAUDE.md files and the git status snapshot to keep research fast and inexpensive. Custom subagents load both, and omitClaudeMd drops the CLAUDE.md files from a custom one.
Create a subagent
The quickest route is to ask Claude to write it. The docs' own prompt:
Create a personal code-improver subagent in ~/.claude/agents/ that scans
files and suggests improvements for readability, performance, and best
practices. It should explain each issue, show the current code, and
provide an improved version. Make it read-only and have it use Sonnet.
The result is a file like this one:
---
name: code-improver
description: Scans files and suggests improvements for readability, performance, and best practices. Use after writing or modifying code.
tools: Read, Grep, Glob
model: sonnet
---
You are a code improvement specialist. For each issue you find, explain
the problem, show the current code, and provide an improved version.
The frontmatter is the configuration. The Markdown body becomes the system prompt. A subagent receives only that prompt plus basic environment details like the working directory, not the full Claude Code system prompt.
Where the file goes
| Location | Scope | Priority |
|---|---|---|
| Managed settings | Organization-wide | 1 (highest) |
--agents CLI flag | Current session only | 2 |
.claude/agents/ | Current project | 3 |
~/.claude/agents/ | All your projects | 4 |
A plugin's agents/ directory | Where the plugin is enabled | 5 (lowest) |
When two subagents share a name, the higher-priority location wins. Put project subagents in .claude/agents/ and commit them, so your team uses the same ones. Put personal ones in ~/.claude/agents/. Claude Code watches both directories, so a new file works within a few seconds. The exception is the first agent file in an agents directory that did not exist when the session started: restart once.
The frontmatter fields
Only name and description are required. The ones you will reach for first:
| Field | What it does |
|---|---|
name | Unique identifier, at most 256 characters. Cannot contain : |
description | When Claude should delegate to this subagent |
tools | Tools the subagent can use, comma-separated or a YAML list. Omit it and the subagent inherits every tool available to subagents |
disallowedTools | Tools to remove from the inherited or specified list |
model | sonnet, opus, haiku, fable, a full model ID, or inherit |
permissionMode | default, acceptEdits, auto, dontAsk, bypassPermissions, plan, or manual as an alias for default |
maxTurns | Agentic turns before the subagent stops |
skills | Skills to preload into the subagent's context at startup. The full skill content is injected |
mcpServers | MCP servers available to this subagent |
hooks | Lifecycle hooks scoped to this subagent |
memory | Persistent memory scope: user, project, or local |
One trap sits in this table. Subagents that come from a plugin ignore hooks, mcpServers and permissionMode: the docs say these fields are ignored when loading agents from a plugin, for security reasons. If you need them, copy the agent file into .claude/agents/ or ~/.claude/agents/.
| isolation | Set to worktree to run in a temporary git worktree |
There are more, among them effort, color, background and omitClaudeMd. The full list is in the sub-agents reference.
Why a subagent file silently does nothing
Two behaviors in the docs cost people an afternoon.
First, field names use camelCase and "must match the table exactly: Claude Code ignores a field it doesn't recognize without reporting an error." Write max_turns and your limit does not apply, with no warning in the session. To find a misspelled field, run claude --debug and search the debug log for does not recognize or apply.
Second, Claude Code skips a file without telling you in the session when the frontmatter has one of these problems:
- No
name. The file is treated as documentation kept beside your agents. - An opening
---that is not the file's first line. The file is read as having no frontmatter. - A
namebut nodescription. The file is skipped and the reason goes to the debug log. - YAML that does not parse. No fields are read from the file.
If a subagent you wrote never shows up, check the file against that list before you suspect anything else. For a check before a session, claude plugin validate run against .claude/agents finds files whose frontmatter does not parse.
How Claude picks a subagent
Claude "automatically delegates tasks based on the task description in your request, the description field in subagent configurations, and current context." So the description is the whole routing signal. A vague one ("helps with code") gives Claude no reason to pick it. A specific one that says when to use the subagent, and what it returns, does.
The docs give a direct tip: "To encourage proactive delegation, include phrases like 'use proactively' in your subagent's description field." Their own example uses it: Expert code reviewer. Use proactively after code changes.
Keep descriptions short. Every description sits in Claude's context, and Claude Code shows a startup warning when the combined descriptions of your non-built-in subagents exceed 15,000 tokens. It still loads every subagent. Move detail into the system prompt, which loads only when that subagent runs.
This is the same gap Flowy writes about for skills: a file on disk is not a file in use, and what decides whether Claude reaches for it is the description. For skills that is covered in why installed skills sit dormant and what skill routing is. If a subagent ships inside a plugin, the docs point to claude plugin eval, which runs each prompt with and without the plugin and scores the results, so you can measure delegation across realistic prompts instead of trying one at a time.
Invoke a subagent yourself
When automatic delegation is not enough, the docs describe three patterns that escalate from a suggestion to a default.
Natural language. Name the subagent in your prompt. There is no special syntax, and Claude decides whether to delegate:
Use the test-runner subagent to fix failing tests
Have the code-reviewer subagent look at my recent changes
@-mention. Type @ and pick the subagent from the typeahead. This guarantees that subagent runs for the task:
@"code-reviewer (agent)" look at the auth changes
Your full message still goes to Claude, which writes the subagent's task prompt. The mention controls which subagent runs, not what prompt it receives. You can type the mention by hand as @agent-<name>. For a plugin subagent, use the scoped name, such as @agent-my-plugin:code-reviewer.
Whole session. Start Claude Code as the subagent, so the main thread takes on its tool restrictions and model:
claude --agent code-reviewer
To make that the default for every session in a project, set agent in .claude/settings.json:
{
"agent": "code-reviewer"
}
The CLI flag overrides the setting if both are present.
Subagent, skill or main conversation
The docs split the choice by the shape of the work.
| Situation | Use |
|---|---|
| The task produces verbose output you do not need in your main context | A subagent |
| You want to enforce tool restrictions or permissions | A subagent |
| The work is self-contained and can return a summary | A subagent |
| The task needs frequent back-and-forth or iterative refinement | The main conversation |
| Several phases share significant context, such as planning, implementation and testing | The main conversation |
| You are making a quick, targeted change | The main conversation |
| You want a reusable prompt or workflow that runs in the main conversation context | A skill |
That last row is the line between a subagent and a skill. A skill's instructions join your current conversation. A subagent works in a separate context and reports back. The docs add a latency note: a subagent that is not a fork starts fresh and may need time to gather context, so a quick edit is faster done directly.
For a question about something already in your conversation, the docs recommend /btw over a subagent.
Subagents and hooks
Hooks work with subagents in two ways. Hooks from your settings files, managed policy and plugins also fire inside subagents: a PreToolUse hook in settings.json runs before every tool a subagent uses, and the input carries agent_id and agent_type. Separately, a subagent can carry its own hooks in frontmatter, which run only while that subagent is active. The events SubagentStart and SubagentStop fire when one starts or finishes.
One trust detail: a project subagent's frontmatter hooks are skipped until you accept the workspace trust dialog for the folder that holds the agent file. That rule is stricter than for settings-file hooks. For the event names, the exit-code contract and the safety warning, read the Claude Code hooks guide.
By default a subagent can spawn subagents of its own, up to three layers below the main conversation. At that limit the subagent does its delegated work itself and returns one summary.
Where to find subagents worth reading
Writing one is useful when you keep spawning the same kind of worker. Reading what others wrote first saves time. The agents directory lists the repositories that ship subagents, and plugins, which can bundle subagents, are listed there too. Open the file before you install: the tools line tells you what it can touch, and the description tells you when Claude will pick it.
