# Context Compaction

> When the context window nears its limit, replace older conversation spans with a model-written digest that preserves decisions and constraints — without stopping the task.

- **Category**: Agentic AI
- **Subcategory**: Memory
- **Canonical URL**: https://designpattern.fyi/patterns/context_compaction/

---

## Description
**Intent**: When the context window nears its limit, replace the older conversation span with a model-written digest that preserves decisions, commitments, and active constraints — discarding noise — so the agent keeps running without losing the thread.
**Context**: A long-running agent accumulates turns — tool calls, raw observations, intermediate reasoning — until history approaches the model's context-window limit mid-task. Most older turns are process noise: superseded plans, large tool dumps, abandoned branches. The decisions and conclusions they produced still matter.
**Solution**: Track context-window utilization. When it crosses a threshold (e.g. 80%), run a compaction pass: feed the older span to the model with an instruction to produce a dense digest preserving goals, decisions, open commitments, and constraints — while discarding raw tool output, superseded plans, and dead-end reasoning. Replace that span with the digest; keep the most recent turns verbatim for local continuity. Pin content that must never be compacted away — the original task statement and hard constraints — outside the compactable region.



## Use Cases
- The agent runs long enough that history approaches the context-window limit.
- Older turns are dominated by raw tool output and superseded reasoning.
- The task must continue past the point where the window would otherwise overflow.
- Decisions and constraints worth preserving can be identified for the digest.






## Trade-offs


### Advantages

- The agent runs past the nominal window limit on long tasks.

- Per-call cost and latency drop because the carried history shrinks.

- Decisions and commitments survive while raw noise is shed.

- A pinned preamble guarantees task statement and hard constraints are never summarized away.




### Considerations & Drawbacks

- Compaction is lossy — a dropped detail the agent needs later cannot be recovered from the digest.

- A summarization error can silently rewrite a commitment or constraint.

- Each trigger costs an extra model call for the compaction pass.

- Too small a recent-verbatim window blurs the agent's sense of what just happened.







---
**Reference**: [Original Source](https://www.agentpatternscatalog.org/patterns/context-compaction/)

