You turned on context editing so your agent stops dying at the context limit. Good. But if you left clear_at_least unset — or set it low — you may have quietly raised your token bill. Here's why, and the one number that fixes it.

The trap, in one paragraph#

clear_tool_uses_20250919 deletes the oldest tool results once your prompt crosses the trigger. Anthropic's docs are blunt about the side effect: clearing tool results invalidates cached prompt prefixes below the clear point (context editing docs). So the surviving suffix has to be re-cached — a cache write at 1.25× input (5-min TTL) or 2× (1-hr), versus the 0.10× cache read you'd have paid if you'd left the cache intact (prompt caching). Clear a little on every turn and you re-warm the cache on every turn. The feature you added to save money starts spending it.

The break-even math#

Think of every clear as a trade: a one-time cost against a recurring saving.

So the clear pays off once C (removed) is large relative to W (re-warmed), amortized over the turns before your next clear. Concretely:

Clearing 2,000 tokens to invalidate a 90,000-token cached prefix is a loss — you re-warm 90k to reclaim 2k. Clearing 40,000 tokens amortizes the re-warm in a turn or two. Same feature, opposite outcome, decided entirely by size.

That is exactly what clear_at_least controls: the strategy won't clear unless it can remove at least that many tokens. It's the guardrail that keeps you on the profitable side of the trade.

The config#

Cache the stable head, edit the tail, and set the threshold high:

response = client.messages.create(
    model="claude-opus-4-8",
    betas=["context-management-2025-06-27"],
    system=[{
        "type": "text",
        "text": SYSTEM_PROMPT,
        "cache_control": {"type": "ephemeral"},   # cache the stable head
    }],
    tools=TOOLS,                                    # tool defs live in the cached head too
    context_management={
        "edits": [{
            "type": "clear_tool_uses_20250919",
            "trigger": {"type": "input_tokens", "value": 100_000},
            "keep": {"type": "tool_uses", "value": 3},
            "clear_at_least": {"type": "input_tokens", "value": 30_000},  # <-- the knob
            "exclude_tools": ["retrieve_docs"],     # results you re-read: never clear
        }],
    },
    messages=messages,
)

Three things earn their place here:

  1. clear_at_least: 30_000. Big enough that each clear dwarfs the re-warmed suffix. Start around a third of your window and adjust to your tool-result sizes — raise it if results are small, lower it if a single tool call returns tens of thousands of tokens.
  2. exclude_tools. Any tool whose output your agent re-reads later (retrieval, a plan, a scratchpad) goes here so it's never cleared out from under you.
  3. The cached head. System prompt and tool definitions sit in the cached prefix. clear_tool_uses clears results, not defs — so the most valuable cached bytes survive every clear. That's the whole reason caching and context editing can coexist instead of cancelling out.

The rule#

Clear big and infrequently, not small and constantly. Set clear_at_least to tens of thousands of tokens, cache the head so editing never touches it, and exclude the tools you re-read. Do that and context editing does what you turned it on to do — keep the agent alive through long runs — without handing back the savings from prompt caching one re-warm at a time. Leave clear_at_least at zero and you've built a machine that pays full price to delete cheap tokens.