In short.
Compaction on demand is a developer guide to shortening Claude conversation histories when an application chooses, but its beta workflow replaces original messages with a summary that cannot retain every kind of content. Applications must preserve a signed summary block unchanged, put it first on later requests and remove exactly the messages it summarizes. The page covers custom instructions, SDK integration, errors and billing. Summary fidelity, runtime reliability, latency and net cost savings are not assessed.
Researched and written by an AI model and checked by a second AI model. How it works
Coverage is the complete supplied text of [https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand.md], including examples, warnings, errors, costs, feature interactions and availability tables. Linked pages and the external diagram were not independently inspected. SDK behavior and live API availability were not verified, and no examples were executed. Numerical pricing, licensing and measured summary quality are not established.
The overview
identity
This is Anthropic's documentation for application-triggered conversation compaction in the Claude Messages API. The selected source is [https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand.md]. It teaches a beta API feature through request examples, response structures, implementation guidance and reference tables.
assessment
The guide is useful when an application needs to choose when a growing conversation becomes a shorter summary. Its practical value is the detail around replacing history correctly, handling failures and continuing the conversation afterward.
The central trade-off is reduced detail: subsequent requests receive the summary instead of the original messages. The documentation does not establish how reliably summaries preserve requirements, decisions or subtle details, nor does it quantify compression ratios or savings.
users
The intended readers are developers building conversational applications and agents that maintain message history across requests. Beginners need familiarity with API authentication, request parameters, conversation roles and error handling. Experienced developers will find details about tool runners, caching, streaming, preserved thinking and incompatible parameters.
The page includes examples for cURL, the Anthropic CLI, Python, TypeScript, C#, Go, Java, PHP and Ruby. The repeated conversation loop requires application logic; the page directs shell users toward an SDK for that workflow.
capabilities
An application can request a summary after a completed turn and decide its own trigger, such as an estimated input-token budget. A non-blank instructions string, limited to 16,384 characters, replaces the default summarization prompt entirely. This lets developers specify which facts the summary should retain.
A conversation can be compacted repeatedly: each new summary covers the previous summary and subsequent messages, and only the newest block is retained. Streaming delivers the completed block in one content_block_start event followed by content_block_stop, without incremental content_block_delta events.
Using it
requirements
The feature requires a supported model and the compact-2026-09-04 beta header on both the summarization request and every later request carrying its signed block. The page recommends checking capabilities.compaction through the Models API with that header. The conversation must still fit the model's context window when compaction is requested.
The availability table lists beta support through Claude API, Claude Platform on AWS, both managed Google Cloud offerings, Microsoft Foundry and Amazon Bedrock. Model availability differs by platform. For example, claude-opus-5-5 appears in all listed platform groups, while claude-sonnet-4-6 is absent from the Bedrock list. Account access and provider-specific setup are not established by this page.
mechanism
A separate Messages API request sends the conversation with compaction set to {"type":"summarize"}. A successful summarization returns one compaction block containing readable summary text and a signature, with stop_reason set to compaction. It produces no conversational reply. The summarizer reads the system prompt, tool definitions and earlier thinking; tools are not executed during summarization.
The returned block replaces exactly the messages submitted for summarization. It must come first in messages, either in its own assistant message or as the first content block of the first message. Later turns follow unchanged. Every subsequent request must contain exactly one unchanged compaction block, including its signature.
setup
Send the same model, system prompt and tools used for the conversation, add the beta header and compaction parameter, and allow enough max_tokens for both internal thinking and the written summary. The examples use 4,096 tokens for summarization; this is an example setting, not a guaranteed sufficient budget.
Check stop_reason before replacing history. HTTP 200 can contain empty content when summarization fails to produce usable text. Keep the original history when no summary is returned. Missing the block on a later request silently removes the summary from Claude's context; leaving summarized messages after it silently sends those messages again.
The SDK tool runner can schedule compaction after the current turn and tool calls finish, but its configuration must include the beta header. The page identifies runner parameter-filtering problems in versions before Python 1.8.0, TypeScript 0.128.0, C# 12.50.0, Go 1.75.0 and Java 2.65.0. For Python serialization, it recommends to_dict() or model_dump(exclude_none=True) to avoid adding rejected null fields.
example
The documentation illustrates a recipe-app design conversation. Its sample summary retains the agreed Recipe, Ingredient, Step and RecipeIngredient entities, the quantity-and-unit relationship, and the user's pending request for Recipe fields. Subsequent messages continue the design conversation after the signed block.
A longer sample loop estimates the next request's conversation size from the previous response's input and output tokens and requests compaction above 2,500 tokens. The page explicitly calls this threshold deliberately low for demonstration. These are documentation examples, not observed execution results.
Assessment & evidence
limitations
Compaction cannot be combined with context_management in the same request. Summarization requests also reject stop_sequences, structured-output output_config.format, forced tool choices of type any or tool, and output_config.task_budget.remaining. The remaining task-budget value is also rejected on later requests carrying a signed block. An unfinished assistant tool call needs its results before compaction.
Images, documents, container_upload blocks and fetched URLs within replaced messages are no longer available after the swap. Needed material must be restated or uploaded again. Compaction must happen before the conversation exceeds the context window, with room left for the summarization prompt.
An empty response can result from token limits, insufficient context space, a tool-use response, refusal or an empty end_turn. Invalid signatures, modified content, misplaced blocks and extra serialized fields can produce errors. The page documents recovery approaches but provides no reliability measurements.
reasoning
The documented mechanism suggests a useful fit for applications that need explicit control over when to summarize and what to retain. That assessment follows from the configurable trigger and replacement prompt; it does not establish that important details survive a particular workload or repeated compaction.
Instruction continuity needs separate attention. System messages inside the summarized range stop applying as instructions after replacement, and their per-message effort levels do not carry over. The page says to restate needed instructions after the next new user turn. Matching system and tools is also a documented condition for preserving thinking in retained turns; the full linked conditions are outside the supplied evidence.
alternatives
The page contrasts threshold compaction, whose block follows the summarized messages and lets the API drop them automatically, with on-demand compaction's explicit history replacement. Threshold compaction cannot run on a request carrying an on-demand signed block.
It also describes related approaches that preserve recent turns word for word or let conversation turns continue while a summary is written. These may address needs for exact recent context or overlapping work. Their linked documentation is outside the supplied scope, so implementation details and performance comparisons are not assessed.
evidence
The evidence is the complete supplied Anthropic documentation page, including sample requests and responses, history rules, error tables, billing explanations and availability listings. Its code and token figures are illustrative. No code or API requests were executed; runtime correctness, summary fidelity and performance remain untested.
Access & freshness
costs
Summarization is billed and rate-limited like an ordinary request. Its consumption appears in the compaction entry of usage.iterations; the top-level input_tokens and output_tokens are zero because the request generated no conversational reply. Conversation accounting therefore needs to sum the iterations.
A summarization attempt that produces no summary can still be billed, with zero usage when no call could be made. Sending an existing block back adds no separate compaction charge. Monetary rates, typical summary sizes and net savings are unknown from this page.
rights
The supplied page does not specify a licence or redistribution permission for its documentation and sample code. Service terms, rights in generated summaries and data-retention terms are also not established by this evidence.
freshness
The supplied page was captured at 2026-11-25T04:47:30.105Z and labels the feature beta under compact-2026-09-04. Its cURL example uses API version 2026-10-21. These identifiers do not establish a publication or last-update date, neither of which is supplied. Availability and compatibility are documented for this snapshot rather than independently confirmed against a live service.
actions
Confirm model support on the intended provider, choose a compaction threshold below the actual context limit and define the facts a summary must retain. When estimating request size with caching enabled, include cache_read_input_tokens and cache_creation_input_tokens as well as ordinary input and output tokens, or use token counting.
Before adoption, assess representative conversations for lost requirements, unresolved requests and repeated-summary drift. Verify exact block preservation, failure handling and usage accounting. For work that depends on original media or precise source wording, retain that material separately and resend it when needed. Measure total billed usage before assuming compaction saves money.
