# 26.3 Tool-output pruning and context editing (/building-ai-workers/context-engineering/tool-output-pruning-and-context-editing)

---
type: Document
title: "26.3 Tool-output pruning and context editing"
description: "Why a tool result is the safe thing to cut, how to make it small at the tool, and how the API clears old results."
status: stable
order: 326.03
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T08:16:27Z
chapter: '26'
part: III
expert_status: manager
concepts: [ "26.3" ]
last_verified: '2026-11-30'

sources:
  - id: anthropic-context-editing
    title: Context editing (verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/build-with-claude/context-editing
  - id: anthropic-eng-context
    title: Effective context engineering for AI agents (Anthropic, verified 2026-11-29)
    resource: https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents
  - id: anthropic-cookbook-context
    title: 'Context engineering: memory, compaction, and tool clearing (Anthropic cookbook, verified 2026-11-29)'
    resource: https://platform.claude.com/cookbook/tool-use-context-engineering-context-engineering-tools
  - id: anthropic-manage-tool-context
    title: Manage tool context (Anthropic, verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/agents-and-tools/tool-use/manage-tool-context
  - id: anthropic-tool-search
    title: Tool search tool (Anthropic, verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool
  - id: anthropic-programmatic-tool-calling
    title: Programmatic tool calling (Anthropic, verified 2026-11-29)
    resource: https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling
  - id: anthropic-sdk-agent-loop
    title: How the agent loop works, Agent SDK (Anthropic, verified 2026-11-29)
    resource: https://code.claude.com/docs/en/agent-sdk/agent-loop
generated:
  at: 2026-11-30T08:16:27Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**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.

<!-- EDITOR: which everyday picture does 26.3 use? This draft uses a mechanic who returns tools to the shop's shelf and keeps the job card, because four of the eight pictures were a student with paper, and 26.1's desk and this section's library desk were close enough to blur. The other option is the first draft's picture: a reader at a library desk who returns books and keeps the notebook. No quiz item names either picture. -->

**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](memory-outside-the-window.md)). 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.

<!-- OPEN: the number of open invoices on April 30, the turn and token count at the middle of the run, and the cost before and after each cut are not fixed until the run is recorded (spec, raised point 8). No number is written here. -->

### 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".[^anthropic-eng-context]

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](memory-outside-the-window.md)).

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".[^anthropic-eng-context]

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](../connectors-and-mcp/a-tool-is-its-description.md)). You met the same idea as a hosted pattern in [23.17](../hosted-agents/patterns-and-when-to-host-it-yourself.md). There, a large output goes to a file, and the model reads only the parts it needs.

<!-- OPEN: how `find_vendor` is asked for fewer fields is not decided. The options are an optional argument that lists fields, or a setting in the connector for the review's caller. The full note is the OPEN comment at Lab 26, Task 3. -->

<!-- EDITOR: does the description of `find_vendor` change when the tool can return fewer fields? This draft says neither that it changes nor that it is fixed: 26.3 says only that the description is where the model learns what comes back, and Lab 26, Task 3 keeps today's result as the default. The options: the description gains a line on the optional fields, or it stays word for word as 17.2 has it and the review's caller is set in the connector. The choice follows the open design of `find_vendor` in the OPEN comment above. -->

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.[^anthropic-context-editing] The model still sees that the call was made, because clearing works "while keeping the record that the call happened".[^anthropic-cookbook-context]

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."[^anthropic-context-editing] This matters on Monday. The check by code compares the report with the recorded tool results ([23.16](../hosted-agents/loops-check-feedback-and-stop.md)), 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.[^anthropic-context-editing]
- *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.[^anthropic-context-editing] 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".[^anthropic-context-editing] 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.

<!-- OPEN: no source note defines preserved thinking. The context editing page only lists the models that have it. Either add the vendor's preserved thinking page as a source and define the term from it, or cut the sentence that names it. -->

### 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.[^anthropic-manage-tool-context] The cut at the tool is this chapter's own first step.

| What the trace shows | The cut | Its price, and where it fits |
| --- | --- | --- |
| A result larger than the task needs | Cut at the tool | Work on the tool and its description. Built in Lab 26, Task 3 |
| Old tool results in the history | Clearing by the API | Breaks the cache, and may cost a second call. Built in Lab 26, Task 3 |
| Many tool definitions, sent on every request | **Tool search:** Claude searches your list of tools and loads only the ones it needs[^anthropic-tool-search] | Fits "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 next | **Programmatic tool calling:** Claude calls your tools from code, and "intermediate results are not loaded into Claude's context window"[^anthropic-programmatic-tool-calling] | The 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 turn | Caching tool definitions | It "doesn't reduce the number of tokens in context, but it reduces what you pay for them".[^anthropic-manage-tool-context] 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.[^anthropic-sdk-agent-loop]

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."[^anthropic-cookbook-context] "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.

[^anthropic-eng-context]: Effective context engineering for AI agents, Anthropic.
[^anthropic-context-editing]: Context editing, Anthropic.
[^anthropic-cookbook-context]: Context engineering: memory, compaction, and tool clearing, Anthropic cookbook.
[^anthropic-manage-tool-context]: Manage tool context, Anthropic.
[^anthropic-tool-search]: Tool search tool, Anthropic.
[^anthropic-programmatic-tool-calling]: Programmatic tool calling, Anthropic.
[^anthropic-sdk-agent-loop]: How the agent loop works, Agent SDK, Anthropic.
