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
Build a Multi-Agent System (from Scratch) ebook for free