Context editing
Automatically manage conversation context as it grows with context editing.Overview
Context editing allows you to automatically manage conversation context as it grows, helping you optimize costs and stay within context window limits. You can use server-side API strategies, client-side SDK features, or both together.Server-side strategies
Context editing is currently in beta with support for tool result clearing and thinking block clearing. To enable it, use the beta header
context-management-2025-06-27 in your API requests.Please reach out through our feedback form to share your feedback on this feature.Tool result clearing
Theclear_tool_uses_20250919 strategy clears tool results when conversation context grows beyond your configured threshold. When activated, the API automatically clears the oldest tool results in chronological order, replacing them with placeholder text to let Claude know the tool result was removed. By default, only tool results are cleared. You can optionally clear both tool results and tool calls (the tool use parameters) by setting clear_tool_inputs to true.
Thinking block clearing
Theclear_thinking_20251015 strategy manages thinking blocks in conversations when extended thinking is enabled. This strategy automatically clears older thinking blocks from previous turns.
An assistant conversation turn may include multiple content blocks (e.g. when using tools) and multiple thinking blocks (e.g. with interleaved thinking).
Supported models
Context editing is available on:- Claude Opus 4.5 (
claude-opus-4-5-20251101) - Claude Opus 4.1 (
claude-opus-4-1-20250805) - Claude Opus 4 (
claude-opus-4-20250514) - Claude Sonnet 4.5 (
claude-sonnet-4-5-20250929) - Claude Sonnet 4 (
claude-sonnet-4-20250514) - Claude Haiku 4.5 (
claude-haiku-4-5-20251001)
Tool result clearing usage
The simplest way to enable tool result clearing is to specify only the strategy type, as all other configuration options will use their default values:Advanced configuration
You can customize the tool result clearing behavior with additional parameters:Thinking block clearing usage
Enable thinking block clearing to manage context and prompt caching effectively when extended thinking is enabled:Configuration options for thinking block clearing
Theclear_thinking_20251015 strategy supports the following configuration:
Example configurations:
Combining strategies
You can use both thinking block clearing and tool result clearing together:When using multiple strategies, the
clear_thinking_20251015 strategy must be listed first in the edits array.Configuration options for tool result clearing
Context editing response
You can see which context edits were applied to your request using thecontext_management response field, along with helpful statistics about the content and input tokens cleared.
Response
message_delta event:
Streaming Response
Token counting
The token counting endpoint supports context management, allowing you to preview how many tokens your prompt will use after context editing is applied.Response
input_tokens) and the original token count before any clearing occurred (original_input_tokens).
Using with the Memory Tool
Context editing can be combined with the memory tool. When your conversation context approaches the configured clearing threshold, Claude receives an automatic warning to preserve important information. This enables Claude to save tool results or context to its memory files before they’re cleared from the conversation history. This combination allows you to:- Preserve important context: Claude can write essential information from tool results to memory files before those results are cleared
- Maintain long-running workflows: Enable agentic workflows that would otherwise exceed context limits by offloading information to persistent storage
- Access information on demand: Claude can look up previously cleared information from memory files when needed, rather than keeping everything in the active context window
Client-side compaction (SDK)
Compaction is available in the Python and TypeScript SDKs when using the
tool_runner method.How compaction works
When compaction is enabled, the SDK monitors token usage after each model response:- Threshold check: The SDK calculates total tokens as
input_tokens + cache_creation_input_tokens + cache_read_input_tokens + output_tokens - Summary generation: When the threshold is exceeded, a summary prompt is injected as a user turn, and Claude generates a structured summary wrapped in
<summary></summary>tags - Context replacement: The SDK extracts the summary and replaces the entire message history with it
- Continuation: The conversation resumes from the summary, with Claude picking up where it left off
Using compaction
Addcompaction_control to your tool_runner call:
What happens during compaction
As the conversation grows, the message history accumulates: Before compaction (approaching 100k tokens):Configuration options
Choosing a token threshold
The threshold determines when compaction occurs. A lower threshold means more frequent compactions with smaller context windows. A higher threshold allows more context but risks hitting limits.Using a different model for summaries
You can use a faster or cheaper model for generating summaries:Custom summary prompts
You can provide a custom prompt for domain-specific needs. Your prompt should instruct Claude to wrap its summary in<summary></summary> tags.
Default summary prompt
The built-in summary prompt instructs Claude to create a structured continuation summary including:- Task Overview: The user’s core request, success criteria, and constraints
- Current State: What has been completed, files modified, and artifacts produced
- Important Discoveries: Technical constraints, decisions made, errors resolved, and failed approaches
- Next Steps: Specific actions needed, blockers, and priority order
- Context to Preserve: User preferences, domain-specific details, and commitments made
Limitations
Server-side tools
When using server-side tools, the SDK may incorrectly calculate token usage, causing compaction to trigger at the wrong time. For example, after a web search operation, the API response might show:cache_read_input_tokens value includes accumulated reads from multiple internal API calls made by the server-side tool, not your actual conversation context. Your real context length might only be the 63,000 input_tokens, but the SDK sees 333k and triggers compaction prematurely.
Workarounds:
- Use the token counting endpoint to get accurate context length
- Avoid compaction when using server-side tools extensively
Tool use edge cases
When compaction is triggered while a tool use response is pending, the SDK removes the tool use block from the message history before generating the summary. Claude will re-issue the tool call after resuming from the summary if still needed.Monitoring compaction
Enable logging to track when compaction occurs:When to use compaction
Good use cases:- Long-running agent tasks that process many files or data sources
- Research workflows that accumulate large amounts of information
- Multi-step tasks with clear, measurable progress
- Tasks that produce artifacts (files, reports) that persist outside the conversation
- Tasks requiring precise recall of early conversation details
- Workflows using server-side tools extensively
- Tasks that need to maintain exact state across many variables