Cost and token budgets
Old pages in context: clear tool results, stop paying every turn
Set up tool result clearing for a fetching agent with Claude context editing or LangChain's ClearToolUsesEdit: trigger, keep, placeholder and cache cost.
By Amaan Mcdonald, AgentFetch team · Published
This article was drafted with AI assistance from the sources listed below and edited and published by the AgentFetch team.
- Trigger: both tools start clearing at 100,000 input tokens by default; fetch-heavy agents may go earlier.
- Keep: The 3 most recent tool results stay by default; older ones are cleared first.
- Inputs: Leave clear_tool_inputs false so the model still sees which URL it read.
- Exclude: Put a small notes tool in exclude_tools so extracted facts survive.
- Cache: Clearing invalidates the cached prefix; use clear_at_least so each clear is worth it.
The agent read six pages to answer one question. By step ten it has used two of them, and all six are still in the request, billed as input again on every turn. The token budget guide says to trim old tool results. This post covers the two mechanisms that do it for you, Claude's context editing and LangChain's ClearToolUsesEdit, and the settings that matter when the tool results are web pages.
Checked against the Claude context editing docs and the LangChain built-in middleware docs, November 2026. Both move quickly, so recheck the defaults before you rely on them.
Why are old pages still in the request?
Default: assume every page the agent fetched is resent with every later model call. The request carries the conversation, and tool results are part of it.
It stops being acceptable when pages are long and the run is long. That is where clearing helps: replace a used page with a short placeholder, and keep only the recent results in full.
What does Claude's context editing do?
Default: turn on the clear_tool_uses_20250919 strategy with its defaults. It is a beta behind the header anthropic-beta: context-management-2025-06-27, and the minimal form is one line in the request body:
{
"context_management": {
"edits": [{ "type": "clear_tool_uses_20250919" }]
}
}
With no options, clearing starts once the input reaches 100,000 tokens and keeps the 3 most recent tool use and result pairs. The oldest results go first, and each one is replaced with placeholder text that tells Claude the result was removed. The clearing happens on the server before the prompt reaches the model, so your client keeps the full, unedited history for logs and debugging.
It stops fitting when a fetch-heavy agent reaches its useful limit well before 100,000 tokens. The trigger also takes a count of tool uses, so you can clear by number of reads rather than by size:
{
"context_management": {
"edits": [
{
"type": "clear_tool_uses_20250919",
"trigger": { "type": "tool_uses", "value": 6 },
"keep": { "type": "tool_uses", "value": 3 },
"clear_at_least": { "type": "input_tokens", "value": 5000 },
"exclude_tools": ["save_note"],
"clear_tool_inputs": false
}
]
}
}
Six and three are starting values for a reading agent, not measured ones. save_note stands for a small tool of your own, covered below.
What is the LangChain equivalent?
Default: add ContextEditingMiddleware with a ClearToolUsesEdit to create_agent. The parameters mirror Claude's: trigger (100,000 tokens by default), keep (3), clear_at_least (0, which clears as much as needed), clear_tool_inputs (False) and exclude_tools.
from langchain.agents import create_agent
from langchain.agents.middleware import ContextEditingMiddleware, ClearToolUsesEdit
agent = create_agent(
model=model, # your chat model
tools=[fetch_page, save_note],
middleware=[
ContextEditingMiddleware(
edits=[
ClearToolUsesEdit(
trigger=60000,
keep=3,
exclude_tools=["save_note"],
placeholder="[page text removed to save context; the URL is in the call above. Fetch it again only if you need an exact quote.]",
),
],
token_count_method="model",
),
],
)
Two settings differ from Claude's. The placeholder is yours to write (the default is [cleared]), and token_count_method is 'approximate' unless you set it to 'model'.
It stops being equivalent when you assume the counts match. An approximate count can start clearing earlier or later than the model's own tokenizer would.
Should the model still see the URL?
Default: keep clear_tool_inputs off. Both tools clear only the results unless you ask for more, so the call with its URL stays visible while the page text goes.
For a fetch tool that matters. The model can still cite the page and knows it already read it. A trace with clearing on looks like this:
step 2 fetch_page(url="https://example.com/pricing") -> 9,800 tokens of Markdown
step 3 save_note("example.com/pricing: refunds within 30 days")
...
step 9 (clearing runs: step 2 result -> placeholder)
step 10 model answers from the note and cites https://example.com/pricing
It stops working when the model treats a cleared page as missing and fetches it again. Then you pay for the fetch and the tokens a second time. Two guards: a placeholder that says when a re-fetch is worth it, and a per-task fetch budget in code, so repeat reads count against the same limit as new ones.
How do facts survive the clearing?
Default: give the agent a small notes tool and list it in exclude_tools. Its results are never cleared, so the extracted facts outlive the pages they came from. On Claude, the memory tool does a similar job: as the context nears the clearing threshold, Claude gets an automatic warning to save what matters before the results are cleared.
It stops working when the note is wrong. A note written from a page that carried injected instructions is still text from that page, and it stays as untrusted as the page was.
What does clearing cost?
Default: set clear_at_least on Claude. Clearing invalidates the cached prompt prefix where the content was removed, and with clear_at_least set the strategy is skipped when it cannot free at least that many tokens. A small clear that forces a full cache write can cost more than it saves.
To see what happened, log the response. context_management.applied_edits reports cleared_tool_uses and cleared_input_tokens for each edit (in the final message_delta event when streaming). The token counting endpoint accepts the same context_management block and returns the count after clearing along with original_input_tokens, so you can preview the saving before a run.
Where this breaks
- The answer needs an exact quote from an early page. Once it is cleared, the model has a placeholder. Raise
keep, or save the quote as a note when the page is read. - Excluding the fetch tool itself. Then nothing is ever cleared. Exclude the notes tool, not the reader.
- Long single pages. Clearing helps across turns, not within one turn. A 40,000-token page is still 40,000 tokens on the step that reads it; the per-page cap does that job.
- Clearing is not a security boundary. Instructions in a page the model has already read may have shaped its next steps before the page was cleared.
For the failure side of the same loop, see what to do when a fetch fails and the agent answers anyway.
Frequently asked questions
Why does a fetched web page cost tokens on every agent turn?
The whole conversation, including earlier tool results, is sent as input with each model request. A page read early in a run is paid for again at every later step until something removes it.
What does Claude's tool result clearing do by default?
The clear_tool_uses_20250919 strategy starts once the input reaches 100,000 tokens, keeps the 3 most recent tool use and result pairs, and replaces older results with placeholder text. It runs on the server, so your client still holds the full history.
What is ClearToolUsesEdit in LangChain?
It is the edit used by LangChain's ContextEditingMiddleware. It clears older tool outputs once a token trigger is reached (100,000 by default), keeps the 3 most recent, and replaces each cleared output with the placeholder [cleared], which you can change.
Does clearing tool results break prompt caching?
On Claude, clearing invalidates the cached prefix at the point where content was removed. The clear_at_least setting makes the strategy skip a clear that would free fewer tokens than you think is worth a new cache write.
Should I also clear the tool call inputs?
For a fetch tool, usually not. Keeping the inputs leaves the URL visible, so the model knows what it already read and can cite it, while the page text itself is gone.
Sources
- Context editing (Claude Docs) (Documentation)
- Built-in middleware (LangChain Docs) (Documentation)