Skip to main content
Module 1: Core Agent 4 / 6
Beginner S04 Subagents Context Architecture

Subagents & Context Isolation

Learn how Claude Code spawns child agents with clean context windows — enabling parallel work, protecting the parent's context, and handling complex subtasks.

March 20, 2026 17 min read
Verified Curriculum reviewed: Oct 21, 2026

What You’ll Learn

When Claude Code encounters a complex subtask — like exploring a codebase or researching an API — it doesn’t always handle it in the main conversation. Instead, it can spawn a subagent: a child agent with its own fresh context window.

By the end, you’ll understand:

  • Why context isolation matters
  • How subagents are spawned and managed
  • The parent-child communication pattern
  • When to use subagents vs. direct execution

The Problem

Every token matters. The context window (the total amount of text the AI can process at once) is finite. When Claude Code reads a large file, runs a complex search, or explores multiple directories, all that output consumes context.

The problem: research work pollutes the working context. If Claude Code reads 50 files to find the right one, those 49 irrelevant file contents are still in the context window, making subsequent work less effective.

How It Works

The Subagent Pattern

┌─────────────────────────────────────────┐
│           Parent Agent                   │
│                                          │
│  Messages: [user prompt, ...]            │
│                                          │
│  "I need to find where auth is           │
│   implemented. Let me spawn an           │
│   explorer agent."                       │
│       │                                  │
│       │ Spawn                            │
│       ▼                                  │
│  ┌──────────────────────────────┐        │
│  │      Child Agent              │       │
│  │  Messages: [task prompt only] │       │
│  │                               │       │
│  │  • Read file A (irrelevant)   │       │
│  │  • Read file B (irrelevant)   │       │
│  │  • Read file C (found it!)    │       │
│  │                               │       │
│  │  Return: "Auth is in          │       │
│  │  src/middleware/auth.ts,      │       │
│  │  uses JWT with refresh        │       │
│  │  tokens..."                   │       │
│  └──────────────┬───────────────┘        │
│                 │                         │
│                 │ Result (summary only)   │
│                 ▼                         │
│  Parent receives: concise summary        │
│  (not all the files the child read)      │
│                                          │
└──────────────────────────────────────────┘

The key insight: only the result flows back, not the full exploration history. The parent’s context stays clean.

What a Subagent Starts With

A subagent that isn’t a fork starts with:

  • Its own system prompt: the prompt from its definition plus environment details, not the Claude Code system prompt
  • The task message that Claude writes when it hands off the work
  • Your CLAUDE.md files and a git status snapshot, except in the built-in Explore and Plan agents, which skip both to stay fast and inexpensive

It never sees your conversation history or the files Claude has already read, so everything it needs must be in the task prompt.

Forks are the exception. A fork inherits the full conversation, system prompt, tools, and model, so it needs no re-explaining. Its own tool calls still stay out of your context, and only its final result comes back. Fork mode is on by default (in the interactive terminal since v2.1.232, everywhere since v2.1.302), so Claude can start forks on its own, and you can start one yourself with /subtask <task>. Set CLAUDE_CODE_FORK_SUBAGENT=0 to turn fork mode off.

How Subagents Are Spawned

In Claude Code, subagents are spawned via the Agent tool:

{
  "type": "tool_use",
  "name": "Agent",
  "input": {
    "description": "Explore auth implementation",
    "prompt": "Find where authentication is implemented in this codebase. Look for middleware, route guards, and token management. Return a concise summary.",
    "subagent_type": "Explore",
    "model": "haiku"
  }
}

Key parameters:

  • prompt: The task for the child agent
  • subagent_type: Specialized behavior (Explore, Plan, general-purpose, etc.)
  • model: Can use a cheaper/faster model for simple tasks

Subagents that Claude spawns from your main conversation start in the background by default, so you can keep working while they run; /tasks lists them.

Context Window Comparison

Without subagents:

Parent context: [user prompt] + [50 file reads] + [actual work]
                 └── context window nearly exhausted ──┘

With subagents:

Parent context: [user prompt] + [agent result: 200 words] + [actual work]
                 └── context window mostly available ──┘

Child context:  [task prompt] + [50 file reads] + [summary]
                 └── separate context, discarded after ──┘

Parallel Subagents

Multiple subagents can run concurrently:

[
  {
    "type": "tool_use",
    "name": "Agent",
    "input": {
      "description": "Research database schema",
      "prompt": "Find and summarize the database schema..."
    }
  },
  {
    "type": "tool_use",
    "name": "Agent",
    "input": {
      "description": "Research API routes",
      "prompt": "List all API routes and their handlers..."
    }
  }
]

Both agents execute in parallel, each in their own isolated context.

Key Insight

Subagents are not just about parallelism — they’re about context management. The most precious resource in an AI coding session is context window space. Every unnecessary token in context reduces the quality of subsequent work.

Think of it like delegation in a real team:

  • You (the parent) stay focused on the big picture
  • Team members (subagents) do the research and report back
  • You only need to know the conclusions, not every step they took

This is why experienced users delegate verbose, self-contained work, such as test runs, log digging, and broad searches, to subagents: not mainly for speed, but to keep the main conversation clean and focused. Delegation isn’t free, though. Each subagent spends its own tokens, and many detailed results still add up in your context. Stay in the main conversation for iterative back-and-forth, for phases that share a lot of context, and for quick, targeted changes.

Hands-On Example

Here’s how to design effective subagent prompts:

# Bad: vague, returns too much
"Look at the codebase"

# Good: specific, asks for structured output
"Find all React components that handle user input forms.
For each, report:
1. File path
2. What data it collects
3. How it validates input
Return as a bullet list. Skip components not related to user data."

The prompt should be:

  • Specific about what to find
  • Structured in what to return
  • Bounded to avoid unbounded exploration

Model Selection for Subagents

Choose the right model for the task:

Haiku (fast, cheap):
  → Simple searches, file exploration, listing tasks
  → "Find all files matching *.test.ts"

Sonnet (balanced):
  → Code analysis, refactoring plans, moderate complexity
  → "Analyze the error handling patterns in this module"

Opus (powerful):
  → Complex reasoning, deep analysis, critical tasks
  → "Trace the root cause of this intermittent test failure"

Fable (top tier):
  → Hardest judgment work: architecture decisions, adversarial
    verification, final synthesis
  → "Design a migration strategy for the database schema"

The Agent tool’s model parameter accepts sonnet | opus | haiku | fable. On the Anthropic API those aliases currently resolve to Sonnet 5.5, Opus 5.5, Haiku 5.5, and Fable 5.1; other providers may map them to earlier versions. A subagent definition’s model field also accepts a full model ID or inherit.

What Changed

Without SubagentsWith Subagents
All work in one contextResearch in child, decisions in parent
Context fills up quicklyContext stays clean
Sequential explorationParallel exploration possible
One model for everythingRight model for each task
Full history visibleOnly summaries returned

Next Session

Session 5 covers Skills & Knowledge Loading — how Claude Code’s two-layer injection system loads skill names upfront but defers full content until needed, keeping the system prompt lean.