Skip to main content

How to Use Claude Code Subagents

Define a subagent once, give it only the tools its job needs, and know when a separate session in its own worktree is the better fit.

Goal​

Create a custom Claude Code subagent, such as a read-only reviewer, that Claude delegates to from your main session. Then decide which work belongs in a subagent and which belongs in a parallel session you review on its own.

As of December 2026, the file paths, fields, and flags below come from the Claude Code subagents docs. Check that page for the full field list.

Prerequisites​

  • Claude Code installed and available as claude on your PATH
  • A repository where you can commit a .claude/agents/ directory

What a subagent is​

A subagent is a Claude instance that the main session starts for one task. It runs in a fresh context window with its own system prompt, tool list, and model. It does not see your conversation history or the files Claude already read. Claude writes a task message when it delegates, and only the subagent's final summary comes back to the main conversation.

Claude Code ships built-in subagents and delegates to them without being asked.

SubagentJobTools
ExploreFast search and analysis of the codebaseNo Edit, Write, or NotebookEdit
PlanGathers context in plan mode before Claude presents a planSame limits as Explore
General-purposeMulti-step tasks that need exploration and actionEvery tool available to subagents

When a subagent pays off​

The Claude Code best practices page lists four cases:

  • Running independent pieces of work at the same time
  • Keeping long output, such as a full test run or a wide search, out of the main conversation
  • Getting a reviewer that has not seen how the code was written
  • Giving a task fewer tools than the main session has

Each subagent sends its own requests, so it costs tokens. A fresh subagent also has to gather context the main session already had, which can make it slower. By default it runs the same model or a smaller one, so delegating gives you parallelism or a fresh pair of eyes, not a better answer.

Create a custom subagent​

A subagent is a Markdown file with YAML frontmatter. Only name and description are required. Save this read-only reviewer as .claude/agents/reviewer.md:

---
name: reviewer
description: Reviews changed files against the spec they were built from. Use after implementing a feature, before opening the PR.
tools: Read, Grep, Glob
---
You're reviewing code you didn't write. Compare the changed files against the spec you're given,
check that every requirement is implemented and the listed edge cases have tests,
and report only the gaps that affect correctness. Skip style.

The tools line is the point. Claude Code enforces it, so this reviewer cannot edit files, whatever the prompt says.

Claude Code reads subagent files from these locations. When two share a name, the higher priority wins.

LocationScopePriority
Managed settingsOrganization-wide1, highest
--agents CLI flagCurrent session2
.claude/agents/Current project3
~/.claude/agents/All your projects4
A plugin's agents/ directoryWhere the plugin is enabled5, lowest

Fields worth setting​

FieldWhat it does
descriptionTells Claude when to delegate. Write it as a trigger, such as "Use after code changes"
toolsAllowlist of tools. Omit it and the subagent inherits every tool available to subagents
disallowedToolsRemoves tools from the inherited or listed set
modelsonnet, opus, haiku, a full model ID, or inherit
isolationSet to worktree to run the subagent in a temporary git worktree
hooksHooks that apply only while this subagent runs
mcpServersMCP servers available to this subagent

Set model: haiku for simple jobs such as collecting the files that import a module. The best practices page recommends it because Haiku costs less per request.

Invoke a subagent​

Claude delegates on its own when a task matches a subagent's description. To pick one yourself, name it in the prompt:

Use the reviewer agent to compare the rate limiter against SPEC.md

You can also type @ and pick the subagent from the typeahead. To run a whole session as a subagent, with its tools and model, start Claude Code with claude --agent reviewer.

For a one-off definition that is not saved to disk, pass JSON with --agents:

claude --agents '{
"code-reviewer": {
"description": "Expert code reviewer. Use proactively after code changes.",
"prompt": "You are a senior code reviewer. Focus on code quality, security, and best practices.",
"tools": ["Read", "Grep", "Glob", "Bash"],
"model": "sonnet"
}
}'

Subagents or parallel sessions​

A subagent shares the main session's working directory unless it sets isolation: worktree. With that field, Claude Code creates a temporary git worktree that branches from your default branch, not from the parent's HEAD. It can remove the worktree if the subagent makes no changes.

Use a subagent when its result feeds back into the task in front of you, such as a search, a test run, or a review. Use a separate session in its own worktree when the work should end as its own branch and pull request. Running multiple Claude Code sessions covers that setup.

Treq is built for the second case. Each task gets a workspace with its own working copy and branch, and you review each diff in the workspace before it merges. A reviewer subagent fits inside one workspace as a first pass before your own review with Claude Code.

Common failure cases​

Claude never delegates. The description is too vague to match a task. Say when to use the subagent, or name it in the prompt.

The subagent repeats work. It starts without your conversation, so it searches for context the main session already had. Put file paths and the definition of done in the task.

Costs climb. Every subagent sends its own requests. Check /usage to see which subagent types take the most, and move simple ones to model: haiku.

Too many nested agents. Subagents can spawn their own subagents, up to three layers by default. Remove Agent from a subagent's tools list to stop it spawning others.