Agent Subagents
A subagent is a named, configurable Claude instance that runs in its own context window within a session. The parent agent delegates a task; the subagent works independently and returns only a summary. No verbose output enters the main conversation. Primary use: isolate context-polluting operations (test runs, log analysis, documentation fetches) while keeping the main conversation clean.Subagents vs Agent Teams vs Main Conversation
Subagent File Format
Subagents are markdown files with YAML frontmatter. The body becomes the system prompt.Scopes and Priority
Higher priority wins when names conflict.
Key Frontmatter Behaviors
tools vs disallowedTools: disallowedTools applied first, then tools resolves against remaining pool. A tool in both is removed.
Spawning restrictions: Use Agent(worker, researcher) syntax in tools to restrict which subagent types this agent can spawn. Omit Agent entirely = cannot spawn subagents.
permissionMode: if parent uses bypassPermissions or acceptEdits, takes precedence — cannot be overridden by subagent.
skills: full skill content injected at startup; subagents do NOT inherit parent conversation’s skills.
memory: subagent gets a persistent directory (~/.claude/agent-memory/<name>/ for user scope). MEMORY.md auto-loaded at startup.
isolation: worktree: subagent gets a temporary git worktree — its file edits are isolated. Worktree auto-cleaned if no changes, or branch+path returned if changes made.
Invocation Patterns
Automatic: Claude matches your request description to subagent descriptions and delegates. @-mention:@"code-reviewer (agent)" look at the auth changes — guarantees that subagent runs.
CLI flag: claude --agent code-reviewer — whole session uses that subagent’s system prompt and tools.
Background: run this in the background or Ctrl+B — concurrent execution. Pre-approves permissions upfront; auto-denies anything not pre-approved.
Resume: subagents retain full conversation history when resumed via SendMessage with the agent’s ID or name as to. This does not require agent teams (an earlier version of this page said it needed CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1; the docs say otherwise, checked 2026-10-09). Built-in Explore and Plan are one-shot and cannot be resumed; a subagent stopped by the user does not auto-resume.
Fork Mode
Experimental (CLAUDE_CODE_FORK_SUBAGENT=1). A fork inherits the full conversation history instead of starting fresh — useful when the subagent needs full context without re-explanation. Forks can’t spawn further forks. Disabled in non-interactive/headless mode.
Use As Agent Team Teammate
Subagent definitions can be referenced as teammate types in agent teams:tools and model. Definition body appended to teammate system prompt (not replacing it). skills and mcpServers frontmatter NOT applied in teammate mode.
Model Tiering for Subagent Types
Match model capability to task type — cheaper models for mechanical work, stronger models for reasoning:
Source: Pi Subagents extension practice (Amos Blomqvist).
Model precedence (Claude Code): per-invocation
model parameter, then the agent’s frontmatter model, then CLAUDE_CODE_SUBAGENT_MODEL, then the main conversation model. The environment variable only fills gaps and does not change built-in Explore and Plan unless CLAUDE_CODE_SUBAGENT_MODEL_FORCE=1. The cost reasoning behind tiering, including why the context lever usually outweighs the price lever, is in Subagent Cost Model; a measured example is Claude Code Subagents Pro Limits.
Depth limiting: two separate mechanisms, from two harnesses.
- Claude Code: a subagent can spawn subagents by default, up to 3 layers below the main conversation.
CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTHsets the limit (1 turns nesting off); at the limit the Agent tool is withheld. Concurrency defaults to 20 (CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS). TheAgent(type, ...)form intoolsrestricts which types an agent may spawn. Verified against the docs 2026-10-09. - Pi subagents extension: a frontmatter
agentsfield lists spawnable types, giving a Master → Worker → Scout/Researcher layering where Workers cannot spawn Workers. This is declarative convention, not a hard cap.
skills, and the roster of sibling agents. It does not see the parent’s conversation, invoked skills or files read. Built-in Explore and Plan skip CLAUDE.md and git status; a custom agent opts out with omitClaudeMd: true. A large CLAUDE.md is therefore paid on every spawn. See Subagent Cost Model.
Non-interactive limitation: subagents cannot ask the user questions. Anything requiring clarification mid-execution must be handled by the orchestrator or designed to avoid requiring it.
Exploration offloading pattern: delegate read-only exploration to Haiku-class subagents before context bloat occurs (not as a reaction to bloat). The master agent keeps its context window clean for execution, not exploration.
When to Use Subagents (not main conversation)
- Task produces verbose output (test runs, log analysis, doc fetches)
- You want to enforce tool restrictions or specific permissions
- Work is self-contained and can return a summary
- You want to protect main context from pollution
When to Fold a Subagent Back In (2026-11-01)
Anthropic’s Applied AI guide (Fold Subagents Into Main Loop) argues that many multi-agent systems, especially enterprise ones built team by team, do better as one agent loop that discloses skills and deferred tools progressively. Its keep-conditions for a subagent are narrower than the list above:- the single loop shows measurable degradation from context bloat;
- several tasks must run in parallel with fresh, discardable context; or
- one agent struggles to choose tools, or its system prompt holds conflicting instructions.
Related Pages
- Fold Subagents Into Main Loop — Anthropic guide on folding subagents into skills and tools, plus practitioner reaction
- Subagent Cost Model — why delegation saves cost, price table, break-even rule
- Agent Teams — when teammates need to coordinate with each other
- Agent Skills — skills and how to preload them into subagents
- Agent Harness — harness components; subagents as delegation primitive
- Context Compression Strategies — why context isolation matters
- Context Degradation Patterns — context-distraction failure mode; exploration offloading as proactive fix
- Agent Primitive Selection — decision tree for choosing between skills, subagents, and teams
Linked pages
- Agent Harness
- Agent Primitive Selection
- Agent Skills
- Agent Teams
- AI and ML Engineering
- Claude Code → Cross-Platform Migration Matrix
- Claude Code Plugins
- Claude Code Worktree-Isolation Guard
- Context Compression Strategies
- Context Degradation Patterns
- Context Engineering
- Model Tier Routing
- Multi-Vendor Adversarial Review
- OpenAI Codex
- Subagent Cost Model
- Worker Coordination: Partial Result Passing Between Parallel Agents
- Worktree Isolation