Configure automatic context management that clears stale tool results.
Usage
claude_context_edit(
trigger_tokens = 30000L,
keep_tool_uses = 5L,
clear_at_least = 2000L,
exclude_tools = NULL
)Arguments
- trigger_tokens
Integer. Input token threshold to trigger clearing. Default 30000.
- keep_tool_uses
Integer. Number of recent tool uses to preserve. Default 5.
- clear_at_least
Integer. Minimum input tokens to clear per edit. Default 2000.
- exclude_tools
Character vector. Tool names to exclude from clearing. Default NULL (clear all).
Value
A context_management configuration for use with claude_new().
Details
Context editing is passed via api_args as context_management parameter.
As agents execute tasks and accumulate tool results, context editing
automatically removes stale content while preserving conversation flow.
In testing, context editing reduced token consumption by 84% in 100-turn sessions and enabled workflows that would otherwise fail from context exhaustion.
Requires: beta = beta_headers$BETA_CONTEXT_MGMT
API Reference: https://anthropic.com/news/context-management
Examples
if (FALSE) { # \dontrun{
# Basic context editing
chat <- claude_new(
tools = list(claude_web_search()),
context_edit = claude_context_edit(),
beta = beta_headers$BETA_CONTEXT_MGMT
)
# Can run many searches without hitting context limits
# Custom thresholds
chat <- claude_new(
tools = list(claude_web_search()),
context_edit = claude_context_edit(
trigger_tokens = 50000L,
keep_tool_uses = 10L,
clear_at_least = 5000L
),
beta = beta_headers$BETA_CONTEXT_MGMT
)
# Exclude specific tools from clearing
chat <- claude_new(
tools = list(claude_memory(tempdir()), claude_web_search()),
context_edit = claude_context_edit(
exclude_tools = c("memory")
),
beta = beta_headers$BETA_CONTEXT_MGMT
)
} # }
