Sub topic 10
Compacting and summarizing long sessions
This chapter assumes you know what a token budget is and how conversation history grows with each turn. You should also be able to read short Python functions.
Why trimming is not enough
Dropping the oldest messages is the simplest way to manage history. It is also lossy. If the user stated a constraint in turn 2, such as "use Python 3.9 only", trimming deletes it. The model then breaks the constraint without any sign that something is missing.
Compaction replaces older messages with a short summary and keeps recent messages word for word. The context shrinks, but the important facts stay.
A minimal compaction function
The function below keeps the last keep_last messages. It passes everything before them to a summarize function you provide. In practice, summarize is usually another model call.
def compact(history, keep_last, summarize):
# history is a list of message strings, oldest first.
# summarize takes a list of messages and returns a short string.
split = max(0, len(history) - keep_last)
older, recent = history[:split], history[split:]
if not older:
return history
summary = summarize(older)
return ["Summary of earlier conversation: " + summary] + recent
def fake_summarize(messages):
return f"{len(messages)} earlier messages about setting up a project."
history = [f"message {i}" for i in range(1, 9)]
print(compact(history, keep_last=3, summarize=fake_summarize))This prints the summary line followed by message 6, message 7 and message 8. The five older messages become one.
When to compact
Do not compact on every turn. Each summary costs a model call, and repeated summarizing loses detail. Two common triggers are:
- The history passes a fraction of its budget, for example 70% of the tokens you allotted to history.
- A natural task boundary, such as finishing a feature or switching to a new question.
Compacting at a boundary is often better than compacting at a size threshold. A size threshold can cut a task in half, so the summary is written while the work is still unfinished.
What a good summary keeps
A summary prompt that says only "summarize this" tends to produce vague prose. Ask for the things the next step needs:
- Decisions made and the reasons for them.
- Constraints and user preferences that still apply.
- Open problems and unfinished steps.
- Exact identifiers, such as file names, function names, IDs and error messages.
Tell the summarizer to drop raw tool output, repeated attempts and chit-chat. Large tool results are usually the best thing to remove first.
Common errors
| Error | Result | Fix |
|---|---|---|
| Summarizing the most recent messages | The model loses the exact wording of the current task | Keep the last few turns verbatim |
| Summarizing a summary again and again | Detail fades with each pass | Fold the old summary and new messages into one fresh summary, and keep key facts in a separate notes list |
| Dropping identifiers | The model invents file names or IDs | Require exact identifiers in the summary prompt |
| Compacting too late | The window is full before the summary call can run | Compact at a threshold below the limit, and leave room for the summary request |
Keep facts that must never be lost, such as the user's hard requirements, out of the summarized text. Store them in a pinned section of the prompt or in external memory, which the next chapter covers.
Exercise
A coding session has 40 messages. You call compact(history, keep_last=6, summarize=s). How many messages does the new history contain, and what is the first one?
Show answer
It contains 7 messages. The first is the summary message beginning "Summary of earlier conversation: ", followed by the 6 most recent messages unchanged.
Exercise
Why is compacting at a task boundary often better than compacting when the history reaches a size limit?
Show answer
A size limit can fall in the middle of a task, so the summary is written while the work is unfinished and may lose details the next step needs. At a task boundary the outcome is settled, so the summary can record what was decided and what remains.