Context Compaction
A technique to manage AI context overflow in long-running tasks by selectively preserving critical information while discarding less relevant details. Solves the core limitation of fixed context windows in models like claude during complex code generation.
Key Principles
- Replaces “one-shot” coding with stepwise execution (e.g., generating one function at a time instead of entire modules)
- Summarizes progress after each step (e.g., “Implemented
calculate_totalwith test coverage”) - Preserves decision logic while pruning redundant code/output
- Maintains task coherence across multiple agent interactions
Workflow Implementation
- Break feature into atomic subtasks (e.g., “Write auth module → test → deploy”)
- After each step, generate compact context summary (max 20% of original context)
- Use summary as new context for next step
- Never exceed context window by design
Why It Works
- Avoids context-window saturation during extended sessions
- Maintains model’s task understanding through progressive summarization
- Enables long-running agents to handle complex projects (e.g., full application builds)
- Proven effective for AI Agent code generation in production environments
Related Concepts
- AI Agent
- context-window
- ai-coding
- Stepwise Problem Solving
2026 04 14 Fixing long running Claude code sessions