compaction-threshold changedbuild-with-claude/compaction-threshold
Nearest release: v2.1.309, published 3 hours 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+31added
Lines−28removed
From line
27
where the diff opens
First seen
21 Sep 2026
this site's first read of the page
Recorded edits17to this page, all time
The whole hunk
from line 27, old and new numbered
/
from line 27
2727 Claude API: beta
2828 Claude Platform on AWS: beta
2929 Amazon Bedrock: beta
30 Google Cloud: beta
30 Claude Models on Google Cloud (Google managed): beta
3131 Microsoft Foundry: beta
3232---
3333
from line 239
239239 $response = $client->beta->messages->create(
240240 maxTokens: 4096,
241241 messages: $messages,
242 model: 'claude-opus-5-5',
242 model: Model::CLAUDE_OPUS_5_5,
243243 betas: ['compact-2026-01-12'],
244244 contextManagement: [
245245 'edits' => [
from line 459
459459 $message = $client->beta->messages->create(
460460 maxTokens: 4096,
461461 messages: $messages,
462 model: 'claude-opus-5-5',
462 model: Model::CLAUDE_OPUS_5_5,
463463 betas: ['compact-2026-01-12'],
464464 contextManagement: [
465465 'edits' => [
from line 684
684684 ['role' => 'assistant', 'content' => "I'll help you build a web scraper..."],
685685 ['role' => 'user', 'content' => 'Add support for JavaScript-rendered pages']
686686 ],
687 model: 'claude-opus-5-5',
687 model: Model::CLAUDE_OPUS_5_5,
688688 betas: ['compact-2026-01-12'],
689689 contextManagement: [
690690 'edits' => [
from line 1070
10701070
10711071#### Enforcing a total token budget
10721072
1073When 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 end the task once a budget is reached.
1073When 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 conclude the task once a budget is reached.
10741074
10751075This example appears in the SDK languages only: its value is the budget-tracking logic around the request. The raw request combines the `trigger` from [Trigger configuration](https://platform.claude.com/docs/en/build-with-claude/compaction-threshold#trigger-configuration) with `pause_after_compaction` from [Pausing after compaction](https://platform.claude.com/docs/en/build-with-claude/compaction-threshold#pausing-after-compaction).
10761076
from line 1180
11801180 compactionCount += 1;
11811181 messages.Add(new BetaAssistantMessageParam
11821182 {
1183 Content = response.Content.Select(b => new BetaContentBlockParam(b.Json)).ToList()
1183 Content = response.Content.Select(block => new BetaContentBlockParam(block.Json)).ToList()
11841184 });
11851185
11861186 // Estimate total tokens consumed; prompt wrap-up if over budget
from line 1884
18841884 try (var streamResponse = client.beta().messages().createStreaming(params)) {
18851885 streamResponse.stream().forEach(event -> {
18861886 event.contentBlockStart().ifPresent(startEvent -> {
1887 startEvent.contentBlock().compaction().ifPresent(c ->
1887 startEvent.contentBlock().compaction().ifPresent(compactionBlock ->
18881888 System.out.println("Compaction started...")
18891889 );
1890 startEvent.contentBlock().text().ifPresent(t ->
1890 startEvent.contentBlock().text().ifPresent(textBlock ->
18911891 System.out.println("Text response started...")
18921892 );
18931893 });
18941894
18951895 event.contentBlockDelta().ifPresent(deltaEvent -> {
1896 deltaEvent.delta().compaction().ifPresent(cd ->
1897 System.out.println("Compaction complete: " + cd.content().map(String::length).orElse(0) + " chars")
1896 deltaEvent.delta().compaction().ifPresent(compactionDelta ->
1897 System.out.println("Compaction complete: " + compactionDelta.content().map(String::length).orElse(0) + " chars")
18981898 );
1899 deltaEvent.delta().text().ifPresent(td ->
1900 System.out.print(td.text())
1899 deltaEvent.delta().text().ifPresent(textDelta ->
1900 System.out.print(textDelta.text())
19011901 );
19021902 });
19031903 });
from line 1911
19111911 use Anthropic\Beta\Messages\BetaRawContentBlockStartEvent;
19121912 use Anthropic\Beta\Messages\BetaTextBlock;
19131913 use Anthropic\Beta\Messages\BetaTextDelta;
1914 use Anthropic\Messages\Model;
19141915
19151916 $client = new Client();
19161917 $messages = [['role' => 'user', 'content' => 'Hello, Claude']];
from line 1919
19181919 $stream = $client->beta->messages->createStream(
19191920 maxTokens: 4096,
19201921 messages: $messages,
1921 model: 'claude-opus-5-5',
1922 model: Model::CLAUDE_OPUS_5_5,
19221923 betas: ['compact-2026-01-12'],
19231924 contextManagement: [
19241925 'edits' => [
from line 1943
19421943 case $event instanceof BetaRawContentBlockDeltaEvent:
19431944 switch (true) {
19441945 case $event->delta instanceof BetaCompactionContentBlockDelta:
1945 echo "Compaction complete: " . strlen($event->delta->content ?? '') . " chars\n";
1946 echo 'Compaction complete: ' . strlen($event->delta->content ?? '') . " chars\n";
19461947 break;
19471948 case $event->delta instanceof BetaTextDelta:
19481949 echo $event->delta->text;
from line 2204
22032204 $response = $client->beta->messages->create(
22042205 maxTokens: 4096,
22052206 messages: [['role' => 'user', 'content' => 'Hello, Claude']],
2206 model: 'claude-opus-5-5',
2207 model: Model::CLAUDE_OPUS_5_5,
22072208 betas: ['compact-2026-01-12'],
22082209 system: [
22092210 [
from line 2444
24432444 ]
24442445 );
24452446
2446 echo "Current tokens: " . $countResponse->inputTokens . "\n";
2447 echo "Original tokens: " . $countResponse->contextManagement->originalInputTokens . "\n";
2447 echo 'Current tokens: ' . $countResponse->inputTokens . "\n";
2448 echo 'Original tokens: ' . $countResponse->contextManagement->originalInputTokens . "\n";
24482449 ```
24492450
24502451 ```ruby Ruby
from line 2592
25912592 return response.Content
25922593 .Select(block => block.Value)
25932594 .OfType<BetaTextBlock>()
2594 .Select(tb => tb.Text)
2595 .Select(textBlock => textBlock.Text)
25952596 .FirstOrDefault() ?? "";
25962597 }
25972598 ```
from line 2698
26972698 $client = new Client();
26982699 $messages = [];
26992700
2700 function chat($client, &$messages, $userMessage) {
2701 function chat($client, &$messages, $userMessage)
2702 {
27012703 $messages[] = ['role' => 'user', 'content' => $userMessage];
27022704
27032705 $response = $client->beta->messages->create(
from line 2727
27252727 return '';
27262728 }
27272729
2728 echo chat($client, $messages, "Help me build a Python web scraper") . "\n";
2729 echo chat($client, $messages, "Add support for JavaScript-rendered pages") . "\n";
2730 echo chat($client, $messages, "Now add rate limiting and error handling") . "\n";
2730 echo chat($client, $messages, 'Help me build a Python web scraper') . "\n";
2731 echo chat($client, $messages, 'Add support for JavaScript-rendered pages') . "\n";
2732 echo chat($client, $messages, 'Now add rate limiting and error handling') . "\n";
27312733 ```
27322734
27332735 ```ruby Ruby
from line 3029
30273029 return response.Content
30283030 .Select(block => block.Value)
30293031 .OfType<BetaTextBlock>()
3030 .Select(tb => tb.Text)
3032 .Select(textBlock => textBlock.Text)
30313033 .FirstOrDefault() ?? "";
30323034 }
30333035 ```
from line 3228
32263228 $client = new Client();
32273229 $messages = [];
32283230
3229 function chat($client, &$messages, $userMessage) {
3231 function chat($client, &$messages, $userMessage)
3232 {
32303233 $messages[] = ['role' => 'user', 'content' => $userMessage];
32313234
32323235 $response = $client->beta->messages->create(
from line 3291
32883291 return '';
32893292 }
32903293
3291 echo chat($client, $messages, "Help me build a Python web scraper") . "\n";
3292 echo chat($client, $messages, "Add support for JavaScript-rendered pages") . "\n";
3293 echo chat($client, $messages, "Now add rate limiting and error handling") . "\n";
3294 echo chat($client, $messages, 'Help me build a Python web scraper') . "\n";
3295 echo chat($client, $messages, 'Add support for JavaScript-rendered pages') . "\n";
3296 echo chat($client, $messages, 'Now add rate limiting and error handling') . "\n";
32943297 ```
32953298
32963299 ```ruby Ruby
from line 3380
33773380 Learn about context window sizes and management strategies.
33783381 </Card>
33793382
3380 <Card title="Session memory compaction recipe" icon="book" href="https://platform.claude.com/cookbook/misc-session-memory-compaction">
3383 <Card title="Session memory compaction recipe (Claude Cookbook)" icon="book" href="https://platform.claude.com/cookbook/misc-session-memory-compaction">
33813384 Explore a practical implementation that manages long-running conversations with instant session memory compaction using background threading and prompt caching.
33823385 </Card>
33833386</CardGroup>
No line in this hunk matches that.