Chapter 7: Memory
This introduction explains why agents need memory and how episodic memory can help them use past task executions.
After this chapter you will be able to
- Episodic memory for task experience. Explain why an agent needs memory across runs and how episodic memory helps with future tasks.
- Reuse of Past Results. Explain how memory could make the Hailstone agent more efficient when a later task overlaps an earlier one.
- Learning Through Supplied Memory. Explain why memory must be supplied through text inputs and how experience can improve later results.
- Experience Based Memory Boundary. Explain the book’s definition of memory and distinguish it from broader uses of the term.
- Working and Episodic Memory. Distinguish working memory from episodic memory in scope, persistence, and purpose.
- Storage and Recall Compatibility. Explain why memory storage and recall methodology must be chosen together, using the section’s examples.
- Episode memory record. Explain what an Episode records and how it can be formatted for storage or presentation to an LLM agent.
- Episode execution record. Describe the information an Episode retains about a task execution and why it can help with future tasks.
- Episode serialization modes. Explain how format() serializes an Episode using XML or CONCAT and how fields can be excluded.
- Persistent episode storage. Explain the role of memory stores in making episodes available to an agent across task executions.
- Common memory store interface. Explain the role of BaseMemoryStore and why it provides a common interface across storage backends.
- Recall modes and limits. Describe what max_results and recall_mode specify, and distinguish RECENT from SEARCH.
- Recall limit validation. Explain how the concrete recall() method checks the result limit and why that check is needed.
- Semantic Episode Similarity. Explain how similarity search uses embeddings to find past episodes relevant to a query.
- Writing Episodes as Points. Trace how write() converts an Episode into a Qdrant point and stores it.
- Query Search and Reconstruction. Trace how _search() finds matching episodes and explain why it uses the same embedding model as write().
- Episode recording and enrichment. Explain how Memory enriches and records an Episode, and why it must leave the original Episode unchanged.
- Recall delegation and formatting. Trace how Memory recalls episodes from its backend and prepares the result for an LLM agent.
- Basic memory recipe designs. Describe memory recipes and distinguish the two basic designs by their recall methods and use of metadata functions.
- Reflective episode enrichment. Explain how reflective memory uses an LLM to enrich an episode before storage and how its reflection prompt can be configured.
- Memory operations in execution. Explain when recall() and record() occur during task execution and why a new Episode is recorded.
- Memory Operations in Execution. Trace how LLMAgent and TaskHandler recall and record memories during task execution, including how individual memory errors are handled.
- Recalled Experience Prompt Injection. Explain how TaskHandler makes recalled episodes available to the LLM during a task step.
- Individual Subagent Memory. Explain how subagents can use individualized memory even when their working memory is discarded by a coordinator.
- Shared Memory and Provenance. Describe how agents can benefit from shared experiences and how the recording agent can be identified.
- Shared Memory Write Safety. Explain why shared-memory writes need care and describe the safeguards proposed in the section.
What it builds on
- Understanding skills (chapter 6) · Skills as instructions for performing tasks, which some broader definitions call procedural memory.
- Implementing LLMAgent: Part 2 (chapter 4) · The TaskHandler and its step-by-step task execution.
- Implementing Task and TaskResult (chapter 4) · The Task and TaskResult types represented in an episode.
- Implementing a processing loop for LLMAgent (chapter 4) · TaskHandler and task-step execution
- Implementing Skill Activation (chapter 6) · Skill activation
Section by section
Introduction
This introduction explains why agents need memory and how episodic memory can help them use past task executions.
- Episodic memory for task experience: Explain why an agent needs memory across runs and how episodic memory helps with future tasks.
Why it matters: Memory lets an agent benefit from experience instead of treating every run as its first. - Memory protocol and taxonomy: Describe the lack of a standard memory protocol and the variation in what frameworks call memory.
Why it matters: The term memory can refer to different things across agent frameworks.
Key terms: episodic memory, recall, completed task executions, memory, protocol, taxonomies
7.1 Introducing memory
7.1.1 Why LLM agents need memory
This section explains why agents need memory to reuse past results and learn from experience despite stateless LLMs.
- Reuse of Past Results: Explain how memory could make the Hailstone agent more efficient when a later task overlaps an earlier one.
Why it matters: Reusing an earlier result can prevent the agent from repeating work. - Learning Through Supplied Memory: Explain why memory must be supplied through text inputs and how experience can improve later results.
Why it matters: Past mistakes can help an agent perform similar future tasks more accurately.
Key terms: clean context, memory, Hailstone, stateless, text inputs
7.1.2 What memory is (and isn’t)
Defines the book’s experience-based notion of agent memory and distinguishes working memory, episodic memory, and their storage and recall needs.
- Experience Based Memory Boundary: Explain the book’s definition of memory and distinguish it from broader uses of the term.
Why it matters: This boundary clarifies what the book treats as memory and why an agent recalls it. - Working and Episodic Memory: Distinguish working memory from episodic memory in scope, persistence, and purpose.
Why it matters: The distinction shows why progress within a task is not enough to retain lessons across tasks. - TaskHandler Rollout as Memory: Describe how the existing TaskHandler rollout provides working memory between task steps.
Why it matters: This connects the general idea of working memory to the agent framework already built. - Storage and Recall Compatibility: Explain why memory storage and recall methodology must be chosen together, using the section’s examples.
Why it matters: Choosing storage without considering how the agent will retrieve memories can restrict the recall it needs.
Key terms: memory, experiences, past task executions, procedural memory, working memory, short-term memory, episodic memory, long-term memory, episode, context window
7.2 Episode: the unit of memory
This section introduces Episode as a record of a completed task and its result, with formatting for storage and presentation to an agent.
- Episode memory record: Explain what an Episode records and how it can be formatted for storage or presentation to an LLM agent.
Why it matters: Episodes provide the records an agent can store and later present as memories.
Key terms: Episode, EpisodeFormatMode, long-term memory
7.2.1 Implementing Episode
This section implements Episode records and describes how they are serialized for use as memory.
- Episode execution record: Describe the information an Episode retains about a task execution and why it can help with future tasks.
Why it matters: An episode preserves both what happened and how the task was performed for future use. - Episode serialization modes: Explain how format() serializes an Episode using XML or CONCAT and how fields can be excluded.
Why it matters: Serialization makes an episode available as text while allowing control over which fields appear. - Formatting delegation and string conversion: Describe how format() delegates to formatting helpers and how Episode string conversion chooses a mode.
Why it matters: This explains how the selected format is applied and what ordinary string conversion produces.
Key terms: Episode, Task, TaskResult, Exception, rollout, metadata, format(), EpisodeFormatMode, XML, CONCAT
7.3 Memory Stores
This section introduces memory stores as a way to persist episodes for use in future task executions.
- Persistent episode storage: Explain the role of memory stores in making episodes available to an agent across task executions.
Why it matters: Persisting episodes gives an agent a way to use past experiences in future tasks.
Key terms: memory stores, Episode, persist
7.3.1 Defining BaseMemoryStore
This section defines the common interface, recall settings, and result-limit validation for memory storage backends.
- Common memory store interface: Explain the role of BaseMemoryStore and why it provides a common interface across storage backends.
Why it matters: A common interface lets Memory work with different kinds of storage backends. - Recall modes and limits: Describe what max_results and recall_mode specify, and distinguish RECENT from SEARCH.
Why it matters: These settings determine how a store recalls episodes and how many records it should return. - Recall limit validation: Explain how the concrete recall() method checks the result limit and why that check is needed.
Why it matters: The check makes an over-limit result visible even when a subclass does not respect the limit.
Key terms: BaseMemoryStore, Memory, memory store, async-first, abstract class, max_results, recall_mode, RecallMode, RECENT, SEARCH
7.3.2 QdrantMemoryStore
This section explains how QdrantMemoryStore embeds, stores, and retrieves episodes by semantic similarity.
- Semantic Episode Similarity: Explain how similarity search uses embeddings to find past episodes relevant to a query.
Why it matters: This explains why the store can recall episodes relevant to a new task. - Store Configuration and Checks: Describe the main configuration choices and checks that support QdrantMemoryStore.
Why it matters: These choices determine where episodes go and what text represents them for similarity search. - Writing Episodes as Points: Trace how write() converts an Episode into a Qdrant point and stores it.
Why it matters: The stored point must support both similarity search and recovery of the original episode. - Query Search and Reconstruction: Trace how _search() finds matching episodes and explain why it uses the same embedding model as write().
Why it matters: Consistent embeddings and retained payloads make relevant episodes both searchable and recoverable.
Key terms: similarity search, semantic search, embedding, embedding space, cosine similarity, _client, _collection_name, _key_fn, _collection_ready, _ensure_collection()
7.4 Memory: a core type for episodic memory
7.4.1 Implementing Memory
This section implements Memory as a store-backed interface for recording enriched episodes and recalling them in a form suitable for an LLM agent.
- Episode recording and enrichment: Explain how Memory enriches and records an Episode, and why it must leave the original Episode unchanged.
Why it matters: This lets each memory store its own episode enrichment without contaminating other memories. - Recall delegation and formatting: Trace how Memory recalls episodes from its backend and prepares the result for an LLM agent.
Why it matters: This separates the store's retrieval responsibility from preparing recalled episodes for the agent. - Store configuration and delegation: Describe what the Memory constructor stores and how delete() and update() use the backend.
Why it matters: It clarifies which operations Memory handles itself and which it passes to its store.
Key terms: Episode, record(), metadata_fns, metadata, recall(), BaseMemoryStore, Memory, delete(), update()
7.4.2 Recipes for memory
This section introduces preconfigured memory recipes, including basic recency and similarity designs and a reflective design that enriches episodes with lessons.
- Basic memory recipe designs: Describe memory recipes and distinguish the two basic designs by their recall methods and use of metadata functions.
Why it matters: These recipes provide starting points for common memory designs. - Reflective episode enrichment: Explain how reflective memory uses an LLM to enrich an episode before storage and how its reflection prompt can be configured.
Why it matters: Lessons from earlier experiences may help an agent perform similar tasks more effectively. - Local JSON memory store: Describe what JSONMemoryStore stores on disk and the recall mode it is designed to support.
Why it matters: The store is a simple local option for a recency-based memory design.
Key terms: recipes, Memory, recency, similarity, metadata functions, reflective Memory, Episode, lesson, metadata_fns, template
7.5 Wiring Memory into LLMAgent
7.5.1 Wiring memory into task execution
This section places memory recall and recording at the start and end of task execution.
- Memory operations in execution: Explain when recall() and record() occur during task execution and why a new Episode is recorded.
Why it matters: This timing connects a task to past experience and preserves its own experience for later tasks.
Key terms: recall(), record(), Episode, task execution
7.5.2 Updating the LLMAgent and TaskHandler classes
This section wires episodic memory into agent task execution, demonstrates its reuse, and introduces knowledge graphs and compression as further memory concepts.
- Memory Operations in Execution: Trace how LLMAgent and TaskHandler recall and record memories during task execution, including how individual memory errors are handled.
Why it matters: This connects stored experiences to the agent’s task lifecycle without letting one memory failure stop execution. - Recalled Experience Prompt Injection: Explain how TaskHandler makes recalled episodes available to the LLM during a task step.
Why it matters: Recall only helps the agent if the experiences are made visible to the model during execution. - Similarity Memory Demonstration: Describe what the repeated stop-at-one task demonstrates about using a recalled episode.
Why it matters: The example shows how a relevant past experience can avoid repeating work. - Knowledge Graph Memory Stores: Explain how a knowledge graph can represent episode data and support questions about relationships among past experiences.
Why it matters: A graph offers a relationship-oriented way to explore stored experiences. - Context Window Compression: Explain why growing task rollouts may require compression, with summarization as an example.
Why it matters: Managing the size of accumulated context helps keep it within the model’s usable window.
Key terms: LLMAgent, TaskHandler, memories, Memory, load_memories(), record_memory(), _recalled_memories, _format_memories_for_system_prompt(), run_step(), system message
7.6 Extending to MAS: Memory
This section explains individualized and shared memory in multi-agent systems, including subagent memory benefits and safe shared-memory management.
- Individual Subagent Memory: Explain how subagents can use individualized memory even when their working memory is discarded by a coordinator.
Why it matters: A subagent can retain useful experience without passing its entire working history to the coordinator. - Shared Memory and Provenance: Describe how agents can benefit from shared experiences and how the recording agent can be identified.
Why it matters: Sharing experiences can improve coordination, while provenance makes it clear which agent recorded an Episode. - Shared Memory Write Safety: Explain why shared-memory writes need care and describe the safeguards proposed in the section.
Why it matters: Careless shared writes can silently erase experiences agents need later.
Key terms: individualized memory, working memory, rollout, long-term memory, coordinator, shared memory, Memory, memory store, metadata function, Episode
7.7 Summary
Where this is used later
- Defining default subagents (chapter 9) · Memory recipes provide the earlier analogy for reusable recipes.
- Updating TaskHandler and LLMAgent (chapter 8) · Recording task execution in memory
- Pattern 3: Supervised-Execution HITL (chapter 8) · Episodic memory in task execution.
- Implementing SupervisedTaskHandler (chapter 8) · Loading and recording episodic memory during task execution.
Concepts explained in this chapter
- memoryMemory is a collection of records from an agent's experiences that can influence its future behavior.
- EpisodeEpisode is a book-specific record of an agent task execution containing its task, result or exception, rollout, metadata, identifier, and completion timestamp.
- BaseMemoryStoreBaseMemoryStore is a book-specific abstract base class defining an asynchronous interface for persisting, retrieving, updating, and deleting memory records.
- working memoryWorking memory is transient memory that retains an agent's progress during its current task execution.
- episodic memoryEpisodic memory retains discrete experiences from an agent's task executions.
- embeddingAn embedding is a numerical vector representation of an item that encodes properties or relationships relevant to machine learning.
- QdrantQdrant is an open-source vector database and similarity-search engine.
- QdrantMemoryStoreQdrantMemoryStore is a book-specific memory-store backend that indexes and retrieves episodes using Qdrant.
- similarity searchSimilarity search retrieves items according to their similarity to a query, often using vector representations.
- LLMAgentLLMAgent is the framework class that implements an LLM agent and its task-execution orchestration.
- TaskHandlerTaskHandler is a framework helper class that orchestrates task execution across the processing loop and resolves to a TaskResult.
- multi-agent systemA multi-agent system is a system composed of multiple interacting agents that cooperate, coordinate, or compete to achieve goals.
- vector databaseA vector database stores and indexes vector embeddings to support similarity-based retrieval.
- recall methodA recall method is a procedure for selecting stored memories to retrieve for a task.
- memory recordA memory record is a purposefully constructed stored record derived from an LLM agent's experience or past task execution.
- LLMAgentBuilderLLMAgentBuilder is a book-specific framework class that discovers tools from selected MCP servers and constructs an LLMAgent with them.
- run()run() is an LLMAgent method that accepts a user task, initiates its processing loop, and returns an awaitable that resolves to a TaskResult.
- run_step()run_step() is an asynchronous TaskHandler method that executes one TaskStep and returns its TaskStepResult.
- coordinator agentA coordinator LLM agent manages subagents or shared resources in a multi-agent system.
- templateA template is a modifiable text structure containing placeholders that can be filled with values before an LLM interaction.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
