II. Context Loading
II
Load context when needed, not all at once. Never exceed 40%.
The Problem
::: danger Context Collapse When context exceeds ~40%, quality degrades suddenly—not gradually. You'll see:
- Inconsistent outputs
- Forgotten instructions
- Hallucinated details
- Lost focus on goals :::
Monolithic Context
Main Agent: 80% full
- Task goals — 5%
- Codebase exploration — 25%
- Research findings — 20%
- Implementation details — 15%
- Validation results — 10%
Result: Context collapse
JIT Loading
Main Agent: 15% full
- High-level goals — 5%
- Orchestration logic — 5%
- Compressed summaries — 5%
Result: Clean, focused
The Solution
Sub-Agent Architecture
Main Agent
10-20% context
Orchestrates only
Explorer
Fresh context
Returns summary
Implementer
Fresh context
Returns diff
Validator
Fresh context
Returns pass/fail
::: tip Key Insight Each sub-agent gets a fresh context window. Deep work happens in isolation. Only compressed results flow back to main. :::
The 40% Rule
| Context Level | Status | Action |
|---|---|---|
| 0-30% | Optimal | Continue working |
| 30-40% | Caution | Consider delegation |
| 40-60% | Degraded | Spawn sub-agent |
| 60%+ | Collapse | Stop, compress, restart |
::: warning Why 40%? This threshold appears across cognitive systems: ADHD hyperfocus, LLM coherence, database performance. It's not arbitrary—it's empirical. :::
Implementation
::: code-group
# Instead of exploring in main context:
# Use Task tool to spawn explorer
Task(
prompt="Find all auth-related files",
subagent_type="Explore"
)
# Returns: 3-5 line summary, not full content
# Sub-agent returns compressed insight:
"Auth handled in src/auth/:
- middleware.ts (JWT validation)
- routes.ts (login/logout)
- types.ts (User interface)"
# NOT the full 500-line files
# Research phase → fresh context
# Plan phase → fresh context
# Implement phase → fresh context
# Each starts clean
:::
Real Example
Session Start: 0% context
1. User: "Add rate limiting to API"
2. Main agent (5% context):
→ Spawns Explorer sub-agent
3. Explorer (fresh context):
→ Reads 15 files
→ Returns: "API routes in src/routes/api.ts,
uses Express middleware pattern"
→ Context discarded
4. Main agent (10% context):
→ Has summary, not 15 files
→ Spawns Implementer
5. Implementer (fresh context):
→ Reads api.ts + rate-limit docs
→ Returns: diff + "Added express-rate-limit"
→ Context discarded
6. Main agent (15% context):
→ Clean, focused, successful
Checklist
- Main agent stays under 20% context
- Delegate exploration to sub-agents
- Sub-agents return summaries, not full content
- Fresh context for each workflow phase
- Compress before crossing 40% threshold
Related Factors
| Factor | Relationship |
|---|---|
| I. Automated Tracking | Git stores what context discards |
| III. Focused Agents | Sub-agents do one job |
| VI. Resume Work | Bundles restore context across sessions |