Module 7, Lecture 7.3 | Implementing Context Management
Sliding window and selective preservation are subsetting strategies: they keep some messages and drop the rest, and both fail once the set of messages worth keeping outgrows the window. Compaction replaces selection with synthesis — the LLM reads the older part of the conversation and writes a single structured summary that stands in for it. The lecture covers when to trigger it (70–85% of the model's window, measured from response.usage.input_tokens, never mid-tool-call, because the compaction call needs headroom and output quality degrades well before the limit), why the compaction prompt is the real engineering artifact (a preserve list, a discard list, and a strict format, each rule tied to a failure mode), and how to wire it into the loop at the same hook point as truncation. The hybrid pattern keeps the last K messages verbatim and summarizes only the older slice, reusing safe_split_index and ensure_alternating_roles from 7.1; the summary call runs on Haiku, which is cheap and fast enough to compact often. Placing the summary at the front makes it self-reinforcing — each new compaction re-summarizes the old summary plus newer work, so it keeps favoring what is current. The lecture closes with the edge cases (oversized summaries, lost details, compaction loops), the cost case for compaction winning on money, speed, and quality at once, and a comparison of all three Module 7 strategies.
Read the full lecture narrative
/compact with focus instructions changes, and what survives a summary.