# What Is Compaction (Summarising History)?

> What compaction is, why a summary can drop a rule, and how to keep a long-running AI agent on track, explained simply for your AI interview.

**In short:** Compaction is when an AI app replaces the older part of a long conversation with a summary, then carries on in a shorter window (the space the model can see). It saves room and cost, but a summary loses some detail, so keep rules and limits in code.

AI and LLM interview question Q096, Prompt and context engineering. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-compaction-summarising-history

## Say it in 30 seconds

Compaction means summarising the older part of a long chat, so the model can keep working in a short window, the space it can see. An agent running for hours would otherwise run out of room. The catch is that a summary loses detail, so a small rule can disappear. I keep the limits in code, keep the last few turns word for word, and check the summary before the agent acts on it.

## Say it in 2 minutes

A model can only see what fits in its context window. A long job, like a motor claim that runs for three days or a coding agent in a long session, fills that window. Compaction is the fix. The app writes a summary of the older turns, then starts a fresh, shorter window with that summary in their place.

The catch is that a summary is lossy. It keeps the big picture and the decisions, but it can drop a small detail that matters later. Anthropic says the art of compaction lies in choosing what to keep, since overly aggressive compaction can lose critical context. In a claims agent, a payout limit that appears once early in the file can vanish, and the agent then pays out a claim that should have gone to a person.

So I don't trust the summary with the rules. The limit lives in code, the last few turns stay word for word, and the summary prompt asks for the goal, the decisions, the IDs and the open questions. I tune it by catching everything first, then cutting the padding.

Cutting and compaction both shorten a chat, but they lose different things. Cutting throws the early context away with no summary at all. Compaction keeps the gist, at the cost of a model call and a wait, and for a long job that trade is usually worth it.

## What is compaction, in plain words?

A model only sees what fits in its context window, the space that holds everything it can read at once: your instructions, the chat so far, and any documents or tool results. A long job fills that space. Compaction is the fix. The app writes a summary of the older turns and starts a new, shorter window with the summary in their place.

Think of a claims handler who inherits a 300-page file. She doesn't reread all of it each morning. She reads a handover note with the facts, the decisions so far and what is still open, plus the last few emails. That note is the summary. A real AI case works the same way: a motor claim with 300 turns of photos, garage quotes and emails, where the agent keeps going from a note.

- **Context window:** The space the model can see at one time, measured in tokens.
- **Token:** A small piece of text, often part of a word, that the model counts.
- **Summary:** A shorter written version of older turns, kept in place of them.
- **Recent turns:** The last few messages, kept word for word so the latest exchange stays exact.
- **Lossy:** Loses some detail on the way. A summary is lossy by nature.

Anthropic's API can write the summary for you, so your app needs no summary code of its own. Anthropic's version is still in beta. OpenAI also offers compaction, but its item is not meant for people to read. You still decide when it runs and what must survive it.

## How does compaction work, step by step?

1. **Watch the size:** Count the tokens so far. Start the step before the limit, so there is room left for the summary itself.
2. **Write the summary:** A model reads the older turns and writes the goal, the decisions, the IDs and the open questions.
3. **Swap it in:** Put the summary first, keep the last few turns word for word, and drop the old turns.
4. **Carry on:** The agent continues in the new window. When that window fills again, the loop runs again.

*Diagram, What a compaction step keeps and loses: Older turns go to a summary model. The summary and the last few turns form a new window, and the agent carries on from it. A small rule can drop out on the way, so keep rules where the summary cannot touch them.*
1. Older turns go to a model that writes the summary.
2. The summary keeps the goal, decisions and open items.
3. A small rule can drop out, with no warning.
4. The agent carries on from the new window.

The summary is lossy. It keeps the big picture and can drop a small detail that matters later. Here is how the vendors put the idea and its trade-off:

> "Compaction is the practice of taking a conversation nearing the context window limit, summarizing its contents, and reinitiating a new context window with the summary." (Anthropic engineering)
>
> "The art of compaction lies in the selection of what to keep versus what to discard, as overly aggressive compaction can result in the loss of subtle but critical context whose importance only becomes apparent later." (Anthropic engineering)
>
> "Compaction helps you balance quality, cost, and latency as conversations grow." (OpenAI)
>
> "Compaction on demand shows a loop that summarizes the whole conversation while your application waits." (Anthropic Claude docs)

So the trade-off is clear. You buy room and lower cost, and you pay with some detail and a wait while the summary is written. Anthropic also suggests tuning the summary prompt for recall first, so it catches everything that matters, and only then for precision, so it drops the padding.

> **Tip:** Asked how compaction works? Say it is a summary plus a fresh window, then name what it loses. Interviewers listen for the loss.

## What should a summary always keep?

Keep what the agent will still need. Drop what it has already used. Old tool outputs are the easiest to drop. Anthropic calls clearing them one of the safest lightest touch forms of compaction. A good summary keeps these five things:

- **The goal:** In the words the user used.
- **Decisions:** What was decided, and why.
- **IDs:** Claim numbers, file paths and order numbers.
- **Open questions:** What is still waiting.
- **Rules and limits:** These should also live in code.

Here is the claims case in plain Python. The summary function is a stand-in for a summary model. It keeps the amounts and the customer's words, and it drops the policy rule, to show how a summary loses a rule. The limit sits in code, so it survives.

```python
# A made-up claims agent's chat history. The summary is a stand-in for a summary model:
# it keeps the amounts and the customer's words, and drops the policy rule.
KEEP_WORD_FOR_WORD = 2  # made-up: the last two turns stay exactly as they were
LIMIT = 5000  # made-up rule, held in code and never passed through the summary

history = [
    "Customer: my car was hit in a supermarket car park on 3 Nov.",
    "Policy P-88: a payout above 5000 needs a human adjuster.",
    "Photos show a dented rear door, about $2,400 of repairs.",
    "Garage quote: $7,400 for the rear door and a sensor.",
    "Customer: when will the payment reach my account?",
]

def compact(turns, keep):
    older, recent = turns[:-keep], turns[-keep:]
    summary = [t for t in older if "$" in t or t.startswith("Customer")]
    return summary, recent

summary, recent = compact(history, KEEP_WORD_FOR_WORD)
print(len(history), "turns before compaction")
print(len(summary) + len(recent), "items after compaction")
print("Policy rule still in context:", any("adjuster" in t for t in summary + recent))
print("Payout of $7,400 is above the limit:", 7400 > LIMIT)
```

Output:

```text
5 turns before compaction
4 items after compaction
Policy rule still in context: False
Payout of $7,400 is above the limit: True
```

> **Tip:** Keep rules in code, where a summary cannot touch them. A summary is a copy of a copy, and every pass can drop something.

## How is compaction different from cutting old messages?

- **Truncation: Cut the oldest turns.** Drops the oldest messages with no summary. It is cheap and simple, but early rules and decisions vanish without a trace.
- **Compaction: Summarise the older turns.** Keeps the gist and the decisions in a short summary, at the cost of one model call and some lost detail.
- **Notes outside: Write it down, read it back.** The agent saves notes outside the window and reads them later. It is the next question in this topic.

### How would you run an agent that works for days?

Start with the plan, not the summary. The plan holds the goal, the rules, the IDs and the steps done. It lives in a file or a database, and the agent reads it back at each step.

Then add compaction for the chat, trigger it before the limit, and keep the last few turns word for word. Test it with the hard case: plant a key rule early in a long run, then check that it still applies at the end.

> **Tip:** Repeated compaction can drift. A summary of a summary may lose a little more each time, so check the rules in code, not in the text.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a summary replaces older turns, so the agent keeps going in a shorter window.
- [ ] Why it is needed: a long job fills the context window.
- [ ] The trade-off: a summary is lossy, and it costs a model call and a wait.
- [ ] What to keep: the goal, decisions, IDs, open questions and the last few turns.
- [ ] Rules and limits in code, never only in the summary.
- [ ] A test: plant a rule early, then check it at the end of a long run.

**Common traps**

- Trap: "Summaries are free, so compact as often as you can." Better: A summary costs a model call, adds a wait and loses detail. Compact when the room runs low, not on habit.
- Trap: "Compaction is the same as cutting old messages." Better: Cutting drops the oldest turns with no summary. Compaction keeps the gist, at a cost.
- Trap: "The summary keeps every rule, so we can trust it." Better: A summary can drop a small rule. Keep the rules in code, and check the summary before the agent acts on it.

## Can you spot the lost rule?

**Spot the lost rule:** Your claims agent is about to act on this summary. Which line should worry you most?

A. Goal: settle the rear door claim. Open: the photos of the dented door.
B. Goal: settle the claim. Decided: pay the $2,400 repair estimate.
C. Goal: pay the $7,400 quote. Policy limits: none recorded.
D. Goal: confirm claim CL-4471 with the customer by email.

**Answer:** C. The payout limit that needs a human adjuster has gone, so the summary shows no limit at all. Check the rule in code before paying anything above it.

**Quick check:** A chat agent has 180 turns and its window is nearly full. Which step keeps the rules safest?

A. Cut the oldest 150 turns and carry on with the rest
B. Summarise the older turns and keep the limits in code
C. Trust the summary, since the model wrote it and read every turn
D. Turn the temperature down to zero

**Answer:** B. A summary can drop details, so the safe version keeps the recent turns exact and moves the hard limits into code. A stronger summary prompt helps recall, but it cannot promise that every rule survives, so the limit still belongs in code.

## Can you answer these from memory?

**Flash cards**

- **What is compaction?** Replacing older turns with a summary, then carrying on in a shorter context window.
- **Why do long jobs need it?** A long job fills the context window, and the model only sees what fits.
- **What does a summary lose?** Small details. A summary is lossy, so keep rules and limits in code.
- **What stays word for word?** The last few turns, so the latest exchange stays exact.
- **Compaction or truncation?** Compaction keeps a summary. Truncation just cuts the oldest turns.
- **What should a summary keep?** The goal, decisions, IDs, open questions and the limits.

## Likely follow-up questions

- Q097 What is structured note-taking (external memory)? https://designervenkat.online/interview/ai/what-is-structured-note-taking-external-memory
- Q105 How do you budget tokens for chat history? https://designervenkat.online/interview/ai/how-do-you-budget-tokens-for-chat-history
- Q083 What is "context rot"? https://designervenkat.online/interview/ai/what-is-context-rot
- Q106 Just-in-time retrieval vs preloading context? (full answer coming)
- Q095 How do you handle very long documents? https://designervenkat.online/interview/ai/how-do-you-handle-very-long-documents

## Learn it in depth

- [Context Compaction](https://designervenkat.online/learn/ai-engineering/context-compaction)
