# Clearing tool results

Canonical URL: https://developers.iagenify.com/sdk/agent/context/clearing/
Language: en

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.

Status: free · keeps the last 3 turns · results over 400 characters

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 {#when}

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

## What is cleared {#what}

| Kept | Cleared |
| --- | --- |
| Everything in the last **3** assistant turns | Tool results older than that |
| Results under 400 characters | Results of 400 characters or more |
| The tool call itself — name and arguments | What it returned |
| — | Attachments on a cleared result |

```javascript
// What the model sees in place of an old result
"[Result cleared to save context — call read again if you still need it.]"
```

> **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 {#why-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 {#batches}

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 {#counting}

```javascript
onEvent: (e) => {
  if (e.type === "turn" && e.contextCleared) {
    console.log(e.contextCleared, "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.

- [Compaction](/sdk/agent/context/compaction): What happens when clearing is not enough.
- [read](/sdk/agent/tools/read): The tool whose results are usually the heaviest.

```python
# What the model sees in place of an old result
"[Result cleared to save context; call read again if needed.]"
```

```python
def on_event(event):
    if event["type"] == "tool_end" and agent.context["cleared"]:
        print(agent.context["cleared"], "results cleared so far")
```
