Skip to content

How subagents work in Claude Code, and how to define your own

On this page

Claude Code subagents are separate Claude instances the main conversation hands a task to. Each runs in its own context window with its own system prompt, tool list, model and permission mode, and only its final report comes back. Claude Code ships several built-in ones (Explore, Plan, general-purpose), and you define your own as Markdown files in .claude/agents/ or ~/.claude/agents/. Use them to keep noisy work out of your main context and to run a task with fewer tools than the main session has.

Checked as of November 2026 against Anthropic’s subagents documentation and cost guide. Subagent behavior changes often between Claude Code releases, so treat the docs as the reference.

What a subagent is

When Claude decides a task fits a subagent, it calls the Agent tool (called Task before v2.1.63; old Task(...) rules still work as aliases) with a delegation prompt it writes itself. The subagent starts fresh. It doesn’t see your conversation history, the files Claude already read, or skills already invoked. Its initial context is:

  • its own system prompt, plus environment details, instead of the Claude Code system prompt;
  • the task message Claude wrote;
  • your CLAUDE.md files (except for Explore and Plan, or when the definition sets omitClaudeMd: true);
  • a git status snapshot;
  • any skills listed in its skills field.

It works through its own tool calls, then returns one report. That report is all the main conversation sees. Anything the subagent read along the way stays in its transcript under ~/.claude/projects/.

Two consequences follow. The delegation prompt is the only briefing the subagent gets, so a vague request produces a vague result. And a rule that only lives in your conversation (“ignore vendor/”) has to be restated when delegating, or put in CLAUDE.md.

A fork is the exception. A fork inherits the whole conversation, system prompt, tools and model, and reuses the parent’s prompt cache. You can start one with /subtask followed by a task.

Built-in subagents

SubagentToolsModelUsed for
ExploreRead-only; Write and Edit deniedThe parent’s model, with an exception when the parent runs FableSearching and understanding a codebase. Claude picks a thoroughness: quick, medium or very thorough
PlanRead-onlyInherits the parentResearch during plan mode
general-purposeEvery tool available to subagentsInherits the parentMulti-step tasks that need both exploration and edits
claudeEvery tool available to subagentsFollows the subagent model orderA catch-all when nothing more specific fits

Two small helpers also exist: claude-code-guide (Haiku) answers questions about Claude Code, and statusline-setup (Sonnet) runs for /statusline.

Explore and Plan skip CLAUDE.md and git status to stay quick. They’re also one-shot: they return no agent ID, so Claude can’t resume them later. A user or project subagent named Explore overrides the built-in one, which is how you move exploration to a cheaper model.

To turn built-ins off, deny them by name in settings, for example "deny": ["Agent(Explore)"]. Denying Agent with no parentheses stops all delegation.

Defining custom subagents

A subagent is a Markdown file with YAML frontmatter. The body becomes its system prompt. Where you put the file sets its scope; when two have the same name, the higher one wins:

LocationScope
Managed settings directory, .claude/agents/Whole organization
--agents JSON flagCurrent session only
.claude/agents/This project (commit it)
~/.claude/agents/All your projects
A plugin’s agents/ directoryWherever the plugin is enabled

Both agent directories are scanned recursively, so subfolders are fine; identity comes from name, not the path. Claude Code watches them and picks up edits within seconds, except for a directory that didn’t exist when the session started, which needs a restart.

The /agents command

On Claude Code v2.1.197 and earlier, /agents opened an interactive wizard with Running and Library tabs. In current versions it prints a reminder to ask Claude to write the file or to edit .claude/agents/ and ~/.claude/agents/ directly. Asking works well: “Create a project subagent in .claude/agents that reviews diffs, read-only, on Sonnet” produces a file you then review. To see running subagents and their models, use /tasks.

Frontmatter fields

Only name and description are required. Misspelled fields are ignored silently, so check casing (disallowedTools, maxTurns).

FieldWhat it does
descriptionWhen Claude should delegate. Add “use proactively” to encourage automatic use
toolsAllowlist. Omit it to inherit every tool available to subagents
disallowedToolsDenylist, applied before tools
modelsonnet, opus, haiku, fable, a full model ID, or inherit
permissionModedefault, acceptEdits, auto, dontAsk, bypassPermissions or plan
mcpServersMCP servers for this subagent, by name or inline
skillsSkills whose full content is preloaded
hooksHooks that run only while this subagent is active
maxTurnsStop after this many agentic turns
memoryPersistent memory scope: user, project or local
isolationworktree runs it in a temporary git worktree
effortEffort level for this subagent

Plugin subagents ignore hooks, mcpServers and permissionMode for security reasons. Copy the file into .claude/agents/ if you need them.

Keep descriptions short. They sit in the main context so Claude can route, and Claude Code warns at startup when your custom subagents’ descriptions add up to more than 15,000 tokens.

How the main agent delegates

Claude matches your request against each subagent’s description. You can steer it three ways, from weakest to strongest:

  1. Name it in plain language: “Use the code-reviewer subagent on my last commit.” Claude usually delegates.
  2. @-mention it, for example @agent-code-reviewer, which guarantees that subagent runs for the task.
  3. Run the whole session as it: claude --agent code-reviewer, or "agent": "code-reviewer" in .claude/settings.json. The main thread then takes the subagent’s prompt, tools and model.

Subagents Claude spawns from the main conversation start in the background by default, so you can keep working. When one hits a tool call that needs permission, the prompt surfaces in your main session with the subagent’s name. Background subagents get a reduced built-in tool set (Read, Grep, Glob, Bash, Edit, Write, WebFetch and a few others) but keep every MCP tool they’d otherwise have.

Subagents can spawn their own, up to three layers below the main conversation by default (CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH changes it). At most 20 run at once by default.

Limiting a subagent’s tools: a read-only reviewer

This is the most useful pattern. A reviewer that can read and run commands but can’t edit files or touch MCP tools:

markdown
---
name: diff-reviewer
description: Reviews the current git diff for bugs, missing tests and leaked secrets. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: sonnet
---

Run `git diff` and `git diff --staged`. Review only changed files.
Report findings as Critical, Warning or Suggestion, each with file and line.
Do not propose rewrites of unchanged code.

Because tools is an allowlist, anything not listed is gone, including Edit, Write and every MCP tool. Leaving Agent out also stops it from spawning its own subagents. Bash still lets it run any command your permission rules allow; to block specific commands, use a deny rule such as Bash(git push *) in settings. A disallowedTools entry with a specifier removes the whole tool, not just the matching commands. The Claude Code permissions guide covers how those rules work.

For MCP, tools and disallowedTools accept server-wide entries:

yaml
disallowedTools: mcp__*          # no MCP tools at all
tools: Read, Grep, mcp__sentry__* # only Sentry's tools from MCP

Scoping MCP servers to a subagent

mcpServers gives a subagent servers the main conversation doesn’t have. A string reuses a server you already configured; an inline definition uses the same schema as .mcp.json and is connected when the subagent starts and disconnected when it finishes:

yaml
---
name: browser-tester
description: Tests UI changes in a real browser with Playwright
mcpServers:
  - playwright:
      type: stdio
      command: npx
      args: ["-y", "@playwright/mcp@latest"]
  - github
---

Defining a server inline keeps its tool descriptions out of the main context entirely. Inline servers in a project’s .claude/agents/ only load after you trust that folder, and managed allowedMcpServers and deniedMcpServers policies still apply. The Playwright MCP post covers that server in detail.

Permission modes for subagents

permissionMode sets the mode a subagent runs in, but the main conversation can override it. If the main session is in bypassPermissions, acceptEdits or auto, the subagent runs in that same mode and your setting is ignored. In plan, the subagent stays in plan mode. Only when the main session is in default or dontAsk does the subagent’s permissionMode apply, and even then a subagent can’t put itself into bypassPermissions. Settings-level deny rules apply to subagents too, as do PreToolUse hooks from settings files, which the Claude Code hooks post covers.

Cost and latency tradeoffs

Subagents save main-context tokens, not total tokens. Each one sends its own requests against the same usage limits, and the cost guide lists subagents among the things that add requests on top of the main conversation. /usage attributes a share of usage to them on paid plans.

What helps:

  • Put routine subagents on a smaller model with model: haiku. Without a model field, a subagent you define runs on the main conversation’s model, so switching the session to Opus moves them to Opus too.
  • Return short reports. Many subagents each returning a long result can fill the main context anyway.
  • Prefer a fork when the task needs everything you’ve already discussed: it reuses the parent’s prompt cache, while a fresh subagent has a separate cache and spends its first turns rediscovering context.

On latency, a non-fork subagent starts cold and has to gather its own context, so a quick targeted edit is faster in the main conversation. Subagents pay off when the output would be long and you only need the conclusion: test runs, log digging, parallel research across separate modules.

Subagents also share the main session’s MCP connections when they reference a server by name. When those connections go through Toolstile, every call a subagent makes is checked against the same per-tool Allow, Require approval or Block rules as the main conversation and logged in the same place, so a subagent can’t reach a tool the Claude Code agent’s grant withholds.