Context editing is currently supported for Anthropic models only. The configuration is ignored when routing to other providers.
Why Context Editing
Prevent Context Overflow
Automatically clear old tool results before hitting context limits
Reduce Token Costs
Keep only relevant context, reducing input tokens on subsequent calls
Enable Long Sessions
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
Thecontext_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
Whencontext_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
Learn More
Anthropic Context Editing Documentation