◉CLAUDE LABJP
●2.1.283 — No Claude Code release over the weekend; 2.1.283 from September 25 is still the latest. Its new auto-mode default deserves a check against your allow rules●PLANS — Pro and Team Standard now default to Opus instead of Sonnet, a change shipped in Claude Code 2.1.280 that shifts how you compare plans●10/07 — Nine days left until the old spellings of the Claude Desktop / Cowork managed config keys stop being accepted; after that they fail closed●AUTO — Users are asking why commands covered by allow rules get rejected in auto mode. The fix is shaping the invoked command to match the rule●NEW — The week Opus 5.5 became the default: three places an alias was still pointing at the old model●CURSOR — Cursor credits burn in proportion to model cost, so routing Claude through Cursor gets expensive fast. Above ~50 requests a day, flat-rate Claude Code Pro is the steadier choice●2.1.283 — No Claude Code release over the weekend; 2.1.283 from September 25 is still the latest. Its new auto-mode default deserves a check against your allow rules●PLANS — Pro and Team Standard now default to Opus instead of Sonnet, a change shipped in Claude Code 2.1.280 that shifts how you compare plans●10/07 — Nine days left until the old spellings of the Claude Desktop / Cowork managed config keys stop being accepted; after that they fail closed●AUTO — Users are asking why commands covered by allow rules get rejected in auto mode. The fix is shaping the invoked command to match the rule●NEW — The week Opus 5.5 became the default: three places an alias was still pointing at the old model●CURSOR — Cursor credits burn in proportion to model cost, so routing Claude through Cursor gets expensive fast. Above ~50 requests a day, flat-rate Claude Code Pro is the steadier choice
Articles/API & SDK
⬡ API & SDK/2026-06-16Advanced

Taming Token Bloat in Long-Running Agents with Context Editing and the Memory Tool

For long-running agents whose input tokens balloon as tool results pile up, here is how to pair context editing with the memory tool and measure the savings with count_tokens, including a working backend implementation.

Claude API123context editingmemory toolagents8token optimization2

✦ Premium Article

As an indie developer, I once let a research agent in my automated publishing pipeline run uninterrupted, and somewhere around the twentieth tool call the input tokens crossed 70,000 and the per-turn cost stopped being something I could ignore. When I looked inside, most of the weight came from finished web_search results sitting untouched in the conversation history. The longer an agent runs, the more those stale tool results crowd the context. That is not unique to my setup; it is a common tax on any tool-heavy workflow.

The Claude API gives you two complementary levers for this. Context editing removes old tool results on the server side, and the memory tool offloads what you want to keep into files. Because their jobs are opposite, the practical move is to use them together rather than picking one. This article builds both up from a minimal setup to a production-ready one, and ends by measuring the savings with count_tokens before any real spend, with working backend code along the way.

Context editing "removes," the memory tool "retains" — opposite jobs

The first thing worth untangling is that these two features solve genuinely different problems.

Context editing (clear_tool_uses_20250919) automatically deletes old tool results on the server side once the history crosses a threshold. Each cleared block is replaced with a placeholder, so Claude knows a result used to be there and is now gone. In other words, search results or file reads you will not revisit get dropped without you having to trim them by hand.

The memory tool (memory_20250818), by contrast, is a client-side tool that Claude uses to write and read files itself. It saves what it learns into a /memories directory and can read it back on demand, even after context editing has cleared the history or a new session has started.

That contrast is the whole point. With context editing alone, an important finding buried in a cleared tool result disappears along with it. So you write just the "I will need this later" facts into memory, and you get both: a light history and persistent knowledge. Both are enabled with the beta header context-management-2025-06-27.

Enabling clear_tool_uses in its simplest form

Start with context editing on its own, in the plainest shape. You pass a single edit in context_management.

import anthropic
 
client = anthropic.Anthropic()
 
response = client.beta.messages.create(
    model="claude-opus-4-8",
    max_tokens=4096,
    messages=[{"role": "user", "content": "Research recent work on AI agents and summarize it"}],
    tools=[{"type": "web_search_20250305", "name": "web_search"}],
    betas=["context-management-2025-06-27"],
    context_management={"edits": [{"type": "clear_tool_uses_20250919"}]},
)

Whether clearing actually ran is reported in context_management.applied_edits. If you skip checking this and just assume "it must be working," you can easily miss that it never fired at all because the threshold was never reached.

applied = getattr(response, "context_management", None)
if applied and applied.applied_edits:
    for edit in applied.applied_edits:
        print(f"cleared tool uses: {edit.cleared_tool_uses} / "
              f"tokens saved: {edit.cleared_input_tokens}")
else:
    print("nothing cleared this turn (below threshold)")

The minimal setup omits trigger, so it runs at the default threshold. On short conversations it never fires, and that is correct behavior — not firing is not a bug. Watch the counts in your logs, then move on to tuning.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦How to actually choose trigger / keep / clear_at_least / exclude_tools for clear_tool_uses_20250919
✦A working memory-tool backend with path-traversal protection built in
✦Measuring the exact token savings with count_tokens before you ship
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $15 for lifetime access
View Membership →

Related Articles

⬡ API & SDK2026-09-02
The 400 that rejects a thinking block, and finding the edit that caused it
On Claude Fable 5.1, editing the system prompt, tools, or earlier messages before a thinking block makes the request fail with a 400. Here is how to check whether your own agent loop does that in a single request, and how to move a rebuilt system prompt and client-side trimming to patterns that leave the prefix untouched.
⬡ API & SDK2026-06-29
Let Claude Actually See the Images Your Tools Return — Use Image Blocks in tool_result and Cut Tokens by Roughly 10x
Stuffing a base64 string into a tool_result makes the same image cost roughly 10–20x more tokens. Here is how to return it as an image content block instead, with SDK code, a token-cost estimate, and the gotchas I hit in production.
⬡ API & SDK2026-03-19
Claude API Advanced Tool Use: Tool Search, Programmatic Tool Calling, and Tool Use Examples
A working walkthrough of Claude API's Tool Search Tool, Programmatic Tool Calling, and Tool Use Examples — starting with the beta header that everything depends on, plus how to read Anthropic's published reduction figures and when to skip each feature.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links