Context Compaction
Resumir y continuar
Escrito por Darjus Kazakevic · revisado el
The context window is finite. The session is not.
Every turn adds a model reply and a tool result to the transcript. The context window — the most text a model can read at once — does not grow. After a few dozen turns, a long session can fail or start losing its first instructions. Compaction replaces the older turns with a summary so the work can continue.
The loss is silent. Nothing fails when the summary drops a rule the user gave at turn three. The agent breaks it eleven turns later, and it looks like bad reasoning. So the real question is not “how to summarize” but “what must survive”.
En esencia — Compaction rewrites the agent's memory and loses detail. Whatever the summary drops is gone, so decide what must survive before it runs.
Cuándo recurrir a él
Recurre a él cuando
- Sessions often run long enough to near the window limit
- Tool results are bulky and repetitive
- The work needs one continuous thread, not separate tasks
Reconsidéralo cuando
- Sessions are short and fit easily
- Each task is independent — start a fresh context instead
- The work needs full detail throughout — use sub-agents instead
The parts
- The threshold (Watermark)
- The point at which compaction runs: a fraction of the window, not the full window. At 70%, for example, there is room to do it calmly. At 100% you compact under pressure, often mid-task.
- The pinned parts
- What is never summarized: the system prompt, the tool schemas, the original task, and the last few turns. The start and the end are cheap to keep and costly to lose. Only the middle is summarized.
- The summary
- What replaces the middle. It should read as state, not as a story: what was decided and why, what is known, what is still open, what was ruled out.
- The offload
- Writing important facts out of the window before compaction: notes to a file, findings to a memory store. What lives outside the transcript cannot be summarized away.
Reglas de diseño
- Summarize to a schema, not to a story
- List what must survive: decisions, user limits, facts, failed approaches, open work. Keep paths and ids exact. A free-text summary reads like a story, and stories drop constraints.
- Write key facts to a file first
- Anything that must not be lost should leave the transcript before compaction, as notes in a file the agent can re-read. A summary is a lossy copy; the file is the original.
- Compact at a threshold, not on an error
- Count tokens and compact at a fixed share of the window, such as 70%, after the current turn ends. If you wait until a request fails for length, you compact mid-task, at the worst moment.
- Keep the start and the end word for word
- Keep the system prompt, tool schemas, the original request and the last few turns exactly as they are. They are small and costly to lose. Summarize only the middle.
Errores habituales
- A free-text summary that drops the user's limits first.
- Paraphrased ids, so the agent looks for orderService.ts when the file is order-service.ts.
- Losing the list of failed approaches, so the agent confidently tries them again.
- Compacting between a tool call and its result, which breaks the message order.