# Context Compaction

> The Context Compaction is a technique of summarizing a long conversation to free space in the context window.

## What is context compaction in AI agents?

The technique where an agent replaces older parts of the conversation with a concise summary when the context window fills up. Allows long sessions to continue, but some details may be lost.

- Topic: Agentic Coding
- Type: technique

## Example

Wrong:

```text
Summarize the conversation so far.
```

Right:

```text
Summarize the conversation for continuing the task. Keep:
- the goal and constraints
- decisions made and why
- files changed and open tasks
- exact file paths and error messages, verbatim
```

A structured compaction prompt keeps the goal, decisions and exact values that a generic summary often drops.

## Common mistakes

- Losing important constraints in the summary, so the agent repeats a fixed mistake.

## Key points

- The summary keeps the goal, decisions, changed files and open tasks.
- Exact values that must not be lost, like file paths or error messages, are kept verbatim.
- Compaction happens before the context window is full, not after requests start failing.

## Interview questions

### Context compaction vs truncation: what is the difference?

Truncation drops the oldest messages entirely. Compaction replaces them with a summary, so key facts survive in shorter form.

### Why does my agent forget things after compaction?

The summary left out details it needed. Keep important rules in persistent instructions or notes files, not only in the conversation.

### Can I trigger context compaction manually?

Many coding agents offer a command to compact on demand, which helps at natural breakpoints, such as after finishing a subtask.

## Related cards

- [Context Window](https://wonderium.dev/ai/context-window.md)
- [Context Rot](https://wonderium.dev/ai/context-rot.md)
- [Context Engineering](https://wonderium.dev/ai/context-engineering.md)
- [Agent Memory](https://wonderium.dev/ai/agent-memory.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/context-compaction
- Last updated: 2026-11-25
