chapter six
6 Adding memory to your agent
This chapter covers
- Understanding the role of memory in LLM agents
- Managing context growth with sliding window, compaction, and summarization
- Implementing sessions for multiturn conversations
- Building asynchronous human-in-the-loop workflows
- Creating long-term memory for cross-session knowledge retention
Memory is what separates a stateless tool from an intelligent assistant. Without memory, an agent can’t recall previous events within the same task, continue conversations from earlier sessions, or learn from experiences. Each interaction starts from scratch, forcing users to repeat context and preventing the agent from improving over time.
This chapter (figure 6.1) addresses memory in three use patterns:
- We implement context optimization strategies to prevent context explosion, in which the context takes up a large share of the model’s context window or grows past it during complex problem solving.
- We build
SessionandSessionManagerto maintain conversation continuity across multiple interactions, extending this architecture to support asynchronous human-in-the-loop (HITL) workflows. - We create a long-term memory system that extracts, stores, and retrieves knowledge across session boundaries using vector search.
Figure 6.1 Book structure overview: chapter 6