Agent swarms in Claude Code are dispatched via the Agent tool, which spawns sub-agents — fresh, forked, or specialized — to run in parallel with optional worktree isolation and the Agent Teams coordination layer for native peer communication.
📝Claude Code provides three distinct agent-spawn modes accessible through the Agent tool: standard sub-agents (fresh context, custom system prompt), forked sub-agents (full parent context cloned, prompt cache shared), and specialized sub-agents (named profile with pre-declared tool permissions). Parallel dispatch is the default — multiple agents launched from a single response run concurrently, and since v2.1.198 they run in the background. The Agent Teams pattern, also called swarm mode, adds native coordination primitives between children: a shared task list with lock-protected claiming, and peer messaging. Worktree isolation routes each sub-agent's filesystem writes to a separate git worktree, enabling parallel implementation branches without merge conflicts.
How It Works
- Standard sub-agents — spawned via the Agent tool with an explicit
subagent_type(e.g.,Explore,Plan,code-reviewer), or with none, which launchesgeneral-purpose. Fresh context window with a custom system prompt. The child receives the task description, CLAUDE.md, and preloaded skills, not the parent's conversation history. - Forked sub-agents — requested as the
forksubagent type, or started by hand with/subtask. Inherits the parent's full uncompressed context and shares its prompt cache, making it cheaper than spawning fresh when session context matters. On by default in interactive sessions since v2.1.232; with agent view on,/forknow copies the whole session into a new background session instead. - Policy islands — pair
context: forkwith a namedagent:in skill or command frontmatter to spawn a sub-agent restricted to a pre-declaredallowed_toolslist. The child runs without mid-workflow approval prompts and returns only a summary. - Agent Teams (swarm mode) — experimental coordination layer, off by default (
CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1), where a lead spawns teammates that share a task list and message each other by name. Allows mid-flight work subdivision rather than only at dispatch time. Teammates share one checkout, so work must be partitioned by file. - Parallel dispatch — multiple Agent tool calls in a single assistant response execute concurrently. Combined with
isolation: "worktree", each sub-agent gets its own filesystem branch. - Agent SDK — programmatic access for non-interactive (CI/CD) environments. Fork mode and teammate spawning are interactive-only; in
-pand SDK runs, Claude spawns standard sub-agents.
Why It Matters
Most Claude-based multi-agent architectures live or die on this primitive. The choice between fork, standard, and policy-island sub-agents determines whether deep session context survives, how many approval prompts the human sees, and whether parallel work can safely share a filesystem. Getting the dispatch mode wrong compounds: a fresh sub-agent loses the architectural decisions made deep into a session, an unforked sub-agent for noisy work pollutes the parent's window with debug churn, and a swarm without worktree isolation collides on concurrent file writes.
The Agent Teams primitive specifically — a shared task list, lock-protected task claiming, peer messaging — is what separates a loose swarm (independent agents that may produce contradictory outputs) from a coordinated team (agents that can negotiate work boundaries and avoid conflict). Production multi-agent systems at scale require the latter. Demos and tutorials usually show the former.
FAQ
What is the difference between a sub-agent and a forked sub-agent in Claude Code?
A standard sub-agent gets a fresh context window with a custom system prompt and receives only the delegation message, not the parent's conversation. A forked sub-agent inherits the parent's full uncompressed conversation history and shares its prompt cache, making it cheaper and richer when session context is load-bearing.
How do I run multiple Claude Code agents in parallel?
Invoke the Agent tool multiple times in a single assistant response. Claude Code dispatches them concurrently. For filesystem isolation, pass isolation: "worktree" to each call so each sub-agent writes to its own git worktree without merge conflicts. For whole parallel sessions, use agent view (claude agents) or Projects.
What is Claude Code Agent Teams (swarm mode)?
Agent Teams is Claude Code's experimental coordination layer for multiple concurrent sessions. A lead spawns teammates that share a task list (claimed under file locks so two teammates never take the same task) and message each other directly. It is disabled by default and enabled with CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1.
Can a forked sub-agent spawn its own forks?
No. A forked sub-agent receives an injected directive that disables further forking. Forks are one level deep by design — recursive forking would explode context costs and create coordination nightmares.
Does fork mode work in CI/CD?
No. Fork mode is interactive-only; non-interactive runs (claude -p, the Agent SDK) spawn standard sub-agents. In interactive sessions it is on by default since v2.1.232, and CLAUDE_CODE_FORK_SUBAGENT=0 turns it off.
Related
- 📝Agent Swarm — the general pattern Claude Code's agent dispatch implements
- 📝Multi-Agent Orchestration at Scale — production architecture for coordinated systems using these primitives
- 📝AI Agent Swarms vs. Sequential Review: When Each Wins — when this primitive is the right tool versus a focused sequential pass
- 📝Claude Code Multi-Agent Orchestration — hub comparing subagents, workflows, agent view, teams, Projects, and cross-session messaging
