Claude Platform Docs
MessagesContext management

Compaction overview

Learn what compaction does, how compaction on demand and compaction at a token threshold differ, and which page covers your task.

Compaction replaces the older turns of a conversation with a summary that Claude writes on the server, so you need no summarization code of your own. It keeps a long conversation or agent task inside the context window, and it keeps the active context small, because response quality degrades as a conversation grows.

If you want to skip this overview, start with Compaction on demand to add compaction to your application. While both it and Compaction at a token threshold are in beta, Compaction on demand covers more common use cases. To clear old tool results or old thinking blocks by rule instead of summarizing them, see Context editing.

Choose how to compact

Use on-demand compaction wherever it is available.

Compaction on demandCompaction at a token thresholdYour own summarizer (Compact on the client)
Who decides whenYou, by sending a requestThe API, when input tokens reach the trigger you setYou
Code you writeA compaction loop that requests the summary and swaps it inOne parameter on your ordinary requestsThe summarization call, its prompt, and the history rewrite
What you send back afterwardThe returned block first in messages, in place of the messages it summarizesThe response, appended as usual. The API drops what came before the blockYour summary, as a message of your own
Recent turns stay word for wordYes: Compaction that keeps recent turnsYes, by pausing after compaction and re-inserting themYes
Runs in the backgroundYes: Compaction in the backgroundNo: it runs inside the request that reaches the thresholdYes, in your own code
Kept turns keep their thinking, on models with preserved thinkingYes, under the conditions in Compaction and preserved thinkingNo: on Claude Fable 5.1 and Claude Opus 5.5, remove or drop the thinking in turns you re-insert (see the threshold compaction examples)No: that thinking fails the check
Beta header, parameter, and platformscompact-2026-09-04 and the top-level compaction parameter. Platforms: see the on-demand Compatibility listSee the threshold Compatibility list and Basic usageNone. It runs in your code
Choose it whenYour application needs to control when compaction happens, can't pause while a summary is written, or must keep recent turns and their thinkingYou want the API to manage context inside ordinary requestsYou already run your own summarizer and it replaces the whole history with the summary

On-demand compaction options

Compaction on demand shows a loop that summarizes the whole conversation while your application waits. The first two pages in this list change how that loop runs, and you can combine them. The third applies if you send thinking blocks back and do either.

Find these topics on other pages:

Threshold compaction has its own page: Compaction at a token threshold.

Was this page helpful?