Claude Code
Sub Agents
Created: 26 Apr 2026
Updated: 26 Apr 2026
Context Flow with Subagents
When Claude Code delegates a task to a subagent:
- The main agent generates a task prompt — a focused description of what it needs
- The subagent receives only that prompt — not the full conversation history
- The subagent executes in complete isolation
- Only the final result travels back to the main agent
- All intermediate work (tool calls, file reads, reasoning) is discarded
This isolation is the foundation of efficient, scalable agentic workflows.
How Context Flows Between Agents
- The subagent starts with two inputs only:
- Its own system prompt (the
.mdfile body) - The task prompt generated by the main agent
- It knows nothing about the user's original question or conversation history
- Output flows one way: subagent → main agent
- The quality of the task prompt the main agent writes directly determines the subagent's output quality
User message
│
▼
Main Agent ──[generates focused task prompt]──▶ Subagent
▲ │
└────────────[condensed final result]────────────┘
Context Window Growth and Its Costs
- Every model has a maximum context window — a token limit
- As a session grows, the window fills with conversation, tool outputs, and file content
- Cost — API pricing is per input token; large contexts are expensive
- Latency — Processing more tokens takes longer on every turn
- Quality — Models reason worse when the context is noisy and bloated
Context Pollution
- Intermediate results that are no longer needed stay in the context
- Example: 20,000 tokens of raw file reads linger after the summary is produced
- Those tokens are re-sent (and re-paid) on every subsequent request
Subagents as Context Preservation
- Delegating a task to a subagent keeps the main context lean
- All noisy intermediate work stays in the subagent's isolated side chain
- Only the clean, condensed result comes back to the main thread
- Multiple subagents can run in parallel without interfering with each other
Main Context (stays lean)
├──▶ Subagent A (reads 15 files) ──▶ summary only
├──▶ Subagent B (runs tests) ──▶ pass/fail only
└──▶ Subagent C (searches web) ──▶ key findings only
Subagents are not just convenience — they are a deliberate strategy for long-session quality.
Building the Mermaid Diagram Subagent
A subagent lives in .claude/agents/mermaid-diagram-generator.md
The YAML frontmatter defines its identity and constraints:
---
name: mermaid-diagram-generator
description: Use this agent when you need to convert textual
descriptions, processes, or concepts into Mermaid diagrams.
The main agent should always simplify the concept to the GIST.
Also write some ascii drawing to the subagent representing
the concept.
tools: Read, WebSearch, WebFetch, Write
model: sonnet
color: cyan
---
- name — How Claude Code identifies this agent
- description — Controls when and how the main agent delegates
- tools — Only the tools this agent actually needs
- model — Right-sized:
sonnetis sufficient for diagrams
Tool Pollution and Least Privilege
- Every tool in a subagent's list is loaded into its context as a definition
- Irrelevant tools add noise, slow reasoning, and increase cost
- Tool pollution: giving an agent far more tools than it needs
The Principle of Least Privilege
- Give a subagent only the tools it needs for its specific job
- A diagram generator needs:
Read,WebSearch,WebFetch,Write - It does not need:
Bash,Edit,MultiEdit,KillBash - A subagent that cannot use
Bashcannot accidentally delete files - Least privilege is a safety mechanism, not just an optimisation
Shaping Subagent Behaviour via the Description Field
The description field has two distinct roles:
- Routing trigger — tells the main agent when to delegate to this subagent
- Prompt shaping — tells the main agent how to write the task prompt
Example from the diagram generator's description:
The main agent should always and always simplify the
concept to the GIST. Also write some ascii drawing to
the subagent representing the concept.
- This is an instruction to the main agent, not the subagent
- Before delegating, the main agent will distil and sketch the concept
- The subagent receives a cleaner, more focused input as a result
- The description field is a pre-processing instruction
Refining Subagent Output
Two refinement points, two different targets:
System Prompt (subagent behaviour)
- Step 0: Added to make the subagent research patterns online before drawing
- Step 7: Added to enforce machine-readable output — diagram only, no preamble
### Step 7: Return only the diagram
Return the raw Mermaid code block without any additional
explanation, preamble, or commentary.
Description Field (main agent prompting)
- Controls what context the main agent provides when it delegates
- Add "simplify to GIST + ASCII sketch" to improve input quality
Refinement is iterative: deploy → observe output → identify weakest point → add targeted instruction.
Key Takeaways
- Subagents receive only the task prompt the main agent generates — never the full conversation history
- All intermediate work stays in the subagent's isolated context — only the final result returns to the main thread
- Subagents are a context preservation strategy: they keep the main session lean, fast, and high-quality
- The
descriptionfield controls both when the main agent delegates and how it writes the task prompt - Apply least privilege to tool selection — fewer tools means faster, cheaper, and safer subagents
- Always review third-party subagent
descriptionfields — they are injected into the main agent's system prompt