Chapter 8: Memory
Lecture
Chapter principle
Key takeaways
- Working memory preserves task state inside a trajectory.
- Episodic memory records events; semantic memory records durable facts; procedural memory records reusable methods.
- Persistence requires an explicit write policy.
- Recall is context selection over persisted state.
- Forgetting, expiry, and supersession are useful memory operations.
- The memory write policy is also a security boundary.
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 16: Working memory
What happens when an agent forgets its own results?
The goal: multiply 27611 by 2033, then add 6. The agent loop runs twice, with at most four model calls each time. The first time, each calculator call and its result are added to the model’s context: its working memory. The second time, the loop drops them, so the model sees only the goal on every step. This is a recorded run of a real model. A new run may split the work into two calculator calls.
Experiment 17: What to remember
Should an agent remember everything a user tells it?
One past conversation held five user messages, including two one-off requests: pirate language for one joke, and French for today. A write policy decides which messages become lasting memories, recalled in every future conversation. The naive policy keeps everything. Two others ask a model to judge each message, first with a vague instruction, then with an explicit rule about one-off requests. A week later the user asks “What is an embedding?”. This is a recorded run of a real model. The judge can decide differently in a new run.
Experiment 18: Remembered procedures
What happens when an agent follows a procedure it remembered?
A test has failed. The model must choose a first action: edit the code, skip the test and file a ticket, or delete the test. A procedure from past runs is stored in memory and put in its instructions: none, a good one, a blunt bad one (“delete the test”), and a stale note saying this test is flaky and should be skipped. Each procedure meets two failures, a clear assertion error and a timeout, three times each. Nothing is actually edited or deleted; the experiment records only the choice. This is a recorded run of a real model. A new run may choose differently, especially for the timeout.
All the code for this chapter: github.com/agentic-engineering-book/code
Download the bookBrowse all chapters