Claude Code Agents: A Practical Guide to Subagents

•Pate Bryant

A Claude Code subagent is a helper that runs in its own context window, with its own system prompt and its own tool permissions. You define one in a Markdown file with YAML frontmatter, put it in .claude/agents/ (one project) or ~/.claude/agents/ (all your projects), and Claude hands it work when a task matches its description. You can also call one by name. Its results come back to the main conversation; the files it read and the commands it ran stay in its own context.

Everything below is checked against Anthropic's Claude Code subagents documentation. Claude Code changes quickly, so treat that page as the final word if anything here has moved.

What a subagent is

The docs describe subagents as specialized assistants for specific kinds of task. Each one runs in its own context window, with a custom system prompt, specific tool access and your permission rules. Three things follow from that:

  • Separate context. A subagent starts fresh. It does not see your main conversation, only the task it was given, and only its result comes back. The exception is a fork, which Claude Code can start on its own (or you can, with /subtask when agent view is on or /fork otherwise): it inherits the whole conversation, though its tool calls still stay out of your context. A search that reads forty files costs the main conversation the length of the answer, not the forty files.
  • Its own system prompt. The Markdown body of the definition file becomes the subagent's system prompt. It gets that prompt plus basic environment details such as the working directory, not the full Claude Code system prompt.
  • Its own tools. You can give a subagent a short allowlist of tools, or remove specific tools, so a reviewer cannot edit files and a researcher cannot run shell commands.

Subagent requests count toward the same usage limits as the main conversation, so a subagent is a way to organize work, not free capacity.

Claude Code also ships with built-in subagents, such as Explore for fast codebase search without file edits, Plan for research in plan mode and general-purpose for multi-step tasks. The rest of this page is about writing your own.

Where subagent files live

A subagent is one Markdown file. Where you put it sets who can use it:

LocationScopeUse it for
.claude/agents/The current projectAgents tied to one codebase; commit them to git
~/.claude/agents/Every project on your machinePersonal agents you want everywhere
--agents CLI flagThe current session onlyTrying a definition out without saving a file
A plugin's agents/ dirWherever that plugin is enabledAgents distributed as part of a plugin
Managed settingsThe whole organizationAgents an administrator sets for every user

When two subagents share a name, the higher-priority location wins. From highest to lowest: managed settings, the --agents flag, the project's .claude/agents/, your user ~/.claude/agents/, then plugins. So a project agent called code-reviewer overrides a personal one with the same name.

Both agent folders are scanned recursively, so you can group files in subfolders. The subfolder does not change the agent's identity; only its name field does. The docs say edits to files in either folder are picked up within seconds, without a restart. The exception is agents in a directory added with --add-dir: Claude Code doesn't watch those, so restart after adding or editing one there.

The definition file

The file is YAML frontmatter between --- lines, then the system prompt. Only name and description are required. Here is a complete reviewer that can read the code and run git, but cannot change anything:

---
name: code-reviewer
description: Reviews a diff for bugs, security problems and unclear code. Use proactively after any code change, before committing.
tools: Read, Grep, Glob, Bash
model: sonnet
color: blue
---

You are a senior code reviewer. You review; you never edit files.

When invoked:

1. Run `git diff HEAD` to see what changed. Review only those changes.
2. For each changed file, read enough surrounding code to judge the change.
3. Look for, in this order: incorrect behaviour, missing error handling,
   security problems (secrets, injection, unchecked input), then readability.

Report findings ranked most severe first. For each one give the file and
line, what is wrong, and a concrete fix. If you find nothing, say so in one
line. Do not pad the report with praise or a summary of the diff.

Frontmatter fields

These are the main fields the docs list today. Field names are case-sensitive, and a field Claude Code does not recognize is ignored without an error, so a typo fails silently.

FieldRequiredWhat it does
nameYesUnique identifier. It cannot contain :. The file name does not have to match it.
descriptionYesWhen Claude should delegate to this agent. This is what automatic delegation reads.
toolsNoComma-separated list (or YAML list) of allowed tools. Leave it out and the agent inherits every tool available to subagents.
disallowedToolsNoTools to remove from the inherited or listed set.
modelNosonnet, opus, haiku, fable, a full model ID, or inherit to use the model of whoever spawned it.
permissionModeNodefault (or its alias manual), acceptEdits, auto, dontAsk, bypassPermissions or plan. Inherits the main session's mode if unset, and some main-session modes override it.
maxTurnsNoCaps how many turns the agent takes before it returns a partial result.
skillsNoSkills whose full content is loaded into the agent at startup.
mcpServersNoMCP servers available to this agent, by name or as inline definitions.
hooksNoLifecycle hooks that apply only while this agent runs.
memoryNouser, project or local: gives the agent a persistent memory folder.
backgroundNotrue keeps the agent in the background even when Claude asks to wait for it. Custom subagents already start in the background by default.
effortNoReasoning effort: low, medium, high, xhigh or max, depending on the model.
isolationNoworktree runs the agent in a temporary git worktree, so its edits don't touch your checkout.
colorNoDisplay color in the UI: red, blue, green, yellow, purple, orange, pink or cyan.

The docs list a few more specialized fields, some tied to recent Claude Code versions. Check the frontmatter reference before relying on one.

A second example, a test runner allowed to edit code but kept on a cheaper model and a short leash:

---
name: test-runner
description: Runs the test suite, diagnoses failures and fixes them. Use when tests fail or after changing code that has tests.
tools: Read, Edit, Grep, Glob, Bash
model: haiku
maxTurns: 20
---

You run tests and fix the failures they reveal.

1. Run the project's test command. If you cannot find it, report that and stop.
2. For each failure, find the cause before changing anything.
3. Fix the code under test, not the test, unless the test is plainly wrong.
   Never delete or skip a test to make the suite pass.
4. Re-run the full suite.

You are done when the suite passes. Report the command you ran, the final
pass and fail counts, and one line per fix explaining what was wrong.

Creating a subagent

The simplest way is to write the file yourself: create .claude/agents/code-reviewer.md (or the same file under ~/.claude/agents/) with the frontmatter and prompt above. You can also ask Claude in the session to write a subagent for you and then edit the file.

The /agents command has changed between versions. On v2.1.197 and earlier it opened an interactive manager for creating, editing and deleting subagents. The current docs say it now points you to asking Claude or editing the .claude/agents/ and ~/.claude/agents/ folders directly. Either way, the result is the same Markdown file.

To try a definition without saving it, pass it as JSON for one session:

claude --agents '{"code-reviewer": {"description": "Reviews diffs for bugs. Use proactively after code changes.", "prompt": "You are a senior code reviewer. Report findings by severity.", "tools": ["Read", "Grep", "Glob", "Bash"], "model": "sonnet"}}'

How Claude decides to delegate

According to the docs, Claude delegates automatically based on three things: what your request asks for, each subagent's description, and the current context. The description is the lever you control. Write it as a trigger, not a job title:

  • Weak: description: Code reviewer.
  • Better: description: Reviews a diff for bugs and security problems. Use proactively after any code change.

The docs suggest phrases like "use proactively" to encourage Claude to delegate without being asked. Keep descriptions short and distinct; if two agents' descriptions overlap, Claude has no clear reason to pick one.

Invoking a subagent explicitly

You don't need special syntax. Name the agent in plain language:

  • "Use the test-runner subagent to fix the failing tests."
  • "Have the code-reviewer subagent look at my recent changes."

You can also @-mention it. Type @ and pick the agent from the typeahead, or type @agent-code-reviewer yourself. To run a whole session as one agent, start Claude Code with claude --agent code-reviewer.

When subagents help, and when they don't

A subagent costs something before it does anything: it starts from an empty context and has to rebuild what the main conversation already knows from the task you hand it. It pays off when it returns more than that.

SituationSubagent?Why
Several independent pieces of workYesSeparate agents can work on disjoint slices at the same time.
A search or read across many filesYesThe file contents stay in the subagent; only the answer comes back.
Reviewing work the main conversation just wroteYesA fresh context isn't anchored on the reasoning that produced the code.
A risky or noisy job (long logs, large test output)YesVerbose output stays out of your main context.
A one-line fix or a single known commandNoWriting the brief takes as long as doing it.
A follow-up on a file already open in the conversationNoThe main conversation already has the context the agent would rebuild.
Work that needs a back-and-forth with youNoUnless it's a fork, the agent can't see the conversation, so every answer means a handoff.

A rough rule: if you could finish the task in two or three tool calls yourself, don't delegate it.

Writing tight subagent briefs

Most bad subagent runs come from loose instructions, not a weak model. A subagent only knows what its system prompt and its task tell it, so both should be specific.

Give each agent a narrow scope. One agent, one job. The docs' own advice is to design focused subagents that each excel at one task. In the task itself, say which files or folders it may touch and what is out of bounds. "Review the changes in src/auth/" finishes; "look at the auth code" wanders.

State a done criterion. Tell the agent exactly what ends the job, ideally something it can check: "done when npm test passes", "done when you have listed every caller of parseConfig". Without one, an agent either stops early or keeps going. Ask for the evidence in the report: the command, the counts, the file and line.

Restrict its tools. Grant only what the job needs. The docs recommend limiting tool access for both security and focus. A reviewer gets Read, Grep, Glob, Bash and no Edit. A researcher gets read tools only. Remember that leaving tools out entirely gives the agent everything subagents can use.

Pass along what you already know. A custom subagent can't see your conversation; only a fork inherits it. If you've already found the bug's file, ruled out a cause or picked an approach, put it in the task so the agent doesn't spend its turns rediscovering it.

Write the description last. Once the prompt says what the agent does, write a description that says exactly when to use it, so it doesn't collide with your other agents.

Commit project agents. Files in .claude/agents/ are ordinary project files. Checking them into git, as the docs suggest, gives your whole team the same reviewer and test runner.

FAQ

What is the difference between Claude Code agents and subagents?

In Claude Code, the custom agents you define in .claude/agents/ are called subagents: helpers that the main conversation delegates to. The main Claude Code session is the agent you talk to; subagents are the specialists it hands work to.

Where do I put Claude Code subagent files?

In .claude/agents/ at the root of your project for agents tied to that codebase, or in ~/.claude/agents/ for agents available in every project. If both define the same name, the project agent wins.

Do subagents share context with the main conversation?

Not by default. Each custom or built-in subagent starts with a fresh context window and does not see the main conversation. It gets its own system prompt and the task it was given, and only its result returns to the main conversation. A fork is the exception: it inherits the conversation so far, though its own tool calls still stay out of the main context and only its final result comes back.

What happens if I leave out the tools field?

The subagent inherits every tool available to subagents. To limit it, list the tools you want in tools, or remove specific ones with disallowedTools.

Can a subagent use a different model?

Yes. Set model to sonnet, opus, haiku, fable, a full model ID, or inherit. A cheaper model suits simple, high-volume jobs such as running tests or searching code.

How do I make Claude use my subagent automatically?

Write a description that says when the agent should be used, and include a phrase like "use proactively". If Claude still doesn't pick it, ask for it by name or @-mention it.

Related

Claude and Claude Code are trademarks of Anthropic. This page is not affiliated with or endorsed by Anthropic.