← Back to Capabilities
🧹

Context Entropy Solution — 5 Compaction Strategies

How to prevent AI from going crazy in long sessions
P1Infrastructure
Summary
Claude Code's solution to 'context entropy' — the tendency for agents to hallucinate as sessions grow. Uses 5 context compaction strategies, 14 cache-break vectors, and a 46,000-line query engine. Multi-agent orchestration fits in a prompt rather than a framework.
Technical Details
Core design insight: context window is a scarce resource. • Memory index is cheap → always loaded • Topic files are expensive → fetched on demand • Transcripts are heaviest → never loaded, only searched Also includes: prompt-shape surgery for Capybara v2 (force safe boundary markers, relocate risky blocks, smoosh reminder text into tool results, add non-empty markers for empty tool outputs). Production bug: autocompact had 1,279 sessions with 50+ consecutive failures (up to 3,272 in one session), wasting ~250K API calls/day globally. Fix was 3 lines: MAX_CONSECUTIVE_AUTOCOMPACT_FAILURES = 3.
Implementation Pattern
TypeScript (conceptual)
// Context compaction strategy (conceptual)
const MAX_CONSECUTIVE_AUTOCOMPACT_FAILURES = 3; // The 3-line fix

interface CompactionStrategy {
  name: string;
  trigger: () => boolean;
  compact: (context: Message[]) => Message[];
}

// 5 strategies for managing context entropy:
const strategies: CompactionStrategy[] = [
  { name: 'summary',    /* summarize old messages */ },
  { name: 'truncate',   /* drop oldest messages */ },
  { name: 'selective',  /* keep only tool results */ },
  { name: 'dedup',      /* remove repeated content */ },
  { name: 'tiered',     /* memory index vs full content */ },
];

// Prompt-shape surgery for Capybara compatibility
function sanitizePromptShape(messages: Message[]): Message[] {
  // Force safe boundary markers
  // Relocate risky blocks
  // Add non-empty markers for empty tool outputs
}
Architecture Insight
The 250K wasted API calls/day bug is a masterclass in why failure budgets matter. A 3-line fix (max consecutive failures = 3) saved enormous resources. Always add failure limits to retry loops.
Official / Public Basis
Context compaction strategies observable in official Claude Code behavior. autocompact bug details (250K wasted API calls/day) found in source. 3-line fix: MAX_CONSECUTIVE_AUTOCOMPACT_FAILURES = 3.
Governance Concerns
Context window is a scarce resource — long sessions degrade quality. Compaction must preserve critical context while discarding noise. Resource monitoring (250K wasted calls/day) shows importance of failure budgets.
LightHope Ecosystem Mapping
LightHope — session efficiency for long tutoring sessions, context management for complex project workflows, resource-aware architecture design