Claude Code Subagents and Parallel Agents
How Claude Code splits work across agents: subagents, agent teams (experimental), --worktree and manual sessions, what each shares, and how parallel runs fail. Checked 2026-11-11.
- Subagents run inside one session in their own context and return a summary. Agent teams are experimental and disabled by default: a lead coordinates teammates that share a task list and message each other. Worktrees separate files and branches for sessions you run yourself.
- Subagent definitions are Markdown files in .claude/agents/ or ~/.claude/agents/. On a name clash the higher-priority location wins, and only name and description are required.
- A worktree does not separate anything outside the checkout, such as a dev database or a port, and agent teams don't put teammates in worktrees, so give each agent its own files, port and data.
This page is a map of splitting work across more than one Claude Code agent: subagents inside one session, agent teams, and sessions you run in parallel with git worktrees. It covers what each approach separates and shares, how to define a subagent, what goes wrong in parallel runs, and the order in which to read our articles on the topic. Every fact was checked against the official docs on 2026-11-11, when the latest Claude Code on npm was 2.1.322.
We did not run any command for this hub. Where the docs did not state something, we left it out or marked it “unverified”.
Ways to run work in parallel
| Approach | How it starts | Separate | Shared |
|---|---|---|---|
| Subagent | Claude delegates on its own, or you ask (“use a subagent to…”) or @-mention one | Its own context window; only a summary comes back | The session’s working directory, unless the subagent has isolation: worktree. Its requests count toward the same usage |
| Agent team (experimental, off by default) | Set CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1, then ask Claude for teammates in an interactive session | Each teammate is a separate Claude Code instance with its own context window | A task list and a mailbox. Teammates load the same CLAUDE.md, MCP servers and skills and start in the lead’s permission mode. They are not put in worktrees |
claude --worktree <name> | One command per terminal | Files and branch: .claude/worktrees/<name>/ on a branch named worktree-<name> | The repository’s .git directory, project-scope plugins, “don’t ask again” approvals, and the main checkout’s untracked skills, agents and commands when the worktree has none of its own |
| Separate sessions by hand | git worktree add, then claude in each directory | Whatever you set up | Everything outside the checkout. Cross-session messaging lets those sessions pass findings to each other |
(source: https://code.claude.com/docs/en/agents, checked 2026-11-11) (source: https://code.claude.com/docs/en/sub-agents, checked 2026-11-11) (source: https://code.claude.com/docs/en/agent-teams, checked 2026-11-11) (source: https://code.claude.com/docs/en/worktrees, checked 2026-11-11)
The docs list two more approaches. Agent view (claude agents, a research preview) dispatches and monitors background sessions. Dynamic workflows run many subagents from a script and cross-check their results. /batch is a skill that splits one large change into 5 to 30 worktree-isolated subagents. We have not written about these yet.
Details from the docs that change how you plan:
- With nothing configured, a subagent Claude spawns from the main conversation starts in the background. By default 20 subagents can run at once in a session (
CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS), and a subagent can spawn its own subagents up to three layers below the main conversation (CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH). - While agent teams are enabled, a subagent that Claude names launches as a teammate, so a team can form even when you did not ask for one. Setting the variable to
0turns that off. - Agent team limits: one team per session, no nested teams, the lead is fixed, and
/resumeand/rewinddo not restore in-process teammates. Split panes need tmux or iTerm2. - Cost: the costs page says agent teams use approximately 7x more tokens than standard sessions when teammates run in plan mode. The agent teams page suggests starting with 3 to 5 teammates, and the costs page suggests Sonnet for teammates and shutting them down when they finish.
(source: https://code.claude.com/docs/en/sub-agents, checked 2026-11-11) (source: https://code.claude.com/docs/en/agent-teams, checked 2026-11-11) (source: https://code.claude.com/docs/en/costs, checked 2026-11-11)
Defining a subagent
A subagent is a Markdown file with YAML frontmatter. The body becomes its system prompt, and it does not receive Claude Code’s own system prompt. /agents no longer opens an editor; it reminds you to ask Claude or edit the directories directly.
| Priority | Location | Scope |
|---|---|---|
| 1 (highest) | Managed settings | Organization |
| 2 | --agents at launch | That session |
| 3 | .claude/agents/ | The project |
| 4 | ~/.claude/agents/ | All your projects |
| 5 | A plugin’s agents/ directory | Where the plugin is enabled |
On a name clash, the higher-priority definition wins. The frontmatter name identifies the subagent, not the filename. Edited files are picked up within a few seconds, but a new agents directory that did not exist at session start needs a restart.
The frontmatter keys most definitions need:
nameanddescriptionare the only required keys.namecan be up to 256 characters and cannot contain:.toolsis an allowlist; without it, the subagent inherits every tool available to subagents.disallowedToolsis a denylist, andBash(git push *)there removes the whole Bash tool.modeltakes an alias such assonnet,opusorhaiku, a full model ID, orinherit. The model is resolved from the model Claude passes for that call, then the definition, thenCLAUDE_CODE_SUBAGENT_MODEL, then the main conversation’s model.permissionModeis ignored when the main conversation is inbypassPermissions,acceptEditsor auto mode. Enforce read-only access withtools.isolation: worktreeruns the subagent in a temporary worktree.memorygives it a persistent memory directory.- Plugin subagents ignore
hooks,mcpServersandpermissionMode.
(source: https://code.claude.com/docs/en/sub-agents, checked 2026-11-11)
Failures in parallel runs and fixes
A worktree has only committed files. Our subagent failures article found that a gitignored verify script was missing in every lane. A subagent with isolation: worktree branches from the repository’s default branch, not the parent session’s HEAD, unless worktree.baseRef is "head". A .worktreeinclude file copies files that match its patterns and are also gitignored into each new worktree.
A worktree does not separate what lives outside the checkout. Our worktree resources test showed a dev database and the default port shared across worktrees, and a cleanup that deleted the local database with the worktree. The fix was a gitignored .env.local per worktree with its own port and database path.
Isolation checks stop git and edits, not every write. While a session is isolated in a worktree, Claude Code blocks edits and git commands aimed at the main checkout. A command that writes there without running git, such as cp or a shell redirect, is not refused by those checks. Hooks keep ${CLAUDE_PROJECT_DIR} pointing at the main checkout.
Agent teams share files. The docs say two teammates editing the same file leads to overwrites; give each teammate its own set of files.
Leftovers. -p runs have no exit prompt, so their worktrees stay until you run git worktree remove, after git worktree unlock if git says it is locked.
Reports are claims. Our failures article puts guards in every prompt (no --amend, both paths named, findings written to a file, stop with BLOCKED when unsure) and checks results with git rather than the self-report.
(source: https://code.claude.com/docs/en/worktrees, checked 2026-11-11) (source: https://code.claude.com/docs/en/sub-agents, checked 2026-11-11) (source: https://code.claude.com/docs/en/agent-teams, checked 2026-11-11)
Splitting memory and context
Each subagent starts with a fresh context window. It does not see your conversation history, the skills you already invoked or the files Claude already read. It gets the task message, CLAUDE.md files (Explore and Plan skip them, and omitClaudeMd: true turns them off), a git status snapshot and any preloaded skills. It does not inherit auto memory.
With memory set to user, project or local, a subagent keeps its own memory directory, such as .claude/agent-memory/<name>/ for project, and the first 200 lines or 25KB of its MEMORY.md load at startup. The docs recommend project as the default scope.
A teammate loads CLAUDE.md, MCP servers and skills like a regular session, but not the lead’s conversation history, so put task details in the spawn prompt. A subagent in its own worktree takes its instruction files from the main conversation, not from the worktree.
How we split lasting policy from current state, and why notes rot, is in Designing Agent Memory.
(source: https://code.claude.com/docs/en/sub-agents, checked 2026-11-11) (source: https://code.claude.com/docs/en/agent-teams, checked 2026-11-11) (source: https://code.claude.com/docs/en/worktrees, checked 2026-11-11)
Reading order
These are starting points, not a ranking. Read top to bottom if you are new to running more than one agent.
- How to Define Claude Code Subagents: built-in subagents, file locations, frontmatter, model choice and tool limits.
- Run Parallel AI Agents with Git Worktrees: git worktree basics,
--worktree, setup per worktree and cleanup. - Git Worktrees Don’t Isolate Your Database or Ports: a reproducible test of shared data and ports, and a per-worktree fix.
- Subagent Failures in Parallel Runs: missing files in worktrees, split handover notes, prompt guards and checking results.
- Designing Agent Memory: memory indexes, handover files and notes that go stale.
- Claude Code Code Review: /code-review vs Managed PR Reviews:
/code-reviewruns as a background subagent with its own context.
Related hubs
- Claude Code Setup and Daily Operations: settings, CLAUDE.md, permissions and day-to-day use.
- Claude Code in CI, Headless and Production: running Claude Code without a person at the keyboard.
- Claude Code Hooks: Events, Exit Codes and Examples: hooks, including
TeammateIdle,TaskCreatedandTaskCompletedfor agent teams. - MCP Servers in Claude Code, Codex and Gemini CLI: MCP servers, including the
mcpServersa subagent can get.
Summary
- Subagents work inside one session in their own context and return a summary. Agent teams are experimental and off by default. Worktrees separate files for sessions you run yourself.
- Definitions live in
.claude/agents/or~/.claude/agents/; the higher-priority location wins andnameidentifies the subagent. - Set
toolsandmodelexplicitly.permissionModeis ignored when the main conversation is inbypassPermissions,acceptEditsor auto mode. - Worktrees hold only committed files and don’t separate databases or ports. Give each agent its own files, port and data.
- A subagent starts without your conversation history; a teammate starts without the lead’s. Put what they need in the prompt.
- Checked against the official docs on 2026-11-11. We ran nothing for this hub.
FAQ
- What is the difference between subagents and agent teams in Claude Code?
- A subagent works inside one session in its own context window and returns a result to the caller, and the main agent manages the work. In an agent team, each teammate is a separate Claude Code instance; teammates message each other directly and coordinate through a shared task list. The docs say teams use more tokens and are experimental.
- How do I turn on agent teams?
- Set CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS to 1 in your environment or in the env block of settings.json, then ask Claude for teammates in an interactive session. In -p mode Claude does not spawn teammates.
- Where do subagent definition files go, and which one wins on a name clash?
- Project subagents go in .claude/agents/ and personal ones in ~/.claude/agents/. The priority is managed settings, then the --agents flag, then .claude/agents/, then ~/.claude/agents/, then a plugin's agents/ directory. The higher-priority definition wins, and the frontmatter name identifies the subagent, not the filename.
- Does a git worktree stop parallel agents from sharing a database or a port?
- No. A worktree has its own files and branch, but a database path or port set outside the repository is still shared. In our test, a reset in one worktree emptied the other's data and the second dev server could not bind the same port. Give each worktree its own gitignored .env.local.
- Does a subagent see my conversation?
- No. Each subagent starts with a fresh context window without your conversation history, the skills you already invoked or the files Claude already read. It does get CLAUDE.md files, except for Explore and Plan or when omitClaudeMd is set, and the task message Claude writes for it.