Overview

7 Memory

In this chapter, the framework is extended with memory so LLM agents can benefit from past task executions instead of starting over with a blank slate each time. The central idea is episodic memory: storing completed experiences, then recalling relevant ones later to improve efficiency and accuracy. This is framed as a practical response to the fact that LLMs are stateless and need memory injected through text inputs if they are to learn from prior runs.

The chapter defines memory narrowly as records derived from an agent’s own experiences, distinguishing it from broader notions such as user preferences, general knowledge, RAG, skills, or model weights. It separates short-term working memory from long-term episodic memory, with working memory already represented by the rollout carried across steps inside a single task, and episodic memory stored persistently across tasks. To support this, the chapter introduces an Episode object as the unit of memory, a BaseMemoryStore interface for persistence and recall, and a concrete vector-backed store using Qdrant for similarity search.

Finally, the chapter wraps memory into a new Memory core type and wires recall and record operations into the agent lifecycle. Memory can enrich episodes with custom metadata before storage, and recipes are provided for common patterns such as recency-based, similarity-based, and reflective memory. The LLMAgent and TaskHandler are then updated so agents load recalled memories at the start of execution and save new episodes at the end, enabling both individual and shared memory designs in multi-agent systems. The result is an agent framework that can reuse prior experience, avoid redundant work, and produce better outcomes over time.

We’ll now give our LLM agents memory so that they can learn from past experiences to perform future tasks more effectively and efficiently.
We’ll now give our LLM agents memory so that they can learn from past experiences to perform future tasks more effectively and efficiently.
Memory records and recalls Episode objects through its BaseMemoryStore backend. LLM agents use these operations within its task execution lifecycle.
Memory records and recalls Episode objects through its BaseMemoryStore backend. LLM agents use these operations within its task execution lifecycle.
The UML class diagram of Episode and EpisodeFormatMode
The UML class diagram of Episode and EpisodeFormatMode
UML class diagram of BaseMemoryStore, our new standard interface for memory data backend
UML class diagram of BaseMemoryStore, our new standard interface for memory data backend
Recording an Episode as a point in the embedding space and recalling the most similar Episode objects to a given query
Recording an Episode as a point in the embedding space and recalling the most similar Episode objects to a given query
The UML class diagram of QdrantMemoryStore which integrates into the qdrant-client Python library
The UML class diagram of QdrantMemoryStore which integrates into the qdrant-client Python library
UML class diagram for our new core type, Memory
UML class diagram for our new core type, Memory
The necessary updates to LLMAgent and TaskHandler to support episodic memory
The necessary updates to LLMAgent and TaskHandler to support episodic memory

Summary

  • LLM agents need memory of past experiences to perform future tasks more efficiently and accurately
  • LLMs are stateless which means memories need to be provided at inference time within prompts or in the LLM’s context window
  • Working memory is the short-term memory that is used to maintain the task execution state, which we already implemented in Chapter 4
  • Episodic memory is a form of long-term memory that records an LLM agent’s task execution and its result
  • The choice of recall method and memory storage are tightly coupled (e.g. recalling with semantic similarity requires a vector-based storage)
  • Deciding what gets stored into memory is an important design decision
  • Episodes are naturally recalled at the start of a task execution and recorded at the end
  • Episode is our new unit of memory
  • Memory is our new core type responsible for recalling and recording Episode objects into its backend BaseMemoryStore

FAQ

What is memory in the context of LLM agents?Memory is a collection of purposefully constructed records from an LLM agent’s past experiences or task executions. These records are recalled later to influence the agent’s behavior and help it achieve better outcomes on future tasks.
Why do LLM agents need memory?LLM agents need memory because they start each run with a clean context and no awareness of prior executions. Memory lets them learn from past tasks, avoid repeating work, improve efficiency, and apply lessons from previous successes or failures.
What is episodic memory?Episodic memory is long-term memory built from completed task executions, called episodes. It stores experiences across multiple tasks so the agent can recall relevant past episodes and use them when handling a current task.
How is working memory different from episodic memory?Working memory is short-term and only stores progress within a single task execution, usually inside the model’s context window. Episodic memory is persistent and stores experiences across tasks in an external data store for later recall.
What is an Episode object?An Episode is the unit of episodic memory. It represents a completed task execution and typically stores the task, the result or error, the rollout, metadata, a unique ID, and a completion timestamp.
Why does Episode support multiple formatting modes?Episode supports multiple formatting modes so the same record can be serialized differently depending on the use case. For example, XML mode is useful for prompt injection, while CONCAT mode is useful for embedding or more compact text representation.
What is BaseMemoryStore used for?BaseMemoryStore is the abstract backend interface for memory storage. It defines standard methods such as write, recall, delete, update, count, and summary, allowing different storage implementations to plug into the same Memory API.
How does QdrantMemoryStore retrieve relevant episodes?QdrantMemoryStore uses semantic similarity search. It converts an Episode into an embedding, stores it in Qdrant, and later compares a query embedding against stored episodes to retrieve the most similar ones.
What does the Memory class do?The Memory class owns a memory store and a set of metadata functions. It records episodes into the backend after enriching them with metadata, and it recalls relevant episodes for a task and formats them for injection into the LLM agent’s prompt.
How is memory wired into the LLMAgent execution lifecycle?Memory is loaded at the start of task execution and recorded at the end. The agent recalls episodes before processing begins, injects them into the system prompt, and then records a new episode after the task succeeds or fails.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Multi-Agent System (from Scratch) Build a Multi-Agent System (from Scratch) ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Multi-Agent System (from Scratch) Build a Multi-Agent System (from Scratch) ebook for free