Automatic Context Management — Context Compaction
Automatic Context Management
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
| Strategy | Mechanics | Best For |
|---|---|---|
| Sliding Window | Retain the last N messages and discard earlier turns | Simple stateless chats |
| Server-Side Compaction (Beta) | API natively distills middle turns into cryptographically signed summary blocks | Long-horizon agent sessions & coding loops |
| Prompt Caching Prefix | Cache unchanging system and reference documents while compacting conversation history | High-frequency RAG agents |
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.Long conversations accumulate stale tokens. Invoke context compaction via the beta compaction header to prune and summarize older turns.