# compact()

Canonical URL: https://developers.iagenify.com/sdk/ref/local-agent/compact/
Language: en

`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.

Status: costs a turn · works outside a run · closes rewinding

```javascript
const { removedMessages, credits } = await agent.compact();
// { removedMessages: 24, credits: 3 }
```

## What it keeps {#what}

- 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 {#when}

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 {#automatic}

At 80% of the limit, after [clearing old tool results](/sdk/agent/context/clearing) has done what it can for free. Calling it yourself is the same operation, forced.

## It closes the door behind it {#rewind}

> **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 {#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 {#outside}

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

- [Compaction](/sdk/agent/context/compaction): The automatic one.
- [What a rewind cannot undo](/sdk/agent/checkpoints/limits): Including this.

```python
summary = agent.compact()
# {"removedMessages": 24, "credits": 3, "summary": "…"}
```
