Sub topic 8
Summarizing Older Turns with a Model
This chapter assumes you can call a chat-style LLM API, know the system, user and assistant roles, and have read the earlier chapters on token budgets and sliding-window truncation. Truncation drops old messages outright. Summarization keeps their substance in fewer tokens by asking a model to rewrite them.
Why summarize instead of drop
A sliding window forgets everything outside it. If the user stated a file path or a constraint 40 turns ago, truncation loses it. A summary condenses those turns into a short note, so the facts that matter can stay in the prompt after the original messages are gone.
The cost is an extra model call and the risk that the summary leaves something out. You trade tokens and latency for memory.
The summarization step
Summarization is an ordinary model call. You give the model the old messages and instructions on what to keep. The result is one short text block that replaces those messages.
Writing the prompt
The prompt decides what survives. A vague request such as "summarize this" tends to produce a fluent paragraph that drops exact values. Be specific about what to keep, what to drop, and how long the result may be.
You maintain a running summary of a conversation between a user and an assistant.
Current summary:
{previous_summary}
New messages to fold in:
{old_messages}
Write an updated summary. Keep:
- the user's goals and any decisions made
- names, numbers, file paths, and other exact values the user gave
- open questions and unfinished tasks
- constraints and preferences the user stated
Drop greetings and repeated content. Do not add facts that are not in the conversation. Keep the summary under 300 words.Each instruction has a purpose:
| Instruction | Purpose |
|---|---|
| Keep exact values | Paths, numbers and names are the first things a model paraphrases away. |
| Keep open tasks | The assistant needs to know what is still unfinished. |
| Do not add facts | Reduces invented details that later turns would treat as true. |
| Word limit | Stops the summary from growing until it costs as much as the history it replaced. |
Formatting the old messages
Before filling the template, turn the message list into plain text with a role label on each line. This keeps the speaker clear.
def format_messages(messages):
return "\n".join(f"{m['role']}: {m['content']}" for m in messages)
def build_summary_prompt(template, previous_summary, old_messages):
return template.format(
previous_summary=previous_summary or "(none yet)",
old_messages=format_messages(old_messages),
)
template = (
"Current summary:\n{previous_summary}\n\n"
"New messages to fold in:\n{old_messages}\n\n"
"Write an updated summary under 300 words."
)
old = [
{"role": "user", "content": "Deploy to /srv/app, not /opt/app."},
{"role": "assistant", "content": "Understood, using /srv/app."},
]
print(build_summary_prompt(template, "", old))The (none yet) placeholder keeps the first call from showing an empty section, which can confuse a model.
Practical settings
- Use a low temperature, such as 0 to 0.3, so the summary stays close to the source.
- A smaller, cheaper model often does well enough, but test it against your own conversations.
- Summarize only the messages being removed. Do not re-summarize the recent window you plan to keep verbatim.
- Store the summary as its own value, separate from the message list, so you can inspect and test it.
Common errors
- Summarizing the summary badly. Passing only the new messages and not the previous summary erases earlier facts. Always include both.
- No length cap. The summary grows each round and eventually crowds out the recent messages.
- Wrong role. Putting the summary in an assistant message makes the model treat it as something it said. A labeled system message is clearer.
- Summarizing in the middle of a tool call. Keep a tool call and its result together in the same batch.
Exercise
Your summarizer is called with only the newest batch of old messages, not the previous summary. After three compactions the assistant has forgotten the project name given in turn 2. What went wrong, and how do you fix it?
Show answer
Each call produced a summary of only its own batch, and the new summary replaced the old one, so facts from earlier batches were lost. Pass the previous summary into every call so the model folds the new messages into it, as the {previous_summary} slot in the prompt does.
Exercise
Why does the prompt include "Keep the summary under 300 words"?
Show answer
Without a cap the summary grows with each compaction. Eventually it uses a large share of the input budget and leaves less room for recent messages, which defeats the purpose of compacting.