Skip to content
The AI Agent Factory

26.3 Tool-output pruning and context editing

Approved by
Panaversity ·
19 min read

In everyday life. A mechanic repairs a car in a shared workshop. The tools come from the shop's shelf, and the job has a job card, where the mechanic writes what was found and what was done. When the bench is full, the mechanic puts back the tools that are finished with. Nothing is lost, because the shelf still has them and any tool can be fetched again. The job card never goes on the shelf, because nobody else has a copy. A careful mechanic also asks the storeroom for one part, not the whole box. In this concept, the tools on the bench are tool results, the shelf is the connector, and the job card is the progress file.

What you will learn. You practice this in Lab 26, Task 3.

  • Why is a tool result the safe thing to cut?
  • How do you make a result small before it arrives?
  • How does the API clear old tool results?
  • Why do you never cut the conversation by hand?
  • Which way of cutting tool context fits which problem?

Why it matters. In a long run, tool results fill most of the window. If you cut the wrong kind of text, the worker loses a rule or a decision. If you cut this kind, the worker only has to fetch it again.

At work. In Friday's run (the overview), every tool result stayed in the window. Each find_vendor result brings the vendor's payment terms, the last four digits of its bank account, and the AP team's note. The review needs only the terms. By the middle of the run the window holds mostly tool results. On Monday the rule and the worker's decisions first get a home outside the window (26.6). Only then do you cut, twice: find_vendor returns only the terms, and the API clears old tool results. After each cut, the same check by code runs on every invoice. You also read the tokens by part and the run's cost, against the review's budget of $1.00.

Why is a tool result the safe thing to cut?

Because it lives somewhere else, and the worker can fetch it again. A tool result is evidence, the third of this chapter's three kinds. The connector still holds the vendor's terms after the text leaves the window. A rule that was said only in the request has no other home. A decision that is only in one of the worker's turns has none either.

The exam guide names this technique tool-output pruning: taking tool results the model no longer needs out of what it reads. Anthropic's engineers call clearing tool results "One of the safest lightest touch forms of compaction".1

It has a price. Fetching again is a second call, paid in tokens, in time and from the budget. And the record may have changed since the first call. So a decision keeps the value it was made on, in the progress file (26.6).

Not pruning is a fair choice for a short run. One question answered in a few turns never fills the window, and pruning would add settings and save nothing.

How do you make a result small before it arrives?

Have the tool return only the fields the task needs. This is the first cut, and the cheapest. A field that never arrives costs nothing on any turn (26.1). Anthropic's advice for tools is the same: return "information that is token efficient".1

In this book's lab, you change the connector so that the review can ask find_vendor for the terms alone. The model must still be told what comes back, and the tool's description is the one place it learns that (17.2). You met the same idea as a hosted pattern in 23.17. There, a large output goes to a file, and the model reads only the parts it needs.

The price is work on the tool. Other work still needs the bank digits, so today's full result stays the default, and only the review asks for less.

How does the API clear old tool results?

It swaps each of the oldest tool results for a placeholder, a short line that stands in its place. It does this only in what the model reads, and leaves your copy alone. Anthropic calls the feature context editing: the API changes the request after your code sends it and before the model reads it. When the input passes a size you set, "the API automatically clears the oldest tool results in chronological order". It replaces each one with "placeholder text indicating to Claude that it was removed". By default the last 3 tool uses are kept.2 The model still sees that the call was made, because clearing works "while keeping the record that the call happened".3

Where does the full text still exist? In two places. The connector can return it again. And the builder's own copy of the conversation, the array of 21.5, is untouched: "Context editing is applied server-side before the prompt reaches Claude. Your client application maintains the full, unmodified conversation history."2 This matters on Monday. The check by code compares the report with the recorded tool results (23.16), and every one of them is still in the array.

Clearing has two prices.

  • The cache. Clearing "Invalidates cached prompt prefixes when content is cleared". That means the API must process the cached part of the request (26.2) again. So one setting gives a minimum number of tokens to clear each time, and a clearing that would save less does not happen.2
  • The second call. A worker that needs a cleared result calls the tool again.

The memory tool is a tool that lets the model keep files between requests (26.6). With it on, "Claude receives an automatic warning to preserve important information" before results are cleared.2 That warning is the worker's moment to write what it decided into the progress file.

Why do you never cut the conversation by hand?

Because your copy is the record, and an edited record breaks the turns after it. The rule from 21.5 still holds: "You may add, but do not rewrite the past." Some models keep their earlier thinking, the reasoning a model writes before its answer, in the conversation, which the vendor calls preserved thinking. On those models, "Client-side edits to earlier turns can invalidate the thinking blocks in every later assistant turn", while "server-side context management never invalidates thinking blocks".2 The lab's model is one of them. A trimmed array has also lost tool results, so the check by code has nothing to compare the report with.

Which way of cutting tool context fits which problem?

Start with the part the trace shows is largest. Anthropic's page on tool context lists four approaches, and each fits a different part of the window.4 The cut at the tool is this chapter's own first step.

What the trace showsThe cutIts price, and where it fits
A result larger than the task needsCut at the toolWork on the tool and its description. Built in Lab 26, Task 3
Old tool results in the historyClearing by the APIBreaks the cache, and may cost a second call. Built in Lab 26, Task 3
Many tool definitions, sent on every requestTool search: Claude searches your list of tools and loads only the ones it needs5Fits "10 or more tools", or definitions over 10,000 tokens. Brightline's worker has four connector tools and the memory tool, so the lab does not build it. Chapter 33 has the depth
Results that only pass from one tool to the nextProgrammatic tool calling: Claude calls your tools from code, and "intermediate results are not loaded into Claude's context window"6The calls run from a code execution container, a closed machine on the vendor's side. Not built in the lab
The same tool definitions, paid for on every turnCaching tool definitionsIt "doesn't reduce the number of tokens in context, but it reduces what you pay for them".4 It fixes cost, not bloat

Tool search is already at work in the Agent SDK, the harness of Chapter 22. By default the SDK does not send the definitions of connector tools at first, and it loads them when they are needed.7

Clearing is one of three moves. Anthropic's cookbook separates them in one line: "compaction compresses the whole window when it grows too large, clearing drops stale re-fetchable data inside the window, and memory moves information out of the window so it survives across sessions."3 "Stale re-fetchable data" means old results that a tool can return again.

The operating rule: cut evidence, and nothing else. Make each result small at the tool, let the API clear old tool results, and never edit your own copy of the conversation.

Do this now. Ask your coding agent to list what one project's tools return. Copy this line:

List every tool this project's worker can call. For each one, list every field its result returns, and mark the fields the worker's main task needs. For each field left unmarked, say where it still exists if the worker never receives it. Change no file.

Check yourself

Question 1 / 8

0 answered

Why is a tool result the safe kind of text to cut from a long run?

  1. Effective context engineering for AI agents, Anthropic.↑↑2
  2. Context editing, Anthropic.↑↑2↑3↑4↑5
  3. Context engineering: memory, compaction, and tool clearing, Anthropic cookbook.↑↑2
Owner
Panaversity