compaction-thinking-blocks changedbuild-with-claude/compaction-thinking-blocks
Nearest release: v2.1.323, published 4 hours after this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Recorded here
Lines+43added
Lines−41removed
From line
1
where the diff opens
First seen
21 Sep 2026
this site's first read of the page
Recorded edits17to this page, all time
## When your code writes the summary
The whole hunk
from line 1, old and new numbered
/
from line 1
11---
22title: Compaction and preserved thinking
33url: https://platform.claude.com/docs/en/build-with-claude/compaction-thinking-blocks
4description: When thinking blocks in turns kept after on-demand compaction stay valid on models with preserved thinking, and how to check.
4description: When thinking in turns kept after compaction, or after your own summary or cut, stays valid on models with preserved thinking, and how to check.
55featureMetadata:
66 status: beta
77 betaHeader: compact-2026-09-04
from line 29
2929 Microsoft Foundry: beta
3030---
3131
32Skip this page unless you send thinking blocks back to a model with [preserved thinking](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking) and keep turns after the compaction block. Kept turns are the turns that follow the block. They’re either recent turns you left out of the compaction request, as in [Compaction that keeps recent turns](https://platform.claude.com/docs/en/build-with-claude/compaction-keep-recent-turns), or turns that arrived while the API was writing the summary, as in [Compaction in the background](https://platform.claude.com/docs/en/build-with-claude/compaction-background).
32Skip this page unless you send thinking blocks back to a model with [preserved thinking](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking) and keep turns after the compaction block, or after your own summary or cut. Kept turns are the turns that follow the block. They’re either recent turns you left out of the compaction request, as in [Compaction that keeps recent turns](https://platform.claude.com/docs/en/build-with-claude/compaction-keep-recent-turns), or turns that arrived while the API was writing the summary, as in [Compaction in the background](https://platform.claude.com/docs/en/build-with-claude/compaction-background).
3333
34Models with preserved thinking check earlier thinking blocks against the conversation that produced them. A summary replaces part of that conversation, but the check accepts the swap when the API wrote the summary, so the thinking in kept turns can stay valid.
34Models with preserved thinking check earlier thinking blocks against the conversation that produced them. A summary replaces part of that conversation, but the [prefix check](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks#prefix-check) accepts the swap when the API wrote the summary, so the thinking in kept turns can stay valid.
3535
3636## Conditions for kept thinking to stay valid
3737
3838The thinking blocks in kept turns stay valid while all of these hold:
3939
40* **The compaction request runs on a model with preserved thinking.** This condition covers every compaction request since a thinking block was produced, not only the most recent one. One way to meet it is to send every compaction request to the model the conversation uses.
41* **The kept turns directly follow the summarized messages, and you send them unchanged.** Send each kept message exactly as it is in your history. Don’t skip or add a message between the last summarized message and the first kept one. The first kept message must also have a different role from the last summarized message, and it can’t be a mid-conversation `role: "system"` message. Otherwise, the API merges it into the last summarized message. One way to get the first kept message right is to compact exactly the `messages` of a request you already sent. The kept turns then start with Claude’s reply to it.
42* **`system` and `tools` don’t change.** They are the same on the compaction request as on the requests that produced the kept thinking, and they stay the same on the requests that follow. The exception is a tool marked `defer_loading: true` that nothing in the conversation has referenced. [Change the system prompt or tools](https://platform.claude.com/docs/en/build-with-claude/compaction-thinking-blocks#change-the-system-prompt-or-tools) covers how to change them safely.
40* **The compaction request runs on a model with preserved thinking.** This condition covers every compaction request since a thinking block was produced, not only the latest. One way to meet it is to send every compaction request to the model the conversation uses.
41* **The kept turns directly follow the summarized messages, and you send them unchanged.** Send each kept message exactly as it is in your history, and don’t send summarized messages again. Don’t skip or add a message between the last summarized message and the first kept one. The first kept message must also have a different role from the last summarized message, and it can’t be a mid-conversation `role: "system"` message. Otherwise, the API merges it into the last summarized message. One way to get the first kept message right is to compact exactly the `messages` of a request you already sent. The kept turns then start with Claude’s reply to it.
42* **`system` and `tools` don’t change.** They are the same on the compaction request as on the requests that produced the kept thinking, and they stay the same on the requests that follow. The exception is a tool marked `defer_loading: true` that nothing has referenced. To change them safely, see [Change the system prompt or tools](https://platform.claude.com/docs/en/build-with-claude/compaction-thinking-blocks#change-the-system-prompt-or-tools).
4343
44If a condition doesn’t hold, the API still accepts the compaction block on later requests. What fails is the kept thinking, on the first later request that sends it. The compaction request runs the same check on the thinking blocks it carries, so a changed `system` or `tools`, or an edited message, can fail that request too. [What the API does with an invalid block](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking#mismatch-behavior) describes what happens then, and [When `prefix_mismatch_behavior` is unset](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking#enforcement) covers requests that don’t set the field, including Message Batches items.
44If a condition doesn’t hold, the API still accepts the compaction block on later requests. What fails is the kept thinking, on the first later request that sends it. The compaction request runs the prefix check on the thinking blocks it carries, so a changed `system` or `tools`, or an edited message, can fail that request too. For what happens then, see [Set `prefix_mismatch_behavior`](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks#preserved-thinking-controls).
4545
4646## Compact again without breaking older thinking
4747
4848You can [compact again](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand#compact-again) and keep turns: the new block covers the old summary and every message that follows it in the compaction request, and any turns you leave out of that request are kept turns of the new block.
4949
50The first of the [conditions for kept thinking](https://platform.claude.com/docs/en/build-with-claude/compaction-thinking-blocks#conditions-for-kept-thinking-to-stay-valid) counts every compaction since a thinking block was produced, so a turn that you keep through two compactions needs both to have run on a model with preserved thinking.
50For a turn’s thinking to stay valid through two compactions, both must have run on a model with preserved thinking.
5151
5252Compactions from before a thinking block was produced don’t count against it. Thinking produced after a block is in place is bound to that block, and it stays valid through later compactions that meet the conditions.
5353
5454## Change the system prompt or tools
5555
56A later request can use a different `system`, different `tools`, or a different model from the compaction request, and the API still accepts the block. Such a change can invalidate the thinking in the kept turns, but it has no other effect.
56A later request can use another `system`, `tools`, or model that supports on-demand compaction, and the API still accepts the block. The change can invalidate the kept thinking.
5757
5858To change `system` or `tools` without invalidating any kept thinking, compact the whole conversation first, so no turns are kept. Then change them on the next request.
5959
60To add an instruction or change the available tools without touching `system` or `tools`, append the change to `messages`, as described in [Make changes without editing the prefix](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking#replace-prefix-edits).
60To add an instruction or change the available tools without touching `system` or `tools`, append the change to `messages`, as described in [Change tools or instructions without failing the prefix check](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/change-tools-or-instructions).
6161
62The summarized turns can include mid-conversation system messages, which are `role: "system"` entries in `messages` (see [Mid-conversation system messages and tool changes](https://platform.claude.com/docs/en/build-with-claude/mid-conversation-system-messages)). They’re summarized with the rest. What happens next depends on what a message carried:
62The summarized turns can include mid-conversation system messages, which are `role: "system"` entries in `messages`. They’re summarized with the rest, so their text instructions and [per-message effort](https://platform.claude.com/docs/en/build-with-claude/effort-per-message) levels stop applying (see [Limits and interactions with other features](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand#how-it-fits-with-the-rest-of-the-api)). Their tool changes can carry over:
6363
64* **Text instructions** stop applying.
65* **A [per-message effort](https://platform.claude.com/docs/en/build-with-claude/effort-per-message) level** stops applying too. Turns then run at the request’s top-level `output_config.effort`. If the request sets none, they run at the model’s default. A later message can set a level again.
66* **Tool changes** can carry over. See the steps that follow.
67
68To carry tool changes over:
69
70641. Send the `inline-tools-2026-09-15` beta header on the compaction request. The returned block then records the changes in a `tool_changes` field.
712. Send the block back unmodified. The API then applies the recorded changes on each request that includes the block. If the field is missing, the API accepts the block but applies nothing.
652. Send the block back unmodified. The API then applies the recorded changes on each request that includes the block. If the field is missing, the API accepts the block but applies nothing, so state the changes again.
72663. Send the same header on every request that includes the block.
7367
74If the summarized turns changed your tools and the returned block has no `tool_changes` field, the changes weren’t recorded. State them again, as described next.
68You can state an instruction, an effort level, or a tool change again. Put it in a `role: "system"` message directly after the new `user` message of your next request. The request’s `messages` then run in this order:
7569
76You can state an instruction, an effort level, or a tool change again. Put it in a `role: "system"` message at the end of your next request, directly after the `user` message. The request’s `messages` then run in this order:
77
78701. The compaction block.
79712. The kept turns, if any.
80723. Your `user` message.
81734. The `role: "system"` message.
8274
83On later requests, leave those entries as they are, and add new turns after the system message.
75On later requests, leave those entries as they are, and add new turns after the system message. Don’t put the system message before or between the kept turns, where it invalidates every kept thinking block after it.
8476
85Don’t put the system message before or between the kept turns. There, it invalidates every kept thinking block that comes after it.
77## When your code writes the summary
8678
79This section applies wherever the [prefix check runs](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks#enforcement), with or without on-demand compaction.
80
81If your code writes the summary, the thinking in turns you keep after it fails the [prefix check](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks#prefix-check), because your summary replaced the messages that thinking came after. To keep that thinking, have the API write the summary.
82
83The same goes for turns you keep by [pausing after compaction](https://platform.claude.com/docs/en/build-with-claude/compaction-threshold#pausing-after-compaction) at a token threshold, turns Claude produced while your code summarized in the background, and turns after a cut.
84
85Remove those turns’ `thinking` and [`redacted_thinking`](https://platform.claude.com/docs/en/build-with-claude/thinking#redacted-thinking-blocks) blocks and keep their other blocks, or send [`"drop_block"`](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks#preserved-thinking-controls) on every request that includes them. Either way, Claude reads those turns without that thinking, and [output tokens can increase](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/fix-bound-to-a-different-conversation#drop-block-cost).
86
87If you send only your summary and the next instruction, as one user message, no earlier thinking is sent, and later thinking is valid.
88
8789## Check that the kept thinking held
8890
8991The compaction response doesn’t say whether the kept thinking holds. The first request after the swap does. To check in your tests:
9092
911. Have a short conversation with thinking on. Use a model that runs the prefix check (see [Keeping the prefix unchanged](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking#prefix-check)), and use it for every step, because a model that can’t read a thinking block drops it with no error.
931. Have a short conversation with thinking on. Use a [model that runs the prefix check](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/prefix-checks#enforcement), and use it for every step, because a model that can’t read a thinking block drops it with no error.
92942. Compact the older turns, and keep at least one turn that holds a thinking block.
93953. Send the next request, with the block first, then the kept turn, then a new `user` message, and with `thinking.block_binding.prefix_mismatch_behavior` set to `"error"`.
944. Read the result. A 200 response whose `input_transformations` array is empty means no thinking block failed the check or was dropped. A 400 error that says the block is bound to a different conversation means one did. The message starts with the path of the first block that failed, and [What the API does with an invalid block](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking#mismatch-behavior) shows it in full.
964. Read the result. A 200 response whose `input_transformations` array is empty means no thinking block failed the prefix check or was dropped. A [400 error](https://platform.claude.com/docs/en/api/errors#thinking-block-conversation-mismatch) that says the block is bound to a different conversation means one did. The message starts with the path of the first block that failed, as [Error: The block is bound to a different conversation](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/fix-bound-to-a-different-conversation#prefix-mismatch-error) shows.
9597
96The `prefix_mismatch_behavior` field needs the `thinking-binding-controls-2026-08-01` beta header in addition to the [`compact-2026-09-04` beta header](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand#request-a-summary). The value a request sets applies on any account.
98The `prefix_mismatch_behavior` setting needs the `thinking-binding-controls-2026-08-01` beta header in addition to the [`compact-2026-09-04` beta header](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand#request-a-summary).
9799
98100The following program runs the four steps. It prints how many thinking blocks the kept turn holds and how many entries `input_transformations` has; no entries means the kept thinking held:
99101
from line 117
115117 MODEL = "claude-fable-5-1"
116118 BETAS = ["compact-2026-09-04", "thinking-binding-controls-2026-08-01"]
117119 SYSTEM = "You help plan a recipe app's release. Keep answers short."
118 # With "error", a thinking block that fails the check makes the request fail with a 400.
120 # With "error", a thinking block that fails the prefix check makes the request fail with a 400.
119121 THINKING: BetaThinkingConfigParam = {
120122 "type": "adaptive",
121123 "block_binding": {"prefix_mismatch_behavior": "error"},
from line 196
194196 "thinking-binding-controls-2026-08-01"
195197 ];
196198 const systemPrompt = "You help plan a recipe app's release. Keep answers short.";
197 // With "error", a thinking block that fails the check makes the request fail with a 400.
199 // With "error", a thinking block that fails the prefix check makes the request fail with a 400.
198200 const thinking: Anthropic.Beta.Messages.BetaThinkingConfigParam = {
199201 type: "adaptive",
200202 block_binding: { prefix_mismatch_behavior: "error" }
from line 278
276278 const Model ModelId = Model.ClaudeFable5_1;
277279 AnthropicBeta[] betas = [AnthropicBeta.Compact2026_09_04, AnthropicBeta.ThinkingBindingControls2026_08_01];
278280 const string SystemPrompt = "You help plan a recipe app's release. Keep answers short.";
279 // With "error", a thinking block that fails the check makes the request fail with a 400.
281 // With "error", a thinking block that fails the prefix check makes the request fail with a 400.
280282 BetaThinkingConfigAdaptive thinking = new()
281283 {
282284 BlockBinding = new() { PrefixMismatchBehavior = BetaThinkingPrefixMismatchBehavior.Error },
from line 376
374376 anthropic.AnthropicBetaThinkingBindingControls2026_08_01,
375377 }
376378 system := []anthropic.BetaTextBlockParam{{Text: "You help plan a recipe app's release. Keep answers short."}}
377 // With "error", a thinking block that fails the check makes the request fail with a 400.
379 // With "error", a thinking block that fails the prefix check makes the request fail with a 400.
378380 thinking := anthropic.BetaThinkingConfigParamUnion{
379381 OfAdaptive: &anthropic.BetaThinkingConfigAdaptiveParam{
380382 BlockBinding: anthropic.BetaThinkingBlockBindingParam{
from line 483
481483 AnthropicBeta.THINKING_BINDING_CONTROLS_2026_08_01
482484 );
483485 static final String SYSTEM = "You help plan a recipe app's release. Keep answers short.";
484 // With "error", a thinking block that fails the check makes the request fail with a 400.
486 // With "error", a thinking block that fails the prefix check makes the request fail with a 400.
485487 static final BetaThinkingConfigAdaptive THINKING = BetaThinkingConfigAdaptive.builder()
486488 .blockBinding(BetaThinkingBlockBinding.builder()
487489 .prefixMismatchBehavior(BetaThinkingPrefixMismatchBehavior.ERROR)
from line 573
571573 const MODEL = Model::CLAUDE_FABLE_5_1;
572574 const BETAS = [AnthropicBeta::COMPACT_2026_09_04, AnthropicBeta::THINKING_BINDING_CONTROLS_2026_08_01];
573575 const SYSTEM = "You help plan a recipe app's release. Keep answers short.";
574 // With "error", a thinking block that fails the check makes the request fail with a 400.
576 // With "error", a thinking block that fails the prefix check makes the request fail with a 400.
575577 $thinking = BetaThinkingConfigAdaptive::with(
576578 blockBinding: BetaThinkingBlockBinding::with(
577579 prefixMismatchBehavior: BetaThinkingPrefixMismatchBehavior::ERROR,
from line 639
637639 );
638640
639641 // 4. A 200 with no dropped blocks means the kept thinking held.
640 printf("Dropped thinking blocks: %d\n", count($third->inputTransformations));
642 printf("Dropped thinking blocks: %d\n", count($third->inputTransformations ?? []));
641643 ```
642644
643645 ```ruby Ruby
from line 652
650652 Anthropic::AnthropicBeta::THINKING_BINDING_CONTROLS_2026_08_01
651653 ]
652654 SYSTEM = "You help plan a recipe app's release. Keep answers short."
653 # With "error", a thinking block that fails the check makes the request fail with a 400.
655 # With "error", a thinking block that fails the prefix check makes the request fail with a 400.
654656 THINKING = Anthropic::Beta::BetaThinkingConfigAdaptive.new(
655657 block_binding: Anthropic::Beta::BetaThinkingBlockBinding.new(
656658 prefix_mismatch_behavior: Anthropic::Beta::BetaThinkingPrefixMismatchBehavior::ERROR
from line 716
714716 )
715717
716718 # 4. A 200 with no dropped blocks means the kept thinking held.
717 puts "Dropped thinking blocks: #{third.input_transformations.length}"
719 puts "Dropped thinking blocks: #{(third.input_transformations || []).length}"
718720 ```
719721</CodeGroup>
720722
from line 725
723725Dropped thinking blocks: 0
724726```
725727
726In production, `"drop_block"` keeps requests succeeding when a condition doesn’t hold. The response lists each dropped block in `input_transformations` with `reason: "prefix_binding_mismatch"`. An entry whose `path` falls in a kept turn means a condition didn’t hold for that turn’s thinking. [What the API does with an invalid block](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking#mismatch-behavior) describes what is dropped and says how to alert on it. On Claude Sonnet 5.5 and Claude Haiku 5.5, `block_binding` works only with `thinking: {"type": "adaptive"}`. With any other thinking setting on those models, you can’t send `block_binding`, so make sure every condition holds or strip the `thinking` and `redacted_thinking` blocks from the kept turns.
728In production, `"drop_block"` keeps requests succeeding when a condition doesn’t hold, and the response lists each dropped block in `input_transformations` with `reason: "prefix_binding_mismatch"`. An entry whose `path` falls in a kept turn means a condition didn’t hold for that turn’s thinking. For how to alert on those entries, see [Watch production, and find reasoning lost with no error](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/fix-bound-to-a-different-conversation#monitor). On Claude Sonnet 5.5 and Claude Haiku 5.5, `block_binding` works only with `thinking: {"type": "adaptive"}`. With any other thinking setting on those models, make sure every condition holds, or remove the `thinking` and `redacted_thinking` blocks from the kept turns.
727729
728730## Availability
729731
No line in this hunk matches that.