# Overview

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

A long run outgrows any window. The engine handles it in two stages: throw away old tool results for free, and only then pay a model to summarise.

Status: 200 000 tokens by default · clearing is free · compaction costs a turn

The order matters. Clearing is free and loses only material the model has already read; summarising costs a turn and loses detail. Doing the cheap thing first is why most runs never pay for the expensive one.

## The two stages {#stages}

| Stage | Starts at | Costs | Loses |
| --- | --- | --- | --- |
| [Clearing results](/sdk/agent/context/clearing) | 40 000 tokens, or half the limit | nothing | The *content* of old tool results. The calls stay visible. |
| [Compaction](/sdk/agent/context/compaction) | 80% of the limit | one model turn | The detail of the older half of the conversation. |

## The limit {#limit}

**200 000 tokens** by default, whatever the model's own window — a deliberate choice, because the useful limit is the one where a turn is still fast and affordable, not the one the provider allows. Raise it with `contextTokens`, up to the model's real window.

```javascript
const agent = api.agents.create({
  runtime: "local", machine: pc, model: "gemini-3.5-flash",
  contextTokens: 400_000,       // a whole number, at least 10 000
});
```

## Watching it {#gauge}

Every `turn` event carries the size measured by the provider, the limit in force, and a breakdown — so a gauge in an interface shows what is actually taking the room.

```javascript
onEvent: (e) => {
  if (e.type !== "turn") return;
  console.log(e.contextTokens, "/", e.contextLimit);
  console.log(e.contextParts);   // { system, tools, memory, messages }
  console.log(e.cachedTokens, "served from cache");
  console.log(e.contextCleared, "results cleared so far");
}
```

> **The total is measured, the parts are estimated**
>
> The total comes from the provider. The four parts are estimated and then scaled so they add up to it exactly — so the proportions are sound and an individual part is close, not exact.

## Attachments drop out on their own {#attachments}

A PDF, an image, a recording attached by `read` stays in the conversation for **six messages**. After that it is replaced by a line telling the model it already saw the file and can read it again. Media is heavy enough that this alone keeps many runs below the first threshold.

- [Window per model](/sdk/agent/context/window): The ceiling, and what is refused above it.
- [Compaction](/sdk/agent/context/compaction): The summary, and what it keeps.
- [Events](/sdk/agent/events): Everything onEvent receives.

```python
agent = api.agents.create(
    runtime="local", machine=pc, model="gemini-3.5-flash",
    context_tokens=400_000,
)
```

```python
def on_event(event):
    if event["type"] != "tool_end":
        return
    context = agent.context
    print(context["tokens"], "/", context["limit"])
    print(context["parts"])        # {"system", "tools", "memory", "messages"}
    print(context["cleared"], "results cleared so far")
```
