Understanding Sub-Agents
As tasks grow larger, doing everything in a single conversation becomes a problem — context fills up, details get compressed, and quality drops. Sub-agents are Claude Code's answer: a main agent manages the work while specialist agents handle each piece in their own separate context window.
What is a sub-agent?
A sub-agent is a separate Claude instance that Claude Code spawns to handle a specific task. It has its own context window, works independently, and returns only a short summary back to the main agent. The main agent stays lean — it never sees the sub-agent's full working context.
The Architecture
Let the main agent manage — let sub-agents do the work. Each sub-agent is a specialist. The main agent only receives a short summary when the sub-agent finishes.
Main Agent
Manages the task
Explorer
Read-only searchFinds files, reads code, maps the codebase
Coder
Write & editCreates files, makes changes, runs commands
Reviewer
Quality checkReviews code, flags issues, never edits
Why Sub-Agents Matter — The Context Problem
Every Claude conversation has a context window. When it fills up, Claude compresses (compacts) older content. Important details get lost, earlier decisions are forgotten, and quality drops — even if you haven't noticed it happening.
✕ Single conversation
All work in one place
✓ Delegated to sub-agents
Heavy lifting happens elsewhere
Context usage comparison — same task
At 80% context compaction kicks in — older details are compressed and may be lost
Main only receives summaries. Sub-agent used its own window for the heavy lifting
Sub-agent can work freely in its own window — main conversation is unaffected
Common Sub-Agent Types
Claude Code ships with built-in sub-agents for the most common tasks. You can also define custom sub-agents for your project's specific needs.
Searches through files, reads code, and answers questions about the codebase. Never writes or edits files — purely a researcher.
Creates new files, edits existing ones, runs terminal commands, and implements features. The workhorse of the sub-agent system.
Reviews code for bugs, security issues, and adherence to conventions. Returns a structured feedback summary — never edits code directly.
Writes unit and integration tests, runs the test suite, and reports failures. Handles all testing work without cluttering the main conversation.
A Simple Worked Example
Task: "Add email notifications when a new user signs up." Here is how the main agent orchestrates sub-agents to complete it.
Receives the task. Plans: explore existing auth code → code the mailer → test it → review.
Reads src/app/api/auth/, finds the signup route at src/app/api/auth/register/route.ts. Reads the existing user creation logic.
Creates src/lib/mailer.ts with sendWelcomeEmail(). Edits register/route.ts to call it after user creation. Installs nodemailer.
Writes src/__tests__/mailer.test.ts. Runs npm run test. All 3 tests pass.
Reviews mailer.ts and the route changes. Flags that the email address should be validated before calling sendWelcomeEmail.
Reads all four summaries. Asks the Coder Agent to add the email validation suggested by the reviewer.
How Claude Invokes Sub-Agents
In most cases Claude Code handles sub-agent dispatch automatically when a task is complex enough. You can also invoke them explicitly in your prompt.
Automatic — Claude decides
For larger tasks Claude Code automatically plans and delegates to the right sub-agents. You just describe the outcome.
Explicit — you name the agent
You can call a specific sub-agent type by name to make intent clear, especially useful for review-only or explore-only tasks.
Sub-Agents — Quick Reference
Main agent = manager
Plans, delegates, collects summaries. Stays lean.
Sub-agent = specialist
Does one job. Returns a short summary to main when done.
Each agent has its own context
Sub-agent context never flows into main. Main stays clean.
Sub-agents can run in parallel
Multiple sub-agents can work simultaneously on independent tasks.
Be explicit for review tasks
Tell reviewer/explorer agents 'do not edit' to avoid unintended changes.
Quality stays high
Main at 15% context → no compaction → no lost details.
What's Next
Sub-agents covered. The next lesson walks through building one from scratch — role definition, instructions, and allowed tools.