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_total with test coverage”)
  • Preserves decision logic while pruning redundant code/output
  • Maintains task coherence across multiple agent interactions

Workflow Implementation

  1. Break feature into atomic subtasks (e.g., “Write auth module → test → deploy”)
  2. After each step, generate compact context summary (max 20% of original context)
  3. Use summary as new context for next step
  4. 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

2026 04 14 Fixing long running Claude code sessions