Sub-Agent Context Isolation
Why Sub-Agents Are Transformative
Section titled “Why Sub-Agents Are Transformative”When the AI agent researches a codebase, it reads files — lots of them. Each file read consumes 3,000-6,000 tokens. Exploring 10 files costs 30,000-60,000 tokens. That’s 15-30% of the context window consumed by exploration alone.
Sub-agents solve this by running in their own context windows:
Main Agent Context Sub-Agent Context──────────────────── ─────────────────────Task description 500 tokens Task: "find all auth files"Agent config 2000 tokens Reads auth.ts 5000 tokensSub-agent summary 800 tokens Reads session.ts 4000 tokens Reads token.ts 3000 tokensImplementation 5000 tokens Reads middleware.ts 6000 tokens Reads rbac.ts 4000 tokens──────────────────── ─────────────────────Total: ~8,300 tokens Total: ~22,000 tokens Returns: 800 token summaryThe sub-agent consumed 22,000 tokens of exploration. The main agent received an 800-token summary. Context savings: 96%.
When to Use Sub-Agents
Section titled “When to Use Sub-Agents”| Use Sub-Agents | Don’t Use Sub-Agents |
|---|---|
| Exploring unknown code | Reading 1-2 specific files |
| Searching across many files | Making targeted edits |
| Research before planning | Simple, well-scoped tasks |
| Code review (unbiased) | Tasks where context history is valuable |
| Parallel investigation tasks | Sequential reasoning chains |
Sub-Agent Patterns
Section titled “Sub-Agent Patterns”Pattern 1: Research Delegation
Section titled “Pattern 1: Research Delegation”Use sub-agents to investigate:1. How our auth middleware handles session tokens2. What rate limiting patterns exist in the codebase3. Which endpoints are currently unprotected
Report findings in a structured summary with file locations.Pattern 2: Parallel Investigation
Section titled “Pattern 2: Parallel Investigation”Use sub-agents to simultaneously:1. Agent A: Find all files that import from src/auth/2. Agent B: List all API endpoints and their auth requirements3. Agent C: Check test coverage for auth-related code
Synthesize the results into a unified assessment.Pattern 3: Verification in Isolation
Section titled “Pattern 3: Verification in Isolation”Use a sub-agent to review the rate limiter implementationin src/middleware/rateLimit.ts.
Check for:- Race conditions in the sliding window logic- Correct Redis key expiration- Proper error handling when Redis is down- Edge cases around window boundariesBecause the reviewer runs in a fresh context, it’s unbiased by the implementation decisions made during coding.
Pattern 4: Research → Summary → Implementation
Section titled “Pattern 4: Research → Summary → Implementation”Phase 1 (sub-agent):Research how webhooks are handled. Document the flow,key files, and patterns in .sdlc/research/webhooks.md.
Phase 2 (main agent, after sub-agent completes):Read .sdlc/research/webhooks.md and create an implementationplan for adding Stripe webhook support.Custom Sub-Agent Definitions
Section titled “Custom Sub-Agent Definitions”For recurring patterns, define custom agents in your tool’s agent directory. See the Tool Configuration Reference for your tool’s agent directory location.
---name: codebase-researcherdescription: Researches codebase patterns and produces structured summariestools: Read, Grep, Glob, Bash# use a cost-efficient model for this task---
Research the topic provided and produce a structured summary:
## Output Format### Overview[2-3 sentence summary]
### Key Files| File | Purpose | Relevance ||------|---------|-----------|
### Code Flow[Step-by-step description of how the code works]
### Patterns Used[Design patterns, conventions, and architectural decisions]
### Potential Issues[Any concerns or areas that need attention]Using a smaller/faster model for research agents is cost-effective — they primarily read and summarize, which doesn’t require the full reasoning capability of a frontier model.
Sub-Agent Communication
Section titled “Sub-Agent Communication”Sub-agents communicate with the main agent through:
- Return value — the summary at the end of their task
- File artifacts — writing to
.sdlc/research/orNOTES.md - Git commits — for implementation agents in worktrees
Anti-Patterns
Section titled “Anti-Patterns”Over-Delegation
Section titled “Over-Delegation”Don’t use sub-agents for simple, focused tasks. The overhead of spawning a sub-agent isn’t worth it when you just need to read one file.
# Overkill — just read the file directlyUse a sub-agent to check what's in config.ts
# Appropriate — this explores many filesUse a sub-agent to understand how configuration flowsfrom config.ts through the applicationMissing Context in Delegation
Section titled “Missing Context in Delegation”Sub-agents don’t inherit your conversation context. Provide enough information for them to succeed:
# Bad — sub-agent has no contextInvestigate the auth bug.
# Good — sub-agent has what it needsInvestigate why token refresh fails after session timeout.The auth middleware is in src/middleware/auth.ts.Token storage uses Redis (src/lib/redis.ts).The error occurs when refreshToken() returns null.Key Takeaways
Section titled “Key Takeaways”- Sub-agents provide 90%+ context savings for exploration tasks
- Use them for research, parallel investigation, and unbiased review
- Define custom agents for recurring patterns (research, review, testing)
- Use a cost-efficient model for read-only tasks to save cost
- Write to files for information that must survive compaction
- Provide sufficient context when delegating — sub-agents don’t inherit history