Get an API key
Context

Clearing tool results

When the conversation gets heavy, the content of old tool results is replaced by a line saying it was cleared. The call stays; only what it returned goes.

freekeeps the last 3 turnsresults over 400 characters
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

This is the cheap half of context management, and it runs before every turn. Nothing is sent anywhere and nothing is charged — the engine edits its own message list.

When it starts

Once the conversation passes 40 000 tokens, or half the limit, whichever is lower. Below that, nothing is touched.

What is cleared

KeptCleared
Everything in the last 3 assistant turnsTool results older than that
Results under 400 charactersResults of 400 characters or more
The tool call itself — name and argumentsWhat it returned
—Attachments on a cleared result
// What the model sees in place of an old result
"[Result cleared to save context — call read again if you still need it.]"
# What the model sees in place of an old result
"[Result cleared to save context; call read again if needed.]"
The message names the tool, so recovery is one call

The model is not told « something was here »; it is told which tool to call again. A cleared result is therefore a cheap round trip, not lost information — and in practice it is rarely needed, because the model has already used what it read.

Why the call stays visible

Removing the call as well would let the model repeat work it has already done — it would have no record of having read the file at all. Keeping the call and dropping the payload preserves the history of actions, which is what a plan is built on, and drops the bulk, which is what costs.

It is done in batches, for the cache

Clearing happens in groups rather than one message at a time, so the beginning of the prompt stays identical between turns and the provider's cache keeps serving it. Rewriting one old message every turn would invalidate the cached prefix each time — a saving on tokens paid for in cache misses.

Counting it

onEvent: (e) => {
  if (e.type === "turn" && e.contextCleared) {
    console.log(e.contextCleared, "results cleared so far");
  }
}
def on_event(event):
    if event["type"] == "tool_end" and agent.context["cleared"]:
        print(agent.context["cleared"], "results cleared so far")

A number that climbs steadily on a long run is normal. One that climbs fast on a short run means the agent is reading very large things — a sign to narrow what it reads, or to raise the limit.