Context compaction

ProductionContext and memoryPublished By Simon Budziak

Context compaction replaces an AI agent's growing conversation history with a smaller representation that preserves the facts, decisions, and unfinished work needed to continue. It frees space inside the context window so a long-running agent can keep working instead of failing when its accumulated history reaches the model's limit.

OpenAI describes how Codex uses a dedicated compaction endpoint once its context crosses a threshold. Other harnesses use summaries, retained messages, or files.

When should an agent compact its context?

Compaction should happen before the context window fills, with enough space left to inspect and preserve important state. The trigger must consider both token usage and task boundaries, because compacting in the middle of a delicate operation can drop the very detail needed to finish it.

What is the main risk of compaction?

Every reduction is selective. A fluent summary may omit a failed check or reversed decision, producing context rot that looks coherent. Keep source material in files or agent memory and treat the compacted text as a navigation aid. Good context engineering decides what remains inline and what the agent harness can retrieve again. Our context engineering guide for deep agents covers those tradeoffs in detail.

Frequently asked questions

Is context compaction just conversation summarization?

Summarization is one method. A compaction system may also keep selected messages, tool results, files, or an opaque continuation state rather than one prose summary.

What can context compaction lose?

It can drop exceptions, reversals, exact wording, provenance, and unfinished details. Important state should also live in structured records or files that can be read again.

Summarize this page with

Train your team to build this