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
compactedevent is emitted with how many messages went and what the summary cost.
What the summary is told to keep
| Kept | Why |
|---|---|
| The user's goals and constraints | Without them the rest of the run drifts. |
| Decisions already made | So they are not re-litigated. |
| Files created or changed, with their paths | The work done so far has to stay findable. |
| Commands run, and how they went | To avoid repeating a failure. |
| Open problems | What is left. |
| The current todo list | The 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.
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.