Skip to content

< CurriculumContext engineering · 21 of 46 ·69 · Level 01, LLM APIs

Compact a Long Conversation Without Losing the Decision

medium · implement · LLM Fundamentals

A long conversation planned a team offsite: date, venue, budget and caterer were settled early, then the talk moved on to icebreakers. The history is now too long to send, and dropping the oldest turns drops every decision. Compaction instead replaces the old turns with a short summary of what they established and keeps the recent turns exactly as they were.

Implement compact(llm, messages, keep_last=4), returning a new list of messages.

  1. If the conversation has keep_last messages or fewer, return it unchanged and make no model call.
  2. Otherwise the recent part is the last keep_last messages and the old part is everything before.
  3. Ask the model, in one call, to summarise the old part. The prompt is the exercise: they discussed plans for an offsite is short and useless. Decisions, names, numbers and dates are what later questions will be about.
  4. Return a "user" message containing the summary, then an "assistant" message acknowledging it, then the recent messages, untouched and in order. The two extra messages keep the roles alternating.

The result must be smaller than what you started with (the tests count tokens), and you must not change the list you were given.

A real model writes the summary and is then asked questions over the compacted conversation, so the tests check properties: the recent turns are intact, one call was made, and the date, venue, budget and caterer can still be answered.