Learning resourcesReviewed · Anthropic

Compaction that keeps recent turns

This Claude guide teaches application developers to shorten older conversation history while preserving recent exchanges exactly, but the application must choose a valid split itself. The technique uses on-demand compaction with the compact-2026-09-04 beta header, then places the retained messages after the returned summary block.

Researched 25 Nov 2026

In short.

This Claude guide teaches application developers to shorten older conversation history while preserving recent exchanges exactly, but the feature is in beta and requires careful message boundaries. It explains how to summarize only the older messages and continue with the summary followed by unchanged recent turns. Examples in seven languages retain two turns and replace history only when the summary response reports stop_reason as compaction. Tool-call boundaries and thinking-block validity require particular care. Prices, measured savings, latency and runtime correctness are not established by the page.

Researched and written by an AI model and checked by a second AI model. How it works

Coverage is limited to the complete page at [https://platform.claude.com/docs/en/build-with-claude/compaction-keep-recent-turns.md], including all seven code examples, the beta notice, the availability table and the required header. Linked pages about base compaction, continuation, preserved thinking, models, platforms and beta headers are excluded. No license notice is supplied. No code was executed; runtime behavior, performance, summary fidelity and actual charges remain unassessed.

The overview

Identity

Compaction that keeps recent turns is a Claude developer documentation guide about retaining an exact recent portion of a conversation while summarizing its older portion. The source address is [https://platform.claude.com/docs/en/build-with-claude/compaction-keep-recent-turns.md]. It teaches an application-side history-management pattern using the Messages API.

Assessment

The guide is useful when an application needs to free conversation space while preserving the wording of recent exchanges. Its strongest practical detail is the distinction between messages sent for summarization and messages retained unchanged. It also identifies invalid tool-call boundaries. It is best read alongside the underlying on-demand compaction documentation: continuation details and some thinking-validity conditions are delegated to linked pages whose contents are not included here.

Users

The intended users are developers maintaining conversation history in Claude applications. Beginners receive a concrete recipe-app conversation example; experienced developers get request fields, response checks and history-replacement logic. The technique particularly fits applications that need recent wording preserved while allowing older exchanges to become a summary. It requires control over the messages submitted to the API.

Capabilities

The pattern lets an application choose how much recent history to retain, request a summary of everything before that boundary, and continue with both portions. The retained messages include their thinking blocks. There is no API parameter that selects the retained turns; the application performs the split. Python, TypeScript, C#, Go, Java, PHP and Ruby examples demonstrate the loop.

Using it

Requirements

The requests require the compact-2026-09-04 beta header and a supported model. The examples use claude-opus-5-5. The page lists beta availability through the Claude API, Amazon Bedrock, Claude Platform on AWS, Claude Models on Google Cloud with Google management, and Microsoft Foundry.

Model availability differs by platform. The table lists Claude Fable 5.2, Claude Mythos 5.2, Claude Mythos Preview, Claude Opus 4.6 and Claude Sonnet 4.6 for the other listed platforms but omits them from Amazon Bedrock. An application must also retain message content faithfully and track tool-call boundaries. Required SDK versions and detailed authentication setup are not stated.

Mechanism

The application divides history into older messages and a recent portion. It sends only the older messages in a request with compaction set to type summarize, because the API summarizes every message it receives. When the response has stop_reason equal to compaction, the application replaces the older portion with the returned assistant message and appends the recent portion unchanged.

Each tool call and its result must remain on the same side of the boundary. A compaction request ending with an assistant tool call that has no result is rejected. Retained thinking must also satisfy separate validity conditions; the page identifies a cut between an assistant reply and the next user message as meeting the boundary-related condition.

Setup

The examples create an Anthropic client, maintain message history, choose a compaction threshold and set the number of turns to retain. Both the ordinary conversation request and the summarization request include the beta header. After each reply, the examples add input_tokens and output_tokens to estimate the conversation size relevant to the next request.

The examples compact only after the threshold is exceeded, when there are more turns than the retained count, and before the final demonstration question. They update history only after the compaction stop reason is returned. Installation commands, minimum SDK versions and a complete account setup procedure are absent.

Example

The demonstration asks eight questions about a recipe application's data model. It retains two turns, defining a turn as one user message plus one assistant reply, so it splits four messages from the end. Earlier messages go into the summary request; the four recent messages follow the returned summary.

The sample threshold is 2,500 tokens, deliberately low so the short demonstration reaches it. Ordinary responses allow up to 8,192 output tokens and summary requests up to 4,096. These are demonstration settings, not established production recommendations. The fixed message-count split assumes the example's alternating conversation structure; tool exchanges require attention to the documented boundary rule.

Assessment & evidence

Limitations

Exact preservation applies to the retained messages. Older exchanges are represented by a summary, and the page supplies no evaluation of how faithfully that summary preserves details. Retaining a larger recent portion frees less space. The application must choose the split, preserve message content and avoid separating tool calls from their results.

The feature is beta and limited to listed model and platform combinations. Thinking-block validity depends on conditions explained elsewhere; a valid cut alone does not establish that all conditions hold. The page supplies code examples without execution results, so runtime correctness, summary quality and production behavior are not assessed.

Design tradeoff

The practical tradeoff is between preserving recent detail and freeing conversation space. A larger retained portion preserves more original wording but leaves more full-length content in later requests. A smaller retained portion gives summarization more of the history. The guide makes this choice explicit and leaves it to the application; it provides no method for choosing an optimal retained length for a particular workload.

Alternatives

The page contrasts this pattern with its linked baseline compaction loop, which summarizes the whole history and replaces it with the returned message. Retaining recent turns is useful when their exact content matters; whole-history compaction does not preserve that recent portion separately. The linked baseline guide is outside the available evidence. Other providers, memory systems and retrieval approaches are not compared.

Evidence

The evidence consists of the complete documentation page at [https://platform.claude.com/docs/en/build-with-claude/compaction-keep-recent-turns.md]. It contains explanatory text, seven language examples, a model and platform availability table, and the required beta-header notice. It establishes documented behavior and implementation guidance, but supplies no execution receipts, benchmarks or independent accuracy measurements.

Access & freshness

Costs

Prices and the billing treatment of compaction requests are unknown from this page. The pattern makes a separate summary request and continues sending the retained messages at full length. Keeping more turns therefore reduces the space freed by compaction. The page provides no measured token savings, monetary savings or latency results, so a cost benefit cannot be quantified.

Rights

The page does not provide a license or redistribution permission for its documentation or code examples. Reuse and redistribution rights are unknown from this evidence. It also does not establish service terms or data-handling commitments for conversation content.

Freshness

The source snapshot was captured at 2026-11-25T06:31:12.722Z. It labels the feature beta and specifies compact-2026-09-04. That identifier names the required beta header; the page does not establish a publication or last-update date. Availability beyond the captured snapshot is unknown.

Adoption steps

Before adopting the pattern, confirm support for the intended model and platform, obtain the missing SDK and billing details, and read the linked continuation and thinking-validity documentation. Choose a retained-history boundary that keeps tool calls with their results and preserve retained blocks unchanged.

Evaluate the intended application's own conversations for summary accuracy, request validity, token use and latency before choosing a threshold or retained length. The example's two-turn retention and 2,500-token threshold do not establish suitable settings for another workload.

Evidence for this report

selected-page.txtselected · checked 2026-11-25