Chapter 9: Context Compression
Lecture
Chapter principle
Key takeaways
- Compression is a lossy transformation; evaluate it by downstream utility.
- Critical constraints, permissions, provenance, and uncertainty may need explicit preservation.
- Shorter context is not automatically better context.
- Compression and memory persistence solve different problems.
- Consolidation creates reusable abstractions from multiple experiences.
- Recoverable compression can preserve access to original evidence.
Experiments
Every experiment in the book is small, runnable and printed with its real output. In the Lab you can replay a recorded real run of each one, with no programming, and watch what the model and the agent do.
Experiment 19: Shrinking the context
What can be left out when the context is shortened?
A long conversation about writing a report. In the fifth message, the user says that sending anything by email needs their approval first. The report is now finished, and the model must take the next step: send the email, or ask for approval. It sees either the full conversation or a sliding window that keeps only the last six messages. Each version is tried three times. This is a recorded run of a real model. A new run may differ.
Experiment 20: Turning episodes into facts
When is it safe to turn past events into a lasting fact?
Three sets of past episodes: consistent (the user asked for concise answers three times), conflicting (concise once, detailed once), and a single episode. Three consolidators each turn a set into one lasting fact. The plain one just asks the model for a fact. The guarded one adds a rule: reply NONE if fewer than two episodes support it, or if they conflict. The checked one counts the episodes in code and asks the guarded model only when there are enough. This is a recorded run of a real model. Its wording will differ in a new run.
All the code for this chapter: github.com/agentic-engineering-book/code
Download the bookBrowse all chapters