Lesson 14 / 25
Summarising Old Turns
Replace old messages with a faithful summary and keep the task, decisions and open items.
Compress, do not erase
When history passes your trigger, ask the model (or a cheaper one) to summarise older turns into a short note, then replace those messages with it and keep the most recent turns verbatim. A good summary preserves the goal, decisions made, files changed and what is still to do. Facts the summary drops are gone, so keep exact values (IDs, paths) in it.
Summarise, keep the tail
summarise is another model call. Never summarise away the first user message, because it holds the original task.
def compact(messages, keep_last=6):
if len(messages) <= keep_last + 1:
return messages
head, old, tail = messages[0], messages[1:-keep_last], messages[-keep_last:]
note = summarise(old) # goal, decisions, open items, exact IDs
return [head,
{"role": "user", "content": f"Summary of earlier work:\n{note}"},
*tail]Check the summary
Summaries can invent or drop details. For important runs, log the original messages somewhere before replacing them so you can audit what was lost.
Quick check: What should never be summarised away?
- The original task
- Greetings
- Old error logs
- Whitespace
Answer
The original task — Losing the original goal makes the rest of the work aimless.