Skip to main content

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:
Teammate honors the definition’s 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_DEPTH sets the limit (1 turns nesting off); at the limit the Agent tool is withheld. Concurrency defaults to 20 (CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS). The Agent(type, ...) form in tools restricts which types an agent may spawn. Verified against the docs 2026-10-09.
  • Pi subagents extension: a frontmatter agents field lists spawnable types, giving a Master → Worker → Scout/Researcher layering where Workers cannot spawn Workers. This is declarative convention, not a hard cap.
What a custom subagent starts with: its own system prompt, environment details, the delegation prompt, the CLAUDE.md hierarchy, a git status snapshot, preloaded 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.
Verification, parallel work and modular permissioning remain named strengths. “Self-contained work that returns a summary” is still a good split, but the guide sharpens it into divide by context, not by problem type: a subagent should need little from the rest of the system and return a compact result. Splitting by problem type (one agent per team or domain) creates a telephone game of lossy handoffs. Migration path, one step at a time: build a small end-to-end task-completion eval; merge specialists that chain or share data; call the remaining ones as tools so the main agent keeps the conversation; then fold each into a skill (its prompt) plus MCP tools (its API calls) and keep it only if the folded version scores worse. Compare which tasks flipped, not only the score, and rerun each task more than once. Practitioner reaction (same summary) converges on one exception: keep an independent reviewer that did not write the code. Give it fresh context, the diff, and the ticket plus acceptance criteria (a diff-only reviewer drifts into a linter); a different model or harness strengthens it (Multi-Vendor Adversarial Review). To decide whether it earns its cost, log what it blocks for a couple of weeks. Name it explicitly at the merge step: one user reported that automatic delegation never picked a closely matching subagent in 2 of 2 runs (anecdote, unverified).

Linked pages