Overview

7 Memory

Adding memory gives the LLM agent a way to learn from prior task executions instead of starting every run from scratch. The chapter frames this need with simple examples showing how repeated or related tasks can be handled more efficiently when the agent can recall past results, avoid redundant tool calls, and reuse lessons from previous successes or failures. Because there is no universal standard for memory in LLM systems, the chapter adopts a practical definition centered on records derived from the agent’s own experiences.

The implementation begins by distinguishing working memory from episodic memory. Working memory is already present in the framework as transient step-by-step progress kept within the context window, while episodic memory is long-term and persistent, storing completed task executions across runs. To support this, the chapter introduces an episode as the unit of memory, containing the task, result or error, rollout, metadata, and timestamps, along with formatting options for serializing episodes either as XML or as plain key-value text for prompt use or embedding.

On top of that record structure, the chapter builds a flexible memory system with an abstract store interface and a Qdrant-backed similarity store for semantic recall. A higher-level Memory class coordinates recording and recalling episodes, optionally enriching them with metadata through custom functions before persistence. Finally, memory is wired into the agent lifecycle so recalled episodes are injected into system prompts at the start of task execution and new episodes are recorded at the end. The result is an LLM agent that can reuse relevant experience, improve efficiency, and act more intelligently on repeated or similar tasks.

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.
The UML class diagram of Episode and EpisodeFormatMode
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
The UML class diagram of QdrantMemoryStore which integrates into the qdrant-client Python library
UML class diagram for our new core type, 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 task executions or experiences. These records are recalled later to influence the agent’s behavior and improve future outcomes.
Why do LLM agents need memory?LLM agents need memory because they start each run with a clean context and cannot naturally learn from previous executions. Memory lets them reuse past results, avoid repeated work, and apply lessons from earlier successes or failures.
What is episodic memory?Episodic memory is long-term memory that stores experiences across multiple task executions. In this chapter, each completed task execution is treated as an episode that can later be recalled if it is relevant to a new task.
How is working memory different from episodic memory?Working memory is short-term and only lasts for a single task execution, typically using the model’s context window. Episodic memory is persistent across tasks and stores completed experiences externally so they can be recalled later.
What is an Episode?An Episode is the unit of memory used in this chapter. It represents a completed task execution and stores the task, rollout, result or error, metadata, a unique ID, and a completion timestamp.
Why does the Episode class support multiple formatting modes?The Episode class supports XML and CONCAT formatting so the same record can be serialized for different uses, such as prompt injection or embedding/search. By default, some fields like rollout are excluded from the serialized output.
What is BaseMemoryStore?BaseMemoryStore is an abstract, async-first interface that defines how memory backends should behave. It declares methods for writing, recalling, searching, deleting, updating, counting, and summarizing episodes.
Why use Qdrant as a memory store?Qdrant is used because it supports vector-based similarity search, which is useful for recalling episodes that are semantically related to the current task. It stores episode representations as embeddings and returns the most relevant matches.
What does the Memory class do?The Memory class connects an LLM agent to a backend store and manages recording and recalling episodes. It can also enrich episodes with metadata before writing them to storage.
How is memory wired into the LLM agent’s task execution lifecycle?Memory is recalled at the start of task execution and recorded at the end. The agent loads relevant memories before processing steps, injects them into the system prompt, and then records the completed episode after success or failure.

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


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) ebook for free
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) ebook for free