How to Create a Custom Subagent in Claude Code (Step by Step)
To create a custom subagent in Claude Code, save a Markdown file with YAML front matter in .claude/agents/ (this project) or ~/.claude/agents/ (all your projects). The file needs a name and a description. Everything below the front matter becomes the subagent's system prompt.
This guide walks through the whole task: where the file goes, which fields you can set, how to write the prompt, how to test it, and how to share it. If you want the concept first, read Claude Code subagents explained. For the full picture of agents, skills and sessions, see the Claude Code subagents hub.
What you need before you start
- Claude Code installed and working in a terminal, the desktop app or an IDE extension.
- A folder to work in. For a project subagent this is your repository root.
- A text editor. A subagent is just a file.
Step 1: Choose where the file lives
Claude Code reads subagents from several places. When two subagents share a name, the higher row wins.
| Location | Scope | Priority |
|---|---|---|
| Managed settings | Whole organization | 1 (highest) |
--agents CLI flag | Current session only | 2 |
.claude/agents/ | Current project | 3 |
~/.claude/agents/ | All your projects | 4 |
A plugin's agents/ folder | Where the plugin is enabled | 5 (lowest) |
For most people the choice is simple:
- Use
.claude/agents/when the agent belongs to one codebase and your team should share it. - Use
~/.claude/agents/when the agent is personal and useful everywhere, such as a general code reviewer.
On Windows, ~ means your user folder, so the personal folder is %USERPROFILE%\.claude\agents. If you set the CLAUDE_CONFIG_DIR environment variable, the personal folder moves there.
Project subagents are found by walking up from your working directory. If nested .claude/agents/ folders define the same name, the one closest to the working directory wins. Subfolders inside agents/ are allowed and do not change a subagent's identity, because identity comes only from name.
Create the folder if it does not exist:
mkdir -p .claude/agents
On Windows PowerShell:
New-Item -ItemType Directory -Force .claude\agents
Step 2: Write the front matter
Create a file such as .claude/agents/code-reviewer.md. The file starts with a block between two --- lines. The opening --- must be the first line of the file.
---
name: code-reviewer
description: Reviews code for quality and best practices. Use proactively after code changes.
tools: Read, Grep, Glob
model: sonnet
---
Only name and description are required. The most useful fields:
| Field | Required | What it does |
|---|---|---|
name | Yes | Unique identifier, up to 256 characters. It cannot contain : or start with -. |
description | Yes | Tells Claude when to delegate to this subagent. |
tools | No | Allow-list, as a comma-separated string or YAML list, for example Read, Grep, Glob. If you omit it, the subagent inherits all tools available to subagents. |
disallowedTools | No | Tools to remove from the inherited or listed set, for example Write, Edit. |
model | No | sonnet, opus, haiku, fable, a full model ID, or inherit. |
permissionMode | No | default, acceptEdits, auto, dontAsk, bypassPermissions, plan or manual. |
maxTurns | No | Maximum number of agentic turns. |
skills | No | Skills whose full content is loaded into the subagent at startup. |
mcpServers | No | MCP servers available to this subagent. |
hooks | No | Lifecycle hooks that run only while this subagent is active. |
memory | No | user, project or local: gives the subagent a persistent memory folder. |
isolation | No | worktree runs the subagent in a temporary git worktree. |
color | No | red, blue, green, yellow, purple, orange, pink or cyan. |
Multi-word fields use camelCase (disallowedTools, maxTurns). Claude Code ignores fields it does not recognize, so a typo in an optional field fails silently.
Claude Code skips a file entirely if it has no name (it is treated as documentation), has an invalid name, has a name without a description, or has YAML it cannot parse. A skipped file is the first thing to check when an agent does not show up.
Step 3: Write a description that routes work correctly
The description is the most important line in the file. Claude reads it to decide whether to hand a task to your subagent.
- State what the agent does and when to use it: "Reviews code for quality and best practices. Use proactively after code changes."
- Phrases such as "use proactively" encourage Claude to delegate without being asked.
- Keep it short. If the combined descriptions of your non-built-in subagents exceed 15,000 tokens, Claude Code shows a startup warning. Put detail in the system prompt instead, because it loads only when the subagent runs.
- Make each description different enough that Claude can tell your agents apart.
Step 4: Write the system prompt
Everything after the closing --- is the system prompt. The subagent receives this prompt plus basic environment details, not the full Claude Code system prompt. It also does not see your conversation history, so write the prompt as if the reader knows nothing about your session.
A solid prompt contains:
- A one-line role.
- What to do when invoked, as numbered steps.
- The checks or rules to apply.
- The output format you want back.
Here is a complete file based on the official docs' code-reviewer example:
---
name: code-reviewer
description: Expert code review specialist. Proactively reviews code for quality, security, and maintainability. Use immediately after writing or modifying code.
tools: Read, Grep, Glob, Bash
model: inherit
---
You are a senior code reviewer ensuring high standards of code quality and security.
When invoked:
1. Run git diff to see recent changes
2. Focus on modified files
3. Begin review immediately
Review checklist:
- Code is clear and readable
- Proper error handling
- No exposed secrets or API keys
- Input validation implemented
Provide feedback organized by priority:
- Critical issues (must fix)
- Warnings (should fix)
- Suggestions (consider improving)
Note that leaving out Edit and Write does not make an agent fully read-only: this reviewer can still change files through Bash. If it must never write, drop Bash too, or add a PreToolUse hook that blocks writes.
Step 5: Choose tools and a model
Give each subagent only the tools its job needs. A researcher can use Read, Grep, Glob. A debugger needs Edit as well.
- Allow-list:
tools: Read, Grep, Glob, Bash - Deny-list:
disallowedTools: Write, Edit - Stop it spawning other subagents: leave
Agentout oftools, or add it todisallowedTools.
For the model, Claude Code resolves it in this order:
- The per-invocation
modelparameter (aliases only). - The
modelfield in your file. - The
CLAUDE_CODE_SUBAGENT_MODELenvironment variable. - The model of the conversation that started the subagent.
Routing simple work to haiku is the official way to lower the cost of a subagent. The subagent's requests still count toward your usage. More detail is in Subagent tool permissions and model selection.
Step 6: Let Claude pick up the file
Claude Code watches ~/.claude/agents/ and .claude/agents/. When you add or edit a file, it detects the change within a few seconds and the next delegation uses the updated definition. No restart is needed. The exception is a folder added with --add-dir: changes there need a restart.
If two files in the same folder share a name, only one loads, and /doctor reports the clash.
Step 7: Test the subagent
Start a session in your project and invoke the agent in one of these ways:
- Plain language: "Use the code-reviewer subagent to look at the auth changes."
- @-mention:
@agent-code-reviewer look at the auth changes - Whole session as that agent:
claude --agent code-reviewer - Project default: add
{ "agent": "code-reviewer" }to.claude/settings.json. The CLI flag overrides it.
Then check three things:
- Did the subagent run, or did Claude answer in the main conversation?
- Did it stay inside its tool list?
- Did the result come back in the format you asked for?
Subagent transcripts are stored at ~/.claude/projects/{project}/{sessionId}/subagents/agent-{agentId}.jsonl, which is useful if you need to see what happened. If the agent never runs, see why Claude Code is not using your subagent.
Step 8: Share it with your team
Commit .claude/agents/ to git. Everyone who clones the repository gets the same subagents, and any change is reviewed like code. Keep personal experiments in ~/.claude/agents/ so they stay out of the repository.
Optional: a session-only subagent with --agents
To try an agent without saving a file, pass JSON on the command line. Each key is the agent name, and prompt holds the system prompt:
claude --agents '{
"debugger": {
"description": "Debugging specialist for errors and test failures.",
"prompt": "You are an expert debugger. Analyze errors, identify root causes, and provide fixes."
}
}'
These agents exist only for that session. They are not saved to disk.
Optional: ask Claude to write the file
The official docs say you can ask Claude to write a subagent for you. Describe the role, the tools it should have and where to save it. Claude Code detects the new file within a few seconds. Read the result before you rely on it.
About the /agents command
The behavior depends on your version. On Claude Code v2.1.197 and earlier, /agents opens an interactive wizard with a Running tab and a Library tab. On later versions it prints a reminder to ask Claude or to edit .claude/agents/ and ~/.claude/agents/ directly. Because the file method works everywhere, this guide uses it.
Seeing your subagents at work with Agent Office
Once you have several subagents, it gets hard to tell which ones actually run, how long they take and how many tokens they use. Agent Office is a free local dashboard that reads your ~/.claude/agents folder and your session transcripts and shows each subagent as a staff member on a live map. It reads tool names, task titles, status and token counts, never your message text. Note that it lists agents from ~/.claude/agents. A project agent from .claude/agents/ shows up when a session calls it, but as a "contractor" because the service has no local definition for it. To try it, follow the setup guide and then open the Office.
Quick checklist
- File ends in
.mdand sits in.claude/agents/or~/.claude/agents/. - The first line is
---. nameis unique and has no:.descriptionsays what the agent does and when to use it.toolslists only what the job needs.- The prompt explains the steps and the output format.
- You tested it by name.
Sources
- Create custom subagents (official docs): https://code.claude.com/docs/en/sub-agents
- Explore the .claude directory: https://code.claude.com/docs/en/claude-directory
- Manage costs effectively: https://code.claude.com/docs/en/costs
Frequently asked questions
Where do Claude Code subagents live?
Project subagents live in .claude/agents/ inside your project. Personal subagents live in ~/.claude/agents/ (on Windows, %USERPROFILE%\\.claude\\agents) and work in all your projects. Both folders are scanned recursively, and Claude Code watches them, so a new file is picked up within a few seconds.
What goes in the front matter of a subagent file?
Only name and description are required. Optional fields include tools, disallowedTools, model, permissionMode, maxTurns, skills, mcpServers, hooks, memory, background, effort, isolation and color. Fields Claude Code does not recognize are silently ignored.
How do I call a subagent by name?
Ask in plain language (for example, 'Use the test-runner subagent to fix failing tests'), type an @-mention such as @agent-code-reviewer, or start a whole session as that agent with claude --agent code-reviewer.
Can a subagent use its own model?
Yes. Set model to sonnet, opus, haiku, fable, a full model ID, or inherit. If you omit it, the subagent runs on the same model as the conversation that starts it.
Why does Claude never use my subagent?
Claude decides based on your request and the description field, so a vague description is the most common cause. Also check that the file has a valid name and description, that the opening --- is the first line, and that a higher-priority subagent with the same name is not winning.
How do I share subagents with my team?
Save them in .claude/agents/ and commit that folder to git. Anyone who clones the repository gets the same subagents. Use ~/.claude/agents/ only for agents that are personal to you.
Do I need the /agents command to create a subagent?
No. On current versions, /agents only prints a reminder to ask Claude or edit the agent folders directly. On Claude Code v2.1.197 and earlier it opens an interactive wizard. Writing the Markdown file yourself works on every version.