Context compaction

  • Context
  • Industry term
4 steps

Context compaction makes room in a long AI conversation by replacing earlier messages with a shorter record of the conversation. Some details may be lost, though the conversation can continue.

The model, the trained AI that generates replies, works with the information the app sends it. After compaction, the app sends the shorter record instead of the messages it replaced. A missing detail must be sent again before the model can use it, even if the original message is still visible on screen.

Where you’ll meet it

Partway through drafting a client proposal, your AI app reports that it has compacted the conversation. Its next draft includes a service you ruled out near the start because it would exceed the client's budget.

What to do

Point out the missed budget and excluded service, then have the agent recover the approved decisions and record them in the project's instructions or a file it rereads after compaction. Make maintaining that record part of its instructions, so the next summary isn't the only place those decisions survive.

Easy to mix up

Context rot happens while every message is still there, and the model uses them less reliably as the input grows. Compaction replaces the earlier messages with a shorter record of them.

Check yourself

A long chat is compacted, then later compacted again. The first summary leaves out a supplier's delivery date. You never supply the date again. Which explanation better fits its absence from the second summary?

Go deeperShorter records take different forms and can interrupt reuse of earlier work

Anthropic's September 2025 context-engineering article describes model-written summaries. OpenAI's compaction documentation describes a different format: an encrypted record, encoded so people can't read it directly. It preserves information from earlier steps in fewer tokens, the small units models use to process text.

OpenAI's prompt-caching documentation describes another tradeoff. Caching can reuse calculations for text the model has already processed at the start of a request. Replacing early messages changes that text, so earlier calculations may no longer match. Anthropic's threshold-compaction documentation also notes that generating a summary adds another request to the model, which counts toward usage.

Sources

Practice Modern AI

Or take the learning quiz on a mix of every set