# 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](https://code.claude.com/docs/en/sub-agents). 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.

| Subagent | Job | Tools |
| --- | --- | --- |
| Explore | Fast search and analysis of the codebase | No `Edit`, `Write`, or `NotebookEdit` |
| Plan | Gathers context in plan mode before Claude presents a plan | Same limits as Explore |
| General-purpose | Multi-step tasks that need exploration and action | Every tool available to subagents |

## When a subagent pays off

The [Claude Code best practices](https://code.claude.com/docs/en/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`:

```markdown
---
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.

| Location | Scope | Priority |
| --- | --- | --- |
| Managed settings | Organization-wide | 1, highest |
| `--agents` CLI flag | 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, lowest |

## Fields worth setting

| Field | What it does |
| --- | --- |
| `description` | Tells Claude when to delegate. Write it as a trigger, such as "Use after code changes" |
| `tools` | Allowlist of tools. Omit it and the subagent inherits every tool available to subagents |
| `disallowedTools` | Removes tools from the inherited or listed set |
| `model` | `sonnet`, `opus`, `haiku`, a full model ID, or `inherit` |
| `isolation` | Set to `worktree` to run the subagent in a temporary git worktree |
| `hooks` | [Hooks](/learn/how-to/claude-code-hooks) that apply only while this subagent runs |
| `mcpServers` | [MCP servers](/learn/how-to/claude-code-mcp) 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:

```text
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`:

```bash
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](/learn/concepts/git/git-worktrees) 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](/learn/how-to/how-to-run-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](/learn/how-to/claude-code-review).

## 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.

## Related

- [Claude Code Best Practices and Tips](/learn/how-to/claude-code-best-practices): when delegation helps and when it only costs tokens
- [How to Use Claude Code Hooks](/learn/how-to/claude-code-hooks): run checks when a subagent stops
- [What is Agent Orchestration?](/learn/concepts/ai-engineering/agent-orchestration): coordinate several agents on one larger task
- [Parallel Coding Agents](/learn/parallel-coding-agents): run agents side by side without sharing a checkout
