Agentic Engineering from First Principles All chaptersLecturesLabDownload the book
Part II · Giving Agents Abilities

Chapter 9: Context Compression

Central questionHow do we make growing context smaller, without removing information that future decisions still need?

Lecture

Chapter principle

Compression is successful when it reduces representation cost, while preserving the information required for future decisions. The diagram shows the whole lifecycle. Raw interaction becomes working context. Working context is compressed into compact working state. Selected items persist into memory. And memory is consolidated into durable facts or skills.

Key takeaways

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

This is Chapter 9 of Agentic Engineering from First Principles: thirty chapters, a case study and fifty-nine executable experiments, from a single model call to controlled, self-improving agents.
Download the bookBrowse all chapters
← Chapter 8: MemoryChapter 10: Model Context Protocol (MCP) →