Skip to main content
Helicone OSS LLM Observability

Context Editing

Automatically manage conversation context by clearing old tool uses and thinking blocks for long-running AI agent sessions
2 min read

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.

Info

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#

The context_editing object supports two strategies for managing context:

Clear Tool Uses#

Automatically clear old tool use results when context grows too large:

ParameterTypeDescription
triggernumberToken threshold to trigger clearing
keepnumberNumber of recent tool uses to preserve
clear_at_leastnumberMinimum tokens to clear when triggered
exclude_toolsstring[]Tool names that should never be cleared
clear_tool_inputsbooleanClear tool inputs while keeping outputs

Clear Thinking#

Manage thinking/reasoning blocks in multi-turn conversations:

ParameterTypeDescription
keepnumber | "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:


  • 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