Skip to main content

Compaction

Server-side context compaction for managing long conversations that approach context window limits.
Server-side compaction is the recommended strategy for managing context in long-running conversations and agentic workflows. It handles context management automatically with minimal integration work.
Compaction extends the effective context length for long-running conversations and tasks by automatically summarizing older context when approaching the context window limit. This is ideal for:
  • Chat-based, multi-turn conversations where you want users to use one chat for a long period of time
  • Task-oriented prompts that require a lot of follow-up work (often tool use) that may exceed the 200K context window
Compaction is currently in beta. Include the beta header compact-2026-01-12 in your API requests to use this feature.

Supported models

Compaction is supported on the following models:
  • Claude Opus 4.6 (claude-opus-4-6)

How compaction works

When compaction is enabled, Claude automatically summarizes your conversation when it approaches the configured token threshold. The API:
  1. Detects when input tokens exceed your specified trigger threshold.
  2. Generates a summary of the current conversation.
  3. Creates a compaction block containing the summary.
  4. Continues the response with the compacted context.
On subsequent requests, append the response to your messages. The API automatically drops all message blocks prior to the compaction block, continuing the conversation from the summary. Compaction flow diagram

Basic usage

Enable compaction by adding the compact_20260112 strategy to context_management.edits in your Messages API request.

Parameters

Trigger configuration

Configure when compaction triggers using the trigger parameter:

Custom summarization instructions

By default, compaction uses the following summarization prompt:
You can provide custom instructions via the instructions parameter to replace this prompt entirely. Custom instructions don’t supplement the default; they completely replace it:

Pausing after compaction

Use pause_after_compaction to pause the API after generating the compaction summary. This allows you to add additional content blocks (such as preserving recent messages or specific instruction-oriented messages) before the API continues with the response. When enabled, the API returns a message with the compaction stop reason after generating the compaction block:

Enforcing a total token budget

When a model works on long tasks with many tool-use iterations, total token consumption can grow significantly. You can combine pause_after_compaction with a compaction counter to estimate cumulative usage and gracefully wrap up the task once a budget is reached:
Python

Working with compaction blocks

When compaction is triggered, the API returns a compaction block at the start of the assistant response. A long-running conversation may result in multiple compactions. The last compaction block reflects the final state of the prompt, replacing content prior to it with the generated summary.

Passing compaction blocks back

You must pass the compaction block back to the API on subsequent requests to continue the conversation with the shortened prompt. The simplest approach is to append the entire response content to your messages:
When the API receives a compaction block, all content blocks before it are ignored. You can either:
  • Keep the original messages in your list and let the API handle removing the compacted content
  • Manually drop the compacted messages and only include the compaction block onwards

Streaming

When streaming responses with compaction enabled, you’ll receive a content_block_start event when compaction begins. The compaction block streams differently from text blocks. You’ll receive a content_block_start event, followed by a single content_block_delta with the complete summary content (no intermediate streaming), and then a content_block_stop event.

Prompt caching

You may add a cache_control breakpoint on compaction blocks, which caches the full system prompt along with the summarized content. The original compacted content is ignored.

Understanding usage

Compaction requires an additional sampling step, which contributes to rate limits and billing. The API returns detailed usage information in the response:
The iterations array shows usage for each sampling iteration. When compaction occurs, you’ll see a compaction iteration followed by the main message iteration. The final iteration’s token counts reflect the effective context size after compaction.
The top-level input_tokens and output_tokens do not include compaction iteration usage—they reflect the sum of all non-compaction iterations. To calculate total tokens consumed and billed for a request, sum across all entries in the usage.iterations array.If you previously relied on usage.input_tokens and usage.output_tokens for cost tracking or auditing, you’ll need to update your tracking logic to aggregate across usage.iterations when compaction is enabled. The iterations array is only populated when a new compaction is triggered during the request. Re-applying a previous compaction block incurs no additional compaction cost, and the top-level usage fields remain accurate in that case.

Combining with other features

Server tools

When using server tools (like web search), the compaction trigger is checked at the start of each sampling iteration. Compaction may occur multiple times within a single request depending on your trigger threshold and the amount of output generated.

Token counting

The token counting endpoint (/v1/messages/count_tokens) applies existing compaction blocks in your prompt but does not trigger new compactions. Use it to check your effective token count after previous compactions:

Examples

Here’s a complete example of a long-running conversation with compaction:
Here’s an example that uses pause_after_compaction to preserve the last two messages (one user + one assistant turn) verbatim instead of summarizing them:

Current limitations

  • Same model for summarization: The model specified in your request is used for summarization. There is no option to use a different (e.g., cheaper) model for the summary.

Next steps

Compaction cookbook

Explore practical examples and implementations in the cookbook.

Context windows

Learn about context window sizes and management strategies.

Context editing

Explore other strategies for managing conversation context like tool result clearing and thinking block clearing.