Follow Discord
Sweep 03 Nov 2026 · 22:27Z Build v2.1.318 536 read Stable v2.1.311 Latest v2.1.318 Next v2.1.318 Feeds RSS JSON llms.txt llms-full.txt Unofficial
One change · api

compaction-threshold changedbuild-with-claude/compaction-threshold

Nearest release: v2.1.318, published 4 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+158added
Lines−106removed
From line 73 where the diff opens
First seen 21 Sep 2026 this site's first read of the page
Recorded edits23to this page, all time

The whole hunk

from line 73, old and new numbered
/
lines
from line 73
7373 ```bash cURL
7474 curl https://api.anthropic.com/v1/messages \
7575 -H "x-api-key: $ANTHROPIC_API_KEY" \
76 -H "anthropic-version: 2023-06-01" \
76 -H "anthropic-version: 2026-10-21" \
7777 -H "anthropic-beta: compact-2026-01-12" \
7878 -H "content-type: application/json" \
7979 -d '{
from line 301
301301 ```bash cURL
302302 curl https://api.anthropic.com/v1/messages \
303303 -H "x-api-key: $ANTHROPIC_API_KEY" \
304 -H "anthropic-version: 2023-06-01" \
304 -H "anthropic-version: 2026-10-21" \
305305 -H "anthropic-beta: compact-2026-01-12" \
306306 -H "content-type: application/json" \
307307 -d '{
from line 522
522522 ```bash cURL
523523 curl https://api.anthropic.com/v1/messages \
524524 -H "x-api-key: $ANTHROPIC_API_KEY" \
525 -H "anthropic-version: 2023-06-01" \
525 -H "anthropic-version: 2026-10-21" \
526526 -H "anthropic-beta: compact-2026-01-12" \
527527 -H "content-type: application/json" \
528528 -d '{
from line 748
748748 # for the full pause-and-continue flow. Single paused request:
749749 curl https://api.anthropic.com/v1/messages \
750750 -H "x-api-key: $ANTHROPIC_API_KEY" \
751 -H "anthropic-version: 2023-06-01" \
751 -H "anthropic-version: 2026-10-21" \
752752 -H "anthropic-beta: compact-2026-01-12" \
753753 -H "content-type: application/json" \
754754 -d '{
from line 1074
10741074 ```
10751075</CodeGroup>
10761076 
1077The API pauses even if no summary is generated. The `compaction` block’s `content` is then `null`, so keep the original messages when you continue. If safeguards block the summary, `content` is also `null` and the stop reason is `refusal` instead of `compaction`. [`stop_details`](https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons#refusal) gives the policy category, if there is one. Continue the same way in both cases: look for the `compaction` block instead of testing `stop_reason`, as the [example that preserves recent messages](https://platform.claude.com/docs/en/build-with-claude/compaction-threshold#examples) does.
1078 
10771079#### Enforcing a total token budget
10781080 
10791081When 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.
from line 1658
16561658* Keep the original messages in your list and let the API handle removing the compacted content
16571659* Manually drop the compacted messages and only include the compaction block onward
16581660 
1661If no summary is generated, the block’s `content` is `null` and the API ignores the block. Nothing is compacted, so keep the original messages.
1662 
16591663On Claude 5.1 and later models, thinking blocks from before a `compaction` block aren’t carried forward, so the summary is all the model has of that earlier work. If you write your own `instructions`, tell the model what the summary must retain; see [Tell the model what to preserve in compaction summaries](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/prompting-claude-fable-5-1#tell-the-model-what-to-preserve-in-compaction-summaries).
16601664 
16611665### Streaming
from line 1670
16661670 ```bash cURL
16671671 curl https://api.anthropic.com/v1/messages \
16681672 -H "x-api-key: $ANTHROPIC_API_KEY" \
1669 -H "anthropic-version: 2023-06-01" \
1673 -H "anthropic-version: 2026-10-21" \
16701674 -H "anthropic-beta: compact-2026-01-12" \
16711675 -H "content-type: application/json" \
16721676 -d '{
from line 2034
20302034 ```bash cURL
20312035 curl https://api.anthropic.com/v1/messages \
20322036 -H "x-api-key: $ANTHROPIC_API_KEY" \
2033 -H "anthropic-version: 2023-06-01" \
2037 -H "anthropic-version: 2026-10-21" \
20342038 -H "anthropic-beta: compact-2026-01-12" \
20352039 -H "content-type: application/json" \
20362040 -d '{
from line 2309
23052309 ```bash cURL
23062310 curl https://api.anthropic.com/v1/messages/count_tokens \
23072311 -H "x-api-key: $ANTHROPIC_API_KEY" \
2308 -H "anthropic-version: 2023-06-01" \
2312 -H "anthropic-version: 2026-10-21" \
23092313 -H "anthropic-beta: compact-2026-01-12" \
23102314 -H "content-type: application/json" \
23112315 -d '{
from line 2521
25172521 
25182522 
25192523 # Run a long conversation
2520 print(chat("Help me build a Python web scraper"))
2521 print(chat("Add support for JavaScript-rendered pages"))
2522 print(chat("Now add rate limiting and error handling"))
2524 print(chat("Briefly, help me build a Python web scraper"))
2525 print(chat("Briefly, add support for JavaScript-rendered pages"))
2526 print(chat("Briefly, add rate limiting and error handling"))
25232527 # Continue calling chat() for as long as the conversation needs
25242528 ```
25252529 
from line 2559
25552559 }
25562560 
25572561 // Run a long conversation
2558 console.log(await chat("Help me build a Python web scraper"));
2559 console.log(await chat("Add support for JavaScript-rendered pages"));
2560 console.log(await chat("Now add rate limiting and error handling"));
2562 console.log(await chat("Briefly, help me build a Python web scraper"));
2563 console.log(await chat("Briefly, add support for JavaScript-rendered pages"));
2564 console.log(await chat("Briefly, add rate limiting and error handling"));
25612565 // Continue calling chat() for as long as the conversation needs
25622566 ```
25632567 
from line 2569
25652569 AnthropicClient client = new();
25662570 List<BetaMessageParam> messages = new();
25672571 
2568 Console.WriteLine(await Chat(client, messages, "Help me build a Python web scraper"));
2569 Console.WriteLine(await Chat(client, messages, "Add support for JavaScript-rendered pages"));
2570 Console.WriteLine(await Chat(client, messages, "Now add rate limiting and error handling"));
2572 Console.WriteLine(await Chat(client, messages, "Briefly, help me build a Python web scraper"));
2573 Console.WriteLine(await Chat(client, messages, "Briefly, add support for JavaScript-rendered pages"));
2574 Console.WriteLine(await Chat(client, messages, "Briefly, add rate limiting and error handling"));
25712575 
25722576 static async Task<string> Chat(AnthropicClient client, List<BetaMessageParam> messages, string userMessage)
25732577 {
from line 2654
26502654 }
26512655 
26522656 func main() {
2653 fmt.Println(chat("Help me build a Python web scraper"))
2654 fmt.Println(chat("Add support for JavaScript-rendered pages"))
2655 fmt.Println(chat("Now add rate limiting and error handling"))
2657 fmt.Println(chat("Briefly, help me build a Python web scraper"))
2658 fmt.Println(chat("Briefly, add support for JavaScript-rendered pages"))
2659 fmt.Println(chat("Briefly, add rate limiting and error handling"))
26562660 }
26572661 ```
26582662 
from line 2669
26652669 private static final List<BetaMessageParam> messages = new ArrayList<>();
26662670 
26672671 public static void main(String[] args) {
2668 System.out.println(chat("Help me build a Python web scraper"));
2669 System.out.println(chat("Add support for JavaScript-rendered pages"));
2670 System.out.println(chat("Now add rate limiting and error handling"));
2672 System.out.println(chat("Briefly, help me build a Python web scraper"));
2673 System.out.println(chat("Briefly, add support for JavaScript-rendered pages"));
2674 System.out.println(chat("Briefly, add rate limiting and error handling"));
26712675 }
26722676 
26732677 private static String chat(String userMessage) {
from line 2737
27332737 return '';
27342738 }
27352739 
2736 echo chat($client, $messages, 'Help me build a Python web scraper') . "\n";
2737 echo chat($client, $messages, 'Add support for JavaScript-rendered pages') . "\n";
2738 echo chat($client, $messages, 'Now add rate limiting and error handling') . "\n";
2740 echo chat($client, $messages, 'Briefly, help me build a Python web scraper') . "\n";
2741 echo chat($client, $messages, 'Briefly, add support for JavaScript-rendered pages') . "\n";
2742 echo chat($client, $messages, 'Briefly, add rate limiting and error handling') . "\n";
27392743 ```
27402744 
27412745 ```ruby Ruby
from line 2769
27652769 response.content.find { |block| block.type == :text }&.text || ""
27662770 end
27672771 
2768 puts chat(client, messages, "Help me build a Python web scraper")
2769 puts chat(client, messages, "Add support for JavaScript-rendered pages")
2770 puts chat(client, messages, "Now add rate limiting and error handling")
2772 puts chat(client, messages, "Briefly, help me build a Python web scraper")
2773 puts chat(client, messages, "Briefly, add support for JavaScript-rendered pages")
2774 puts chat(client, messages, "Briefly, add rate limiting and error handling")
27712775 ```
27722776</CodeGroup>
27732777 
from line 2815
28112815 },
28122816 )
28132817 
2814 # Check if compaction occurred and paused
2815 if response.stop_reason == "compaction":
2816 # Get the compaction block from the response
2817 compaction_block = response.content[0]
2818 # Look for the compaction block, not the stop reason:
2819 # stop_reason is "refusal", not "compaction", if the summary was blocked
2820 compaction_block = next(
2821 (block for block in response.content if block.type == "compaction"), None
2822 )
28182823 
2824 # Replace the history only if a summary was generated
2825 if compaction_block is not None and compaction_block.content is not None:
28192826 # Preserve the prior exchange + current user message (3 messages)
28202827 # by including them after the compaction block, without thinking
28212828 # blocks; an assistant turn left with no content is dropped
from line 2847
28402847 {"role": "assistant", "content": new_assistant_content}
28412848 ] + preserved_messages
28422849 
2843 # Continue the request with the compacted context + preserved messages
2850 # Update the message list to reflect the compaction
2851 messages.clear()
2852 messages.extend(messages_after_compaction)
2853 
2854 if compaction_block is not None:
2855 # Continue the request, with the full history if there is no summary
28442856 response = client.beta.messages.create(
28452857 betas=["compact-2026-01-12"],
28462858 model="claude-opus-5-5",
28472859 max_tokens=4096,
2848 messages=messages_after_compaction,
2860 messages=messages,
28492861 context_management={"edits": [{"type": "compact_20260112"}]},
28502862 )
28512863 
2852 # Update the message list to reflect the compaction
2853 messages.clear()
2854 messages.extend(messages_after_compaction)
2855 
28562864 # Append the final response
28572865 messages.append({"role": "assistant", "content": response.content})
28582866 
from line 2869
28612869 
28622870 
28632871 # Run a long conversation
2864 print(chat("Help me build a Python web scraper"))
2865 print(chat("Add support for JavaScript-rendered pages"))
2866 print(chat("Now add rate limiting and error handling"))
2872 print(chat("Briefly, help me build a Python web scraper"))
2873 print(chat("Briefly, add support for JavaScript-rendered pages"))
2874 print(chat("Briefly, add rate limiting and error handling"))
28672875 # Continue calling chat() for as long as the conversation needs
28682876 ```
28692877 
from line 2899
28912899 }
28922900 });
28932901 
2894 // Check if compaction occurred and paused
2895 if (response.stop_reason === "compaction") {
2896 // Get the compaction block from the response
2897 const compactionBlock = response.content[0];
2902 // Look for the compaction block, not the stop reason:
2903 // stop_reason is "refusal", not "compaction", if the summary was blocked
2904 const compactionBlock = response.content.find((block) => block.type === "compaction");
28982905 
2906 // Replace the history only if a summary was generated
2907 if (compactionBlock && compactionBlock.content !== null) {
28992908 // Preserve the prior exchange + current user message (3 messages)
29002909 // by including them after the compaction block, without thinking
29012910 // blocks; an assistant turn left with no content is dropped
from line 2928
29192928 ...preservedMessages
29202929 ];
29212930 
2922 // Continue the request with the compacted context + preserved messages
2931 // Update the message list to reflect the compaction
2932 messages = messagesAfterCompaction;
2933 }
2934 
2935 if (compactionBlock) {
2936 // Continue the request, with the full history if there is no summary
29232937 response = await client.beta.messages.create({
29242938 betas: ["compact-2026-01-12"],
29252939 model: "claude-opus-5-5",
29262940 max_tokens: 4096,
2927 messages: messagesAfterCompaction,
2941 messages,
29282942 context_management: {
29292943 edits: [{ type: "compact_20260112" }]
29302944 }
29312945 });
2932 
2933 // Update the message list to reflect the compaction
2934 messages = messagesAfterCompaction;
29352946 }
29362947 
29372948 // Append the final response
from line 2954
29432954 }
29442955 
29452956 // Run a long conversation
2946 console.log(await chat("Help me build a Python web scraper"));
2947 console.log(await chat("Add support for JavaScript-rendered pages"));
2948 console.log(await chat("Now add rate limiting and error handling"));
2957 console.log(await chat("Briefly, help me build a Python web scraper"));
2958 console.log(await chat("Briefly, add support for JavaScript-rendered pages"));
2959 console.log(await chat("Briefly, add rate limiting and error handling"));
29492960 // Continue calling chat() for as long as the conversation needs
29502961 ```
29512962 
from line 2964
29532964 AnthropicClient client = new();
29542965 List<BetaMessageParam> messages = new();
29552966 
2956 Console.WriteLine(await Chat("Help me build a Python web scraper"));
2957 Console.WriteLine(await Chat("Add support for JavaScript-rendered pages"));
2958 Console.WriteLine(await Chat("Now add rate limiting and error handling"));
2967 Console.WriteLine(await Chat("Briefly, help me build a Python web scraper"));
2968 Console.WriteLine(await Chat("Briefly, add support for JavaScript-rendered pages"));
2969 Console.WriteLine(await Chat("Briefly, add rate limiting and error handling"));
29592970 
29602971 async Task<string> Chat(string userMessage)
29612972 {
from line 2988
29772988 }
29782989 });
29792990 
2980 if (response.StopReason == BetaStopReason.Compaction)
2981 {
2982 if (!response.Content[0].TryPickCompaction(out _))
2983 throw new InvalidOperationException("Expected compaction block");
2991 // Look for the compaction block, not the stop reason:
2992 // StopReason is Refusal, not Compaction, if the summary was blocked
2993 var compactionBlock = response.Content.FirstOrDefault(block => block.Value is BetaCompactionBlock);
29842994 
2995 // Replace the history only if a summary was generated
2996 if (compactionBlock?.Value is BetaCompactionBlock { Content: not null })
2997 {
29852998 // Preserve the prior exchange + current user message (3 messages),
29862999 // without thinking blocks; an assistant turn left empty is dropped
29873000 var preserved = new List<BetaMessageParam>();
from line 3020
30073020 {
30083021 new BetaAssistantMessageParam
30093022 {
3010 Content = new List<BetaContentBlockParam> { new BetaContentBlockParam(response.Content[0].Json) }
3023 Content = new List<BetaContentBlockParam> { new BetaContentBlockParam(compactionBlock.Json) }
30113024 }
30123025 };
30133026 messagesAfterCompaction.AddRange(preserved);
30143027 
3028 messages = messagesAfterCompaction;
3029 }
3030 
3031 if (compactionBlock is not null)
3032 {
3033 // Continue the request, with the full history if there is no summary
30153034 response = await client.Beta.Messages.Create(new()
30163035 {
30173036 Betas = ["compact-2026-01-12"],
30183037 Model = Model.ClaudeOpus5_5,
30193038 MaxTokens = 4096,
3020 Messages = messagesAfterCompaction,
3039 Messages = messages,
30213040 ContextManagement = new BetaContextManagementConfig
30223041 {
30233042 Edits = [new BetaCompact20260112Edit()]
30243043 }
30253044 });
3026 
3027 messages = messagesAfterCompaction;
30283045 }
30293046 
30303047 messages.Add(new BetaAssistantMessageParam
from line 3064
30473064 "context"
30483065 "fmt"
30493066 "log"
3067 "slices"
30503068 
30513069 "github.com/anthropics/anthropic-sdk-go/v2"
30523070 )
from line 3097
30793097 log.Fatal(err)
30803098 }
30813099 
3082 if response.StopReason == "compaction" {
3083 compactionParam := response.Content[0].ToParam()
3100 // Look for the compaction block, not the stop reason:
3101 // StopReason is "refusal", not "compaction", if the summary was blocked
3102 compactionIndex := slices.IndexFunc(response.Content, func(block anthropic.BetaContentBlockUnion) bool {
3103 return block.Type == "compaction"
3104 })
30843105 
3106 // Replace the history only if a summary was generated (content is not null)
3107 if compactionIndex >= 0 && response.Content[compactionIndex].JSON.Content.Valid() {
3108 compactionParam := response.Content[compactionIndex].ToParam()
3109 
30853110 // Preserve the prior exchange + current user message (3 messages),
30863111 // without thinking blocks; an assistant turn left empty is dropped
30873112 var preserved []anthropic.BetaMessageParam
from line 3132
31073132 }
31083133 messagesAfterCompaction = append(messagesAfterCompaction, preserved...)
31093134 
3135 messages = messagesAfterCompaction
3136 }
3137 
3138 if compactionIndex >= 0 {
3139 // Continue the request, with the full history if there is no summary
31103140 response, err = client.Beta.Messages.New(context.TODO(), anthropic.BetaMessageNewParams{
31113141 Model: anthropic.ModelClaudeOpus5_5,
31123142 MaxTokens: 4096,
3113 Messages: messagesAfterCompaction,
3143 Messages: messages,
31143144 ContextManagement: anthropic.BetaContextManagementConfigParam{
31153145 Edits: []anthropic.BetaContextManagementConfigEditUnionParam{
31163146 {OfCompact20260112: &anthropic.BetaCompact20260112EditParam{}},
from line 3151
31213151 if err != nil {
31223152 log.Fatal(err)
31233153 }
3124 
3125 messages = messagesAfterCompaction
31263154 }
31273155 
31283156 messages = append(messages, response.ToParam())
from line 3164
31363164 }
31373165 
31383166 func main() {
3139 fmt.Println(chat("Help me build a Python web scraper"))
3140 fmt.Println(chat("Add support for JavaScript-rendered pages"))
3141 fmt.Println(chat("Now add rate limiting and error handling"))
3167 fmt.Println(chat("Briefly, help me build a Python web scraper"))
3168 fmt.Println(chat("Briefly, add support for JavaScript-rendered pages"))
3169 fmt.Println(chat("Briefly, add rate limiting and error handling"))
31423170 }
31433171 ```
31443172 
from line 3174
31463174 import com.anthropic.models.beta.messages.BetaContextManagementConfig;
31473175 import com.anthropic.models.beta.messages.BetaCompact20260112Edit;
31483176 import com.anthropic.models.beta.messages.BetaInputTokensTrigger;
3149 import com.anthropic.models.beta.messages.BetaStopReason;
31503177 // ...
31513178 private static final AnthropicClient client = AnthropicOkHttpClient.fromEnv();
31523179 private static final List<BetaMessageParam> messages = new ArrayList<>();
from line 3198
31713198 
31723199 BetaMessage response = client.beta().messages().create(params);
31733200 
3174 // Check if compaction occurred and paused
3175 if (response.stopReason().isPresent()
3176 && response.stopReason().get().equals(BetaStopReason.COMPACTION)) {
3201 // Look for the compaction block, not the stop reason:
3202 // stopReason() is REFUSAL, not COMPACTION, if the summary was blocked
3203 var compactionBlock = response.content().stream()
3204 .flatMap(block -> block.compaction().stream())
3205 .findFirst();
3206 
3207 // Replace the history only if a summary was generated
3208 if (compactionBlock.flatMap(block -> block.content()).isPresent()) {
31773209 // Preserve the prior exchange + current user message (3 messages),
31783210 // without thinking blocks; an assistant turn left empty is dropped
31793211 List<BetaMessageParam> preservedMessages = new ArrayList<>();
from line 3227
31953227 messagesAfterCompaction.add(response.toParam());
31963228 messagesAfterCompaction.addAll(preservedMessages);
31973229 
3198 // Continue the request with the compacted context + preserved messages
3230 // Update the message list to reflect the compaction
3231 messages.clear();
3232 messages.addAll(messagesAfterCompaction);
3233 }
3234 
3235 if (compactionBlock.isPresent()) {
3236 // Continue the request, with the full history if there is no summary
31993237 MessageCreateParams continueParams = MessageCreateParams.builder()
32003238 .addBeta("compact-2026-01-12")
32013239 .model(Model.CLAUDE_OPUS_5_5)
32023240 .maxTokens(4096L)
3203 .messages(messagesAfterCompaction)
3241 .messages(messages)
32043242 .contextManagement(BetaContextManagementConfig.builder()
32053243 .addEdit(BetaCompact20260112Edit.builder().build())
32063244 .build())
from line 3245
32073245 .build();
32083246 
32093247 response = client.beta().messages().create(continueParams);
3210 
3211 // Update the message list to reflect the compaction
3212 messages.clear();
3213 messages.addAll(messagesAfterCompaction);
32143248 }
32153249 
32163250 // Append the final response
from line 3258
32243258 }
32253259 
32263260 public static void main(String[] args) {
3227 System.out.println(chat("Help me build a Python web scraper"));
3228 System.out.println(chat("Add support for JavaScript-rendered pages"));
3229 System.out.println(chat("Now add rate limiting and error handling"));
3261 System.out.println(chat("Briefly, help me build a Python web scraper"));
3262 System.out.println(chat("Briefly, add support for JavaScript-rendered pages"));
3263 System.out.println(chat("Briefly, add rate limiting and error handling"));
32303264 }
32313265 ```
32323266 
from line 3288
32543288 ]
32553289 );
32563290 
3257 if ($response->stopReason === 'compaction') {
3258 $compactionBlock = $response->content[0];
3291 // Look for the compaction block, not the stop reason:
3292 // stopReason is 'refusal', not 'compaction', if the summary was blocked
3293 $compactionBlock = null;
3294 foreach ($response->content as $block) {
3295 if ($block instanceof \Anthropic\Beta\Messages\BetaCompactionBlock) {
3296 $compactionBlock = $block;
3297 break;
3298 }
3299 }
32593300 
3301 // Replace the history only if a summary was generated
3302 if ($compactionBlock?->content !== null) {
32603303 // Preserve the prior exchange + current user message (3 messages),
32613304 // without thinking blocks; an assistant turn left empty is dropped
32623305 $preserved = array_values(array_filter(array_map(
from line 3317
32743317 $preserved
32753318 );
32763319 
3320 $messages = $messagesAfterCompaction;
3321 }
3322 
3323 if ($compactionBlock !== null) {
3324 // Continue the request, with the full history if there is no summary
32773325 $response = $client->beta->messages->create(
32783326 maxTokens: 4096,
3279 messages: $messagesAfterCompaction,
3327 messages: $messages,
32803328 model: 'claude-opus-5-5',
32813329 betas: ['compact-2026-01-12'],
32823330 contextManagement: [
from line 3331
32833331 'edits' => [['type' => 'compact_20260112']]
32843332 ]
32853333 );
3286 
3287 $messages = $messagesAfterCompaction;
32883334 }
32893335 
32903336 $messages[] = ['role' => 'assistant', 'content' => $response->content];
from line 3343
32973343 return '';
32983344 }
32993345 
3300 echo chat($client, $messages, 'Help me build a Python web scraper') . "\n";
3301 echo chat($client, $messages, 'Add support for JavaScript-rendered pages') . "\n";
3302 echo chat($client, $messages, 'Now add rate limiting and error handling') . "\n";
3346 echo chat($client, $messages, 'Briefly, help me build a Python web scraper') . "\n";
3347 echo chat($client, $messages, 'Briefly, add support for JavaScript-rendered pages') . "\n";
3348 echo chat($client, $messages, 'Briefly, add rate limiting and error handling') . "\n";
33033349 ```
33043350 
33053351 ```ruby Ruby
from line 3371
33253371 }
33263372 )
33273373 
3328 if response.stop_reason == :compaction
3329 compaction_block = response.content[0]
3374 # Look for the compaction block, not the stop reason:
3375 # stop_reason is :refusal, not :compaction, if the summary was blocked
3376 compaction_block = response.content.find { |block| block.type == :compaction }
33303377 
3378 # Replace the history only if a summary was generated
3379 if compaction_block&.content
33313380 # Preserve the prior exchange + current user message (3 messages),
33323381 # without thinking blocks; an assistant turn left empty is dropped
33333382 preserved = messages.last(3).map do |message|
from line 3389
33403389 { role: "assistant", content: [compaction_block] }
33413390 ] + preserved
33423391 
3392 messages.clear
3393 messages.concat(messages_after_compaction)
3394 end
3395 
3396 if compaction_block
3397 # Continue the request, with the full history if there is no summary
33433398 response = client.beta.messages.create(
33443399 betas: ["compact-2026-01-12"],
33453400 model: Anthropic::Model::CLAUDE_OPUS_5_5,
33463401 max_tokens: 4096,
3347 messages: messages_after_compaction,
3402 messages: messages,
33483403 context_management: {
33493404 edits: [{ type: "compact_20260112" }]
33503405 }
33513406 )
3352 
3353 messages.clear
3354 messages.concat(messages_after_compaction)
33553407 end
33563408 
33573409 messages << { role: "assistant", content: response.content }
from line 3411
33593411 response.content.find { |block| block.type == :text }&.text || ""
33603412 end
33613413 
3362 puts chat(client, messages, "Help me build a Python web scraper")
3363 puts chat(client, messages, "Add support for JavaScript-rendered pages")
3364 puts chat(client, messages, "Now add rate limiting and error handling")
3414 puts chat(client, messages, "Briefly, help me build a Python web scraper")
3415 puts chat(client, messages, "Briefly, add support for JavaScript-rendered pages")
3416 puts chat(client, messages, "Briefly, add rate limiting and error handling")
33653417 ```
33663418</CodeGroup>
33673419 
Feedback