compaction-on-demand changedbuild-with-claude/compaction-on-demand
Nearest release: v2.1.281, published an hour before 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+19added
Lines−23removed
From line
62
where the diff opens
First seen
21 Sep 2026
this site's first read of the page
Recorded edits5to this page, all time
The whole hunk
from line 62, old and new numbered
/
from line 62
6262 }'
6363 ```
6464
65 <MultiFileExample language="cli" label="CLI">
66 ```bash CLI
67 ant beta:messages create --beta compact-2026-09-04 < request.yaml
68 ```
65 ```bash CLI
66 ant beta:messages create --beta compact-2026-09-04 <<'YAML'
67 model: claude-opus-5-5
68 # max_tokens caps the whole call, including any thinking, so allow several thousand tokens.
69 max_tokens: 4096
70 messages:
71 - role: user
72 content: I am building a recipe app. Help me name the main entities in the data model.
73 - role: assistant
74 content: Start with Recipe, Ingredient, and Step. Add a RecipeIngredient entry that holds the quantity and unit for each ingredient in a recipe.
75 - role: user
76 content: Good. Now suggest field names for Recipe.
77 compaction:
78 type: summarize
79 YAML
80 ```
6981
70 <File filename="request.yaml">
71 ```yaml
72 model: claude-opus-5-5
73 # max_tokens caps the whole call, including any thinking, so allow several thousand tokens.
74 max_tokens: 4096
75 messages:
76 - role: user
77 content: I am building a recipe app. Help me name the main entities in the data model.
78 - role: assistant
79 content: Start with Recipe, Ingredient, and Step. Add a RecipeIngredient entry that holds the quantity and unit for each ingredient in a recipe.
80 - role: user
81 content: Good. Now suggest field names for Recipe.
82 compaction:
83 type: summarize
84 ```
85 </File>
86 </MultiFileExample>
87
8882 ```python Python
8983 from anthropic.types.beta import BetaMessageParam
9084
from line 762
768762
769763The check on `stop_reason` comes before the code looks for the block; [Handle a missing summary or an error](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand#when-no-summary-comes-back) says why. The history is replaced, not appended to: the returned message replaces every message the request carried, under the rules in [Continue from the summary](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand#continue-from-the-summary). When no summary comes back, the loop keeps its history and asks again after the next turn.
770764
771The SDK [tool runner](https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-runner) in Python, TypeScript, C#, Go, and Java can send the compaction request for you. When you decide to compact, call `compact_before_next_turn()` on the runner (`compactBeforeNextTurn()` in TypeScript and Java, `CompactBeforeNextTurn()` in C# and Go). Once the current turn and its tool calls finish, the runner sends the compaction request and replaces its history with the returned message. Create the runner with the `compact-2026-09-04` beta, because the runner doesn't add it. The runner builds the request from its own parameters and leaves `context_management` out. If those parameters include `stop_sequences`, a `tool_choice` of type `any` or `tool`, or a structured-output `output_config.format`, the API rejects the request with a 400 error. [Request a summary](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand#request-a-summary) explains why. The runner refuses to compact while its `context_management` has a compaction edit, so use one kind of compaction on a runner.
765The SDK [tool runner](https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-runner) in Python, TypeScript, C#, Go, Java, PHP, and Ruby can send the compaction request for you. When you decide to compact, call `compact_before_next_turn()` on the runner (`compactBeforeNextTurn()` in TypeScript, Java, and PHP, and `CompactBeforeNextTurn()` in C# and Go). Once the current turn and its tool calls finish, the runner sends the compaction request and replaces its history with the returned message. Create the runner with the `compact-2026-09-04` beta, because the runner doesn't add it.
766
767The runner builds the compaction request from its own parameters and leaves `context_management` out. It also leaves out `stop_sequences`, a `tool_choice` of type `any` or `tool`, and a structured-output `output_config.format`, which the API rejects on a compaction request. [Request a summary](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand#request-a-summary) explains why. The runner sends them again on its later requests. SDK versions before Python 1.8.0, TypeScript 0.128.0, C# 12.50.0, Go 1.75.0, and Java 2.65.0 send them on the compaction request too. On those versions, a runner that sets any of these parameters gets a 400 error. The runner sends a [task budget](https://platform.claude.com/docs/en/build-with-claude/task-budgets) unchanged. If `output_config.task_budget` sets `remaining`, the compaction request returns a 400 error, so leave `remaining` unset, as [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) says. The runner refuses to compact while its `context_management` has a compaction edit, so use one kind of compaction on a runner.
772768
773769### When to compact
774770
No line in this hunk matches that.