esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

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.

11 min readIntermediateCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

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.

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.

Key words

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. 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. 2

    Write the summary

    A model reads the older turns and writes the goal, the decisions, the IDs and the open questions.

  3. 3

    Swap it in

    Put the summary first, keep the last few turns word for word, and drop the old turns.

  4. 4

    Carry on

    The agent continues in the new window. When that window fills again, the loop runs again.

What a compaction step keeps and loses

Older turns

the claim file and every step so far

Summary model

writes a shorter summary

New window

The summary

goals, decisions, open items

Kept turns

last few turns, kept as they were

Lost in the summary

Payout limit rule

the $5,000 adjuster rule, dropped

Agent carries on

keeps going on summary and kept turns

  1. 1Older turns go to a model that writes the summary.
  2. 2The summary keeps the goal, decisions and open items.
  3. 3A small rule can drop out, with no warning.
  4. 4The agent carries on from the new window.
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.

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

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."

    BetterA 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."

    BetterCutting 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."

    BetterA 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?

Quick check

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

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q097What is structured note-taking (external memory)?12 min
  2. Q105How do you budget tokens for chat history?10 min
  3. Q083What is "context rot"?10 min
  4. Q106Just-in-time retrieval vs preloading context?Coming soon
  5. Q095How do you handle very long documents?12 min

Learn it in depth

Last updated on 26 November 2026

Building an agent that runs for days?

Venkat designs and builds AI products and agents, including the UI and the system behind them. Book a short call and say what you need.