Overview
A subagent runs in its own context window with its own system prompt and tool set, and returns a summary to the parent. Use one to keep a wide investigation out of the main context or to run independent work in parallel. Claude Code ships Explore (read-only search), Plan, and general-purpose subagents. The spawning tool is named Agent (renamed from Task in v2.1.63; old Task(...) rules still work). Isolate every file-writing subagent in a git worktree and give each one branch and one PR. Coordination patterns beyond Claude Code are in multi-agent.
Define each subagent as a Markdown file
Put a file with YAML frontmatter in .claude/agents/ (project, commit it) or ~/.claude/agents/ (user). The body is the system prompt. On a name clash the order is managed settings, the --agents flag, project, user, then plugin. /agents no longer opens a wizard (v2.1.198 and later): ask Claude or edit the file.
---
name: page-writer
description: Drafts one content page from a brief. Use for independent page work.
tools: Read, Write, Edit, Bash
model: sonnet
isolation: worktree
---
You write one content page per invocation. Follow the schema in CLAUDE.md.
Commit to the branch the parent assigns. Touch only the file you own.Only name and description are required. Other fields: tools, disallowedTools, model (sonnet, opus, haiku, fable, a full ID, or inherit), permissionMode, maxTurns, skills, mcpServers, hooks, memory, background, effort, isolation. The model resolves from the per-call parameter, then frontmatter, then CLAUDE_CODE_SUBAGENT_MODEL, then the main conversation. A subagent starts with its own prompt plus the CLAUDE.md hierarchy (Explore and Plan skip it), not the conversation history or the parent’s auto memory.
Scope tools to the job
Omitting tools inherits everything. Give a researcher Read, Grep, Glob so it cannot write, and give a writer only the servers it needs. In an agent run as the main thread with claude --agent, tools: Agent(worker, researcher), Read restricts which subagents it may spawn; inside a subagent definition the type list is ignored. Background subagents get a smaller built-in tool set, and their permission prompts surface in the main session.
Isolate every file-writing subagent in a git worktree
Context isolation does not isolate the filesystem: two agents writing the same tree race. isolation: worktree gives each run a temporary worktree under .claude/worktrees/, removed automatically when the subagent finishes without changes. It branches from the repository’s default branch, not the parent’s HEAD, so unpushed work is invisible; set worktree.baseRef to "head" to branch from your current state. Add .claude/worktrees/ to .gitignore, and list gitignored files such as .env in .worktreeinclude so each worktree gets a copy. Skip isolation only for read-only subagents.
Your own parallel sessions use claude --worktree <name>, which creates .claude/worktrees/<name> on branch worktree-<name>. Outside Claude Code’s subagent system, git worktree add .claude/worktrees/<id> -b claude/<task-id> gives the same layout.
Give each agent one branch and one pull request
Commit each subagent’s work to a deterministic branch (claude/<task-id>; generic names like claude/work-1 clash across sessions), then open one PR per branch and review each diff before merging any. A diff from one agent is reviewable alone, a broken agent’s PR can be closed without reverting the others, and CI reports per agent. When the work must land together, merge each PR into a staging branch and merge that to main once.
Parallelize only independent work
Fan out when tasks share no output files and no results: separate content pages, a list of PRs to review, tests for different packages. Run dependent or same-file tasks in sequence.
- Pass everything a subagent needs in its brief; parallel agents do not see each other’s in-progress work. If B needs A’s output, run A, then feed it to B.
- Route writes to high-conflict files (MOC indexes,
llms.txt, shared config) through the parent, not the workers. - Subagents can nest three layers deep by default (
CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH) and up to 20 run at once (CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS). /batch <instruction>splits one change across 5 to 30 subagents, each in its own worktree. Agent teams, for sessions that message each other, are experimental and off by default.