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
| Stage | Starts at | Costs | Loses |
|---|---|---|---|
| Clearing results | 40 000 tokens, or half the limit | nothing | The content of old tool results. The calls stay visible. |
| Compaction | 80% of the limit | one model turn | The detail of the older half of the conversation. |
The 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.
const agent = api.agents.create({
runtime: "local", machine: pc, model: "gemini-3.5-flash",
contextTokens: 400_000, // a whole number, at least 10 000
});agent = api.agents.create(
runtime="local", machine=pc, model="gemini-3.5-flash",
context_tokens=400_000,
)Watching it
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.
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");
}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")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
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.