Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Book: Claude Code VS OpenCode: Architecture, Design and The Road Ahead Chapter: 11 — Claude Code’s Commercial Design Model: openai/gpt-5.4 Generated: 2026-04-01 Token Usage: unavailable in current environment

11.5 Multi-Strategy Compaction

Claude Code’s context-management system is commercially important because it does not rely on a single “summarize the conversation” fallback. Instead, it uses multiple compaction mechanisms distributed across services/compact/ and services/contextCollapse/. The best way to understand it is as a five-layer defense against context-window exhaustion.

The first layer is auto-compact, implemented in autoCompact.ts. This is the proactive threshold-based system. Claude Code estimates token usage, reserves headroom for summary output, computes an effective context window, and triggers compaction before the session reaches a blocking limit. This is the standard commercial answer to long conversations: compress before failure, not after failure. The important point is that auto-compact is policy-driven. It can be enabled, disabled, and tuned against context-window thresholds.

The second layer is snip-compact, associated here with history snipping. Conceptually, snipping removes or trims older context segments rather than fully re-summarizing everything. In product terms, this is a lower-latency, more surgical response to context bloat. Instead of immediately producing a large replacement summary, the runtime can discard or replay only the history segments that are no longer worth their token cost. This keeps the working set small while preserving recent conversational fidelity.

The third layer is micro-compact, implemented in microCompact.ts. This mechanism operates below the level of full conversation summaries. It focuses on compacting bulky tool results from selected tools such as file reads, shell output, grep, glob, web fetch, web search, and file edits. That is a crucial insight. In coding-agent sessions, the biggest context offender is often not user dialogue but verbose tool output. Micro-compaction therefore attacks the real payload inflation source: large, stale, low-value tool results.

The fourth layer is session memory compaction, implemented in sessionMemoryCompact.ts. Claude Code maintains session memory as a separate artifact, then compresses or truncates it when needed while preserving API invariants such as tool-use and tool-result pairing. This is a more advanced idea than ordinary conversation summarization. It recognizes that memory itself can become oversized and must therefore be compacted as an object in its own right. In other words, Claude Code manages both the visible transcript and the hidden memory substrate.

The fifth layer is context collapse, exposed through services/contextCollapse/. Even though some external builds stub parts of this area, the architectural idea is clear: old messages are progressively converted into committed summaries or collapsed context structures. This is not just “compaction after overflow.” It is a long-horizon strategy for turning raw history into compressed state. The distinction matters. Compaction is reactive; collapse is structural.

What makes this system impressive is the combination. Auto-compact handles predictable growth. Snip-compact removes low-value history segments. Micro-compact shrinks tool-result payloads. Session-memory compact reduces persistent memory inflation. Context collapse turns aging conversation into durable summary state. Each mechanism is addressing a different failure mode.

Commercially, this is a much stronger approach than one-shot summarization. Large agent sessions fail in several ways: too many messages, too much tool output, too much remembered state, or too much long-tail history. A single summarizer is rarely optimal across all four. Claude Code’s architecture therefore resembles storage hierarchy design in systems engineering: short-term context, cached artifacts, compressed memory, and collapsed history each play distinct roles.

This also connects directly to cost and reliability. Smaller active context means lower token spend, fewer prompt-too-long failures, and more predictable latency. It also makes high-autonomy modes more viable because the system can keep working deep into long-running sessions instead of degrading suddenly. OpenCode and Oh-My-OpenCode also care deeply about context engineering, but Claude Code’s commercial edge here is the breadth of mechanisms assembled behind one user-facing experience.

The larger lesson is that future agent systems will need context hierarchies, not just context windows. Claude Code’s multi-strategy compaction stack shows one path forward: treat conversational history, tool artifacts, memory files, and long-tail transcript state as different data classes, then compress each one differently.