Skip to content
AgenticAIWiki
  1. Learn
  2. Part 11
  3. 58. Context engineering
  4. 58.3

Lesson 58.3 · 3 of 7 · 4 min read · Reviewed 24 Nov 2026

Trimming and clearing tool results

By the end, you can

  • Remove old tool results that no longer matter.
  • Keep a short note of what was found instead of the raw output.

In agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. runs, tool results are usually the biggest consumer of context: file contents, search results, API responses, logs.

Most results have a short life

A search result is useful for choosing the next file to read. Twenty turns later, its raw text is just noise.

Ways to trim

  1. Return less in the first place. Tools should return the useful fields, not whole records (34.1).
  2. Clear old results. Replace the raw content of results older than N turns with a placeholder: “[log from turn 3 removed; it showed 3 timeout errors at 14:02]”.
  3. Keep an id so the agent can fetch it again if it really needs to.

Be careful with

  • Results the agent still relies on, such as the current plan or the latest test run.
  • Results with facts that appear nowhere else. Summarise them before removing.

Claude’s API can do some of this for you with context editingContext editing: Removing or replacing parts of an agent’s context, such as old tool results, while it runs. (58.6).

Key points

  • Tool results are often the largest part of an agent's context.
  • Once a result has been used, its raw content is rarely needed again.
  • Replace old results with a short placeholder or summary, keeping ids so they can be fetched again if needed.
Hands-on labRuns in the Playground

Clear old tool results with useful placeholders

Write a trimming function that clears old tool results, keeps the newest few and any excluded tools, and leaves a placeholder with an id and the key fact.

You will have: A clear_old function and a before and after view of a sample message list with token counts.

Steps

  1. Read MESSAGES: a mix of chat turns and tool results with an id, a tool name, content and a one-line summary.
  2. Write clear_old(messages, keep, exclude) that replaces the content of all but the newest keep tool results.
  3. Never clear results from tools in exclude (here get_plan).
  4. Each placeholder reads: [cleared <id>]: <summary>.
  5. Print the messages and the tokens before and after.

Done when

  • The 3 newest tool results are untouched.
  • get_plan's result is never cleared.
  • Placeholders keep the id and summary, and tokens drop by more than half.

Open in the Playground No API key needed.

Starter code (Python)
MESSAGES = []
for i in range(1, 11):
    MESSAGES.append({"type": "text", "content": "assistant step %d" % i})
    tool = "get_plan" if i == 2 else "read_log"
    MESSAGES.append({"type": "tool_result", "id": "t%d" % i, "tool": tool, "content": "log line ... " * 80,
                     "summary": "step %d log: %d timeouts" % (i, i % 3)})

def tokens(msgs):
    return sum(len(m["content"]) // 4 for m in msgs)

def clear_old(messages, keep=3, exclude=("get_plan",)):
    # TODO: find tool results (oldest first), skip excluded tools and the newest `keep`,
    # and replace content with "[cleared <id>]: <summary>"
    return messages

before = tokens(MESSAGES)
clear_old(MESSAGES)
print("tokens before:", before, "after:", tokens(MESSAGES))
Show a solution
MESSAGES = []
for i in range(1, 11):
    MESSAGES.append({"type": "text", "content": "assistant step %d" % i})
    tool = "get_plan" if i == 2 else "read_log"
    MESSAGES.append({"type": "tool_result", "id": "t%d" % i, "tool": tool, "content": "log line ... " * 80,
                     "summary": "step %d log: %d timeouts" % (i, i % 3)})

def tokens(msgs):
    return sum(len(m["content"]) // 4 for m in msgs)

def clear_old(messages, keep=3, exclude=("get_plan",)):
    results = [m for m in messages if m["type"] == "tool_result"]
    for m in results[:-keep] if keep else results:
        if m["tool"] in exclude:
            continue
        m["content"] = "[cleared " + m["id"] + "]: " + m["summary"]
    return messages

before = tokens(MESSAGES)
clear_old(MESSAGES)
for m in MESSAGES:
    if m["type"] == "tool_result":
        print(m["id"], m["tool"], "->", m["content"][:40])
print("tokens before:", before, "after:", tokens(MESSAGES))
Quick checkTurn 3 fetched a 40 KB log; by turn 20 the agent has already used it. What should happen to it?
How sure are you?
Pick an answer and how sure you are