Context

Compaction

At 80% of the limit the older part of the conversation is replaced by a summary the model writes itself. It costs one turn, and it is the only thing that loses detail.

at 80% of the limitcosts a turnagent.compact() on demand
Using Python?

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

When it happens

Before a turn, once the conversation passes 80% of the limit and holds at least 10 messages. It runs after clearing has already done what it can.

What it does

  • The last 8 messages are kept, with the cut moved back to a user or assistant message — never between a tool call and its result.
  • Everything before the cut is sent to the same model, with a summarising instruction, capped at 4 000 output tokens.
  • The conversation becomes: the summary as a user message, a one-line acknowledgement, then the kept tail.
  • A compacted event is emitted with how many messages went and what the summary cost.

What the summary is told to keep

KeptWhy
The user's goals and constraintsWithout them the rest of the run drifts.
Decisions already madeSo they are not re-litigated.
Files created or changed, with their pathsThe work done so far has to stay findable.
Commands run, and how they wentTo avoid repeating a failure.
Open problemsWhat is left.
The current todo listThe plan survives the summary.

It is billed, and it counts against the budget

onEvent: (e) => {
  if (e.type === "compacted") {
    console.log(`summarised ${e.removedMessages} messages for ${e.credits} credits`);
  }
}
# The Python engine compacts on its own when a session (memory=True) outgrows its limit;
# the run's usage then includes the summary's credits.
result = agent.run(message="…")
print(result["usage"]["credits"])

Doing it on purpose

const { removedMessages, credits } = await agent.compact();
summary = agent.compact()
removed_messages, credits = summary["removedMessages"], summary["credits"]

Useful between two phases of work — the investigation is summarised, the execution starts light. It works outside a run: the session is reopened first if it is not already.

Compaction removes the ability to rewind past it

The summarised messages are gone, so the conversation cannot be cut before the first surviving one. Checkpoints older than that are forgotten rather than offered in vain, and rewinding to one answers checkpoint_compacted.

In a session

With memory: true the summarised state is written to the device journal, so reopening the session resumes from the summary rather than replaying everything — and the summary is kept on the account too.