Harness Engineering 6 months ahead, in a few hours.

How to create and configure a subagent in Claude Code

Thomas Poulain · · Updated

A subagent in Claude Code is a separate Claude instance that your session starts for one focused task. It works in its own context window, with its own instructions and tools. Only its result comes back to you. You define one in a Markdown file with YAML frontmatter, saved under .claude/agents/.

Everything below applies to Claude Code 2.1.330 and is based on the official subagents docs.

What does a subagent get when it starts?

A subagent starts with an empty context window. It doesn’t see your conversation history or the files Claude already read. Claude writes it a delegation message that sums up the task. For a custom agent, the body of its file becomes its system prompt. It also loads your CLAUDE.md files, a git status snapshot and the skills named in its skills field.

Only its final report lands in your conversation. Its requests count toward the same usage limits as your session. Since v2.1.198, a subagent started from your main conversation runs in the background by default.

Which subagents are built into Claude Code?

AgentToolsUsed for
ExploreRead-only, no Write or EditSearching and understanding code
PlanRead-onlyResearch while in plan mode
general-purposeEvery tool available to subagentsTasks that explore and then change code

Explore and Plan start without your CLAUDE.md files or the git snapshot, which keeps them fast. Set CLAUDE_CODE_DISABLE_EXPLORE_PLAN_AGENTS=1 (v2.1.198) to remove them, and Claude reads files directly instead.

Where do subagent files go?

LocationScopePriority
Managed settingsWhole organization1
--agents flag at launchCurrent session2
.claude/agents/Current project3
~/.claude/agents/All your projects4
A plugin’s agents/ directoryWhere the plugin is enabled5

When two subagents share a name, the higher-priority one wins. Claude Code scans .claude/agents/ and ~/.claude/agents/ recursively, so you can sort files into folders like agents/review/. Only the name field identifies the agent, whatever the filename.

Claude Code also watches both folders. A new or edited file is picked up on the next delegation, with no restart. The --agents flag takes JSON definitions that last for one session. With -p, it also accepts a path to a JSON file since v2.1.281.

What does a subagent file look like?

---
name: security-reviewer
description: Reviews changed code for security issues. Use after any change to authentication code.
tools: Read, Grep, Glob
model: sonnet
effort: high
maxTurns: 25
---

You review code for security vulnerabilities.
For each issue, give the file, the line, the risk and a fix.
Don't modify any file.

The frontmatter configures the subagent, and the body becomes its system prompt. Only name and description are required.

Claude decides whether to delegate by reading the description. Write down when the agent should be used. To get Claude to delegate on its own, the docs suggest a phrase like “use proactively”. Keep it brief, since Claude Code warns at startup once your subagents’ descriptions together pass 15,000 tokens.

Which frontmatter fields should you know?

FieldWhat it does
nameUnique ID, up to 256 characters, no :
descriptionWhen Claude should delegate to this subagent
tools, disallowedToolsTools allowed, tools removed
modelsonnet, opus, haiku, fable, a full model ID, or inherit
effortlow, medium, high, xhigh or max, depending on the model
permissionModedefault, acceptEdits, auto, dontAsk, bypassPermissions or plan
maxTurnsMaximum number of turns before the subagent stops
skillsSkills loaded in full at startup
mcpServersMCP servers scoped to this subagent
hooksHooks that run only while this subagent is active
memoryPersistent memory: user, project or local
isolationworktree to work in an isolated copy of the repo
omitClaudeMdtrue to start without your CLAUDE.md files (v2.1.271)
autoCompactWindowCompact earlier, from 100,000 tokens up (v2.1.296)

Multi-word fields use camelCase. A misspelled field is ignored without any error. Since v2.1.296, run claude --debug and search ~/.claude/debug/<session-id>.txt for does not recognize or apply. The log names the fields it didn’t recognize.

How do you restrict a subagent’s tools?

Without tools, a subagent gets every tool available to subagents, MCP tools included. tools is an allowlist and disallowedTools removes entries. If you set both, Claude Code removes first, then applies the allowlist to what remains.

---
name: local-read-only
description: Analyzes local code without writing files or calling GitHub
disallowedTools: Write, Edit, mcp__github
---
  • With Bash in its tools, a subagent can still change files. Leave it out of a read-only reviewer.
  • Bash(git push *) in disallowedTools removes the whole Bash tool. To block one command, use permissions.deny in your settings.
  • A background subagent keeps its MCP tools and a fixed list of built-in ones, such as Read, Edit and Bash. Most others are dropped without an error.

To stop Claude from using a subagent at all, deny it like a tool.

{
  "permissions": {
    "deny": ["Agent(Explore)", "Bash(git push *)"]
  }
}

How do you pick a subagent’s model?

Without a model field, a subagent runs on your conversation’s model. CLAUDE_CODE_SUBAGENT_MODEL changes that default, and Claude can also pick a model when it starts the subagent. Since v2.1.316, CLAUDE_CODE_SUBAGENT_EFFORT sets the effort level when nothing else does. To match Haiku, Sonnet or Opus to each role, read the post on subagent models.

How do you call a specific subagent?

Claude delegates on its own when your request matches a description. You can also name the agent or @-mention it.

Use the security-reviewer subagent to check my changes
@agent-security-reviewer review the authentication module

To run a whole session as that subagent, with its tools and model, launch claude --agent security-reviewer. Its system prompt then replaces Claude Code’s own. To make it the default for a project, add "agent": "security-reviewer" to .claude/settings.json.

When should you delegate to a subagent?

Delegate when a task produces a lot of output and you only need the conclusion. Running a test suite or digging through a large log file are good examples. A subagent is also the way to enforce a narrower set of tools or permissions.

Stay in the main conversation when the task needs back-and-forth, or when several steps share a lot of context. Small fixes belong there too, because a subagent starts from zero and has to read the code first. If the side task depends on what you just discussed, start a fork instead, since it inherits your conversation.

What pitfalls should you watch for?

  • The file doesn’t load. Claude Code skips it with no name, or with a name but no description. Same if the opening --- isn’t on the first line or the YAML doesn’t parse. claude plugin validate .claude/agents flags frontmatter that doesn’t parse.
  • A rule has to reach the subagent. It never sees your conversation. Explore, Plan and agents with omitClaudeMd don’t load your CLAUDE.md files at startup. Repeat the rule that matters in your request.
  • permissionMode has no effect. If your session runs in bypassPermissions, acceptEdits or auto mode, the subagent uses that mode instead.
  • Count limits. By default, Claude can’t start another subagent while 20 are already running (CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS). A subagent can spawn its own, up to three levels below your conversation. CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH=1 turns nesting off.

What changed in recent versions?

  • v2.1.63: the Task tool was renamed Agent. Existing Task(...) rules still work.
  • v2.1.198: subagents run in the background by default, and the /agents wizard is gone. Ask Claude to write the file, or write it yourself.
  • v2.1.271: the omitClaudeMd field.
  • v2.1.296: the autoCompactWindow field, and --debug names unknown fields.
  • v2.1.319: in /tasks, an “Earlier in this session” row reopens finished subagents.
  • v2.1.327: a subagent with no model resumes on the model it started on, even after /model.

Each release is detailed in the Claude Code changelog.


A file in .claude/agents/ can also define a teammate’s role in an agent team. Module 14, tmux and Agent Teams covers when to move from isolated subagents to agents that coordinate with each other.

To see these articles more often on Google, add Harness Engineering to your preferred sources.

You might also like