LocalAgent

compact()

agent.compact() → Promise<{ removedMessages, credits }>

Summarise the conversation now, instead of waiting for the limit. One model turn, and the older part becomes a summary.

costs a turnworks outside a runcloses rewinding
Using Python?

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

const { removedMessages, credits } = await agent.compact();
// { removedMessages: 24, credits: 3 }
summary = agent.compact()
# {"removedMessages": 24, "credits": 3, "summary": "…"}

What it keeps

  • The user's goals and constraints.
  • Decisions already made.
  • Files created or changed, with their paths.
  • Commands run, and how they went.
  • Open problems, and the current todo list.

The last messages are kept as they are, with the cut moved back to a user or assistant message — never between a tool call and its result.

When to call it

Between two phases of work: the investigation is summarised, the execution starts light. Or after a context_too_large, as the thing to do before retrying.

It also happens on its own

At 80% of the limit, after clearing old tool results has done what it can for free. Calling it yourself is the same operation, forced.

It closes the door behind it

Compaction removes the ability to rewind past it

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

In a session

The compacted state is written to the device journal and the summary kept on the account — so reopening resumes from the summary rather than replaying everything.

It works outside a run

The session is reopened first if it is not already, which is what lets an interface offer « compact this conversation » as a button.