Skip to main content

Automatic Context Management — Context Compaction

[ ABORT TO HUD ]
SEQ. 1

Automatic Context Management

🗜️ Context Compaction⏱ 15 min⭐ 1500 BASE XP⌨ HANDS-ON LAB

Managing Long Contexts

In multi-turn agentic workflows, conversation history can grow rapidly. Context Compaction summarizes historical turns into information-dense, cryptographically anchored blocks—keeping sessions fast and economical while preserving critical task state and decisions.

Messages API Compaction Modes (On-Demand vs. Threshold Auto-Summarization)

Anthropic provides two server-side compaction modes in the Messages API: On-Demand Compaction (anthropic-beta: compact-2026-09-04 via top-level compaction: { type: "summarize" }, returning a signed block with stop_reason: "compaction" to round-trip first in subsequent messages) and Threshold Auto-Compaction (anthropic-beta: compact-2026-01-12 via context_management.edits):

// 1. On-Demand Compaction (compact-2026-09-04)
const summaryTurn = await anthropic.beta.messages.create({
  model: "claude-sonnet-5-5",
  max_tokens: 4096,
  betas: ["compact-2026-09-04"],
  compaction: { type: "summarize" },
  messages: conversationHistory
});

// 2. Threshold Auto-Compaction (compact-2026-01-12)
const autoManaged = await anthropic.beta.messages.create({
  model: "claude-sonnet-5-5",
  max_tokens: 8192,
  betas: ["compact-2026-01-12"],
  context_management: {
    edits: [{ type: "compact_20260112", trigger_threshold: 150000 }]
  },
  messages: conversationHistory
});

Client vs Server Context Strategies

StrategyMechanicsBest For
Sliding WindowRetain the last N messages and discard earlier turnsSimple stateless chats
Server-Side Compaction (Beta)API natively distills middle turns into cryptographically signed summary blocksLong-horizon agent sessions & coding loops
Prompt Caching PrefixCache unchanging system and reference documents while compacting conversation historyHigh-frequency RAG agents
💡 SOE / AOE Architecture Tip: In enterprise production harnesses, combine compact-2026-09-04 or compact-2026-01-12 with Prompt Caching. Keep your system prompt and tool definitions locked in the static cache prefix, allowing conversation turns to be seamlessly compacted without invalidating the 90%–97.5% cached prefix discount.
⌨ HANDS-ON LABCompact High-Context Sessions
⭐ +200 XP

Long conversations accumulate stale tokens. Invoke context compaction via the beta compaction header to prune and summarize older turns.

1POST to /v1/messages with the compact-2026-09-04 beta header and top-level compaction: {"type": "summarize"}.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 1 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 3
Why is structured compaction preferred over simple message truncation in complex agents?
It uses more tokens
It preserves critical historical decisions and state while reclaiming context space
It runs faster on the GPU
It bypasses rate limits