Context Management Best Practices for Agents
Purpose: Proactive token usage optimization through strategic compacting and subagent isolationOverview
Claude Code agents have access to 200K token context windows, but long-running tasks can accumulate significant context. This guide provides strategies for managing context efficiently.Two Primary Strategies
1. Manual Compacting (/compact command)
Summarize and compress conversation history at strategic points
2. Subagent Isolation (Task tool)
Spawn independent agents with fresh context windowsStrategy 1: When to Recommend /compact
Trigger Points
Recommend compacting to the user when: ✅ Transitioning Between Major PhasesWhen NOT to Compact
❌ Mid-Implementation - Lose important working context ❌ During Active Debugging - Need full error trace history ❌ Iterating on Specific Feature - Context is still relevant ❌ Just Started Session - No accumulated context yetHow to Recommend
Use this format when recommending:Strategy 2: Use Subagents for Context Isolation
When to Use Subagents
Use the Task tool instead of continuing in same context when: ✅ Independent Work ModulesSubagent Benefits
- Fresh Context: Each subagent starts with clean 200K tokens
- Focused Work: Subagent only sees relevant task details
- Natural Summary: Parent gets concise summary when subagent completes
- Parallel Potential: Can spawn multiple subagents for different tasks
- Automatic Cleanup: Subagent context discarded after completion
Subagent Usage Pattern
Pattern 1: Isolated ImplementationContext Management in Agent Workflows
Add to All Agents
Each agent should include a context management section:Implementation Guidelines
For Agent Developers
When updating agents with context management:- Add Section: Include “Context Management” section after main workflow
- Strategic Points: Identify natural breakpoints in agent workflow
- Recommend, Don’t Command: Suggest
/compact, don’t require it - Provide Context: Explain WHY compacting would help
- Subagent Patterns: Show WHEN to use Task tool vs continue
Template for Agents
Measuring Context Usage
Rough Estimates
- Average message: ~500-1000 tokens
- File read (200 lines): ~1500-2000 tokens
- Code generation: ~2000-5000 tokens per feature
- Large analysis: ~10000-20000 tokens
High Context Indicators
You’re likely over 100K tokens if:- Read 20+ files in conversation
- Generated multiple large features
- Had extensive back-and-forth debugging
- Performed multiple codebase searches
- Generated comprehensive documentation
Best Practices Summary
✅ Do
- Recommend compacting at natural breakpoints
- Use subagents for isolated work
- Preserve key decisions when compacting
- Explain benefits to user
- Monitor for high context scenarios
❌ Don’t
- Recommend compacting mid-implementation
- Overuse compacting (not every task)
- Use subagents for trivial tasks
- Interrupt user flow unnecessarily
- Mandate compacting (always suggest)
Examples
Example 1: Phase Transition
Example 2: Using Subagent
Example 3: Agent Handoff
Monitoring and Optimization
Track These Metrics
- How often compacting is recommended
- User acceptance rate of recommendations
- Subagent usage patterns
- Context-related performance issues
Iterate Based On
- User feedback on recommendation frequency
- Actual token usage patterns
- Agent performance after compacting
- Subagent success rates
Summary
Context management is about balance:- Use
/compactat strategic phase transitions - Use subagents for naturally isolated work
- Recommend, don’t mandate
- Explain the “why” to users
- Keep parent context clean and focused