Context editing enables automatic management of conversation context by intelligently clearing old tool uses and thinking blocks. This can greatly reduce costs in long-running sessions with minimal tradeoffs in context performance.
Context editing is currently supported for Anthropic models only. The configuration is ignored when routing to other providers.
Why Context Editing#
Automatically clear old tool results before hitting context limits
Keep only relevant context, reducing input tokens on subsequent calls
Run AI agents for longer periods without manual context management
Quick Start#
Enable context editing with a simple configuration. The AI Gateway handles the translation to Anthropic's native format.
Configuration Options#
The context_editing object supports two strategies for managing context:
Clear Tool Uses#
Automatically clear old tool use results when context grows too large:
| Parameter | Type | Description |
|---|---|---|
trigger | number | Token threshold to trigger clearing |
keep | number | Number of recent tool uses to preserve |
clear_at_least | number | Minimum tokens to clear when triggered |
exclude_tools | string[] | Tool names that should never be cleared |
clear_tool_inputs | boolean | Clear tool inputs while keeping outputs |
Clear Thinking#
Manage thinking/reasoning blocks in multi-turn conversations:
| Parameter | Type | Description |
|---|---|---|
keep | number | "all" | Number of thinking turns to keep, or "all" |
Complete Example#
Here's a full configuration for a long-running coding agent:
Responses API Support#
Context editing works with both the Chat Completions API and the Responses API:
Default Behavior#
When context_editing.enabled is true but no specific strategies are provided, the AI Gateway uses sensible defaults:
Related Features#
- Reasoning - Extended thinking that benefits from context editing
- Prompt Caching - Cache static context for cost savings
- Sessions - Track and analyze long-running agent sessions
Anthropic Context Editing Documentation
