# Compaction

Canonical URL: https://developers.iagenify.com/sdk/agent/context/compaction/
Language: en

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.

Status: at 80% of the limit · costs a turn · agent.compact() on demand

## When it happens {#when}

Before a turn, once the conversation passes **80%** of the limit and holds at least 10 messages. It runs after [clearing](/sdk/agent/context/clearing) has already done what it can.

## What it does {#how}

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

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

```javascript
onEvent: (e) => {
  if (e.type === "compacted") {
    console.log(`summarised ${e.removedMessages} messages for ${e.credits} credits`);
  }
}
```

## Doing it on purpose {#ondemand}

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

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

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.

- [What a rewind cannot undo](/sdk/agent/checkpoints/limits): Including a compaction that happened in between.
- [compact()](/sdk/ref/local-agent/compact): The method.
- [done](/sdk/agent/events/done): What a run reports at the end.

```python
# 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"])
```

```python
summary = agent.compact()
removed_messages, credits = summary["removedMessages"], summary["credits"]
```
