Skip to content
Back to log

Agent memory types: Short, long and episodic

Chainabit's Chao · · 7 min read

In short

Short-term memory holds the state of a conversation, while long-term memory is available across sessions. Episodic memory is a type of memory that stores past experiences.

Two distinct distinctions are used to define an agent's memory: which interactions can access the information and what type of information the memory holds. LangChain explains the first distinction in terms of short-term and long-term memory. CoALA divides long-term memory into episodic, semantic and procedural memory. In this classification, episodic memory is not an alternative duration category to long-term memory; it is a type of memory that holds past experiences.

Short-term memory: The state of a conversation

According to LangChain, short-term memory enables an agent to remember previous interactions within a thread or conversation. A thread organizes multiple interactions in a session together. This scope describes which conversation the memory is associated with.

LangGraph manages short-term memory as part of the agent's state. State is stored in thread-scoped checkpoints. A checkpointer saves state to a database, allowing the thread to be resumed later. Short-term memory is updated when the graph is invoked or a step is completed. State is read at the start of each step.

State does not have to consist only of message history. Alongside conversation history, LangChain gives uploaded files and generated outputs as examples of data that can be held in state. When this information is in graph state, the application can access the context of the relevant conversation while keeping different threads separate.

In the chat applications described by LangChain, messages alternate between user inputs and model responses. As these interactions continue, the message list grows. The context given to the chat model can also include a system message supplied by the developer and a human message sent by the user.

The concept of working memory in CoALA also focuses on information used in the current decision cycle. This memory can contain perceptual inputs, active information generated through reasoning or retrieved from long-term memory, and goals carried over from the previous decision cycle. CoALA does not limit working memory to the LLM's context; it treats it as a data structure that persists between LLM calls.

For each LLM call, the input is constructed from a portion of the working memory contents. CoALA gives a prompt template and the associated variables as an example. The model's output is parsed and written back to variables in working memory. These variables can include the name and arguments of an action, which are then used to execute the corresponding action.

Long-term memory: Information across sessions

In LangGraph, long-term memory holds information across different conversations or sessions. In contrast to the thread scope of short-term memory, long-term memory is stored in custom namespaces. LangChain states that this memory can contain user-specific or application-level data and can be shared across conversation threads. LangGraph stores provide functions for saving and retrieving this information.

CoALA's distinction considers the content of information as well as how long it is retained. Semantic memory holds the agent's knowledge about the world and itself. Episodic memory stores experiences from previous decision cycles. In CoALA, procedural memory covers knowledge embedded in model weights and knowledge explicitly written in the agent's code.

LangChain gives facts about a user as an example of semantic memory. This type of memory is not the same concept as semantic search. Semantic memory refers to the storage of facts and information. Semantic search is a method of retrieving similar content based on meaning rather than exact matches.

LangChain also describes storing semantic memory as a profile. Such a profile can contain selected information about a specific entity, such as a user, an organization or the agent itself. A profile is typically a JSON structure made up of key-value pairs that represent the domain. The previous profile can be given to the model to generate a new profile or a JSON patch to apply to the old structure. LangChain notes that this update can become error-prone as the profile grows.

The same information can also be held in a collection of separate memory records that are updated and expanded over time. According to LangChain, this approach also requires updating or deleting existing records and searching within the list. Some models may tend to add too many records to the list, while others may tend to update existing records too often. LangChain Store offers semantic search and filtering by content across a collection.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

Although each record in a collection follows a specific schema, this structure may not capture relationships between records or the full context. LangChain notes that, in such a case, the model may lack context that could be present in a unified profile when generating a response.

Episodic memory: Past experiences

CoALA defines episodic memory as the place where experiences from previous decision cycles are held. Its contents can be input-output pairs used for training, sequences of past events or sequences of moves from previous game episodes. Each of these is a different representation of the agent's experience.

During the planning stage of a decision cycle, past experiences can be retrieved into working memory and used during reasoning. In CoALA, the flow also works in the opposite direction: the agent can write experiences from working memory to episodic memory as a form of learning. Thus, reading and writing take place between the experience held in the long-term record and the information active for the current decision.

LangChain says that episodic memory is often used to help an agent remember how to perform a task. In practice, this memory can be built using few-shot examples drawn from past sequences. In the few-shot approach, the prompt is updated with input-output examples to demonstrate the intended behavior.

In this use, having examples and deciding which examples to retrieve are separate operations. LangChain notes that selecting relevant examples based on user input can be a challenge. A memory store is one way to store few-shot examples. LangChain also describes keeping data in a LangSmith Dataset and developing retrieval logic that selects examples based on user input.

How does a memory stream work?

The Generative Agents paper presents a memory stream design that holds experiences in natural language. The memory stream is a record of the agent's experiences, organized as a list of memory objects. Each object contains a natural-language description, a creation time and a last access time.

The basic element of this structure is an observation: an event the agent directly perceives. An observation can describe the agent's own behavior or the observed behavior of another agent or object. Examples in the paper include food being prepared at a café, a person studying while drinking coffee and an empty refrigerator.

The retrieval function in the paper takes the agent's current situation as input and passes a subset of the memory stream to the language model. The researchers' implementation scores records on recency, importance and relevance. Recency gives higher scores to recently accessed objects. Importance distinguishes ordinary events from those the agent considers important. Relevance measures how closely a record relates to the current situation.

To calculate relevance, the paper's implementation generates an embedding vector for the memory description and compares it with the query's embedding vector using cosine similarity. The scores for the three criteria are normalized and combined using weights. The highest-ranked records that fit within the model's context window are included in the prompt.

In the Generative Agents paper, reflection is also a type of memory. A reflection represents a more abstract thought generated by the agent and is considered alongside observation records during retrieval. The paper's architecture uses retrieved memories to determine actions, create longer-term plans and generate reflections. Plans and reflection records are added to the memory stream for later use.

Reflection generation in the paper can also use other reflection records. The researchers describe this structure as a tree: observation records are at the leaves, while higher-level inferences sit above them. Plans are also stored in the memory stream and included in retrieval. This allows observation, reflection and plan records to be considered in the same retrieval process.

Scope and limitations

According to LangChain, the entire history of a conversation may not fit within the LLM's context window; this can lead to an unrecoverable error. Even if the history fits, old or off-topic content in long contexts can affect model performance. The Generative Agents paper also notes that using the entire memory stream in the prompt can distract the model and may not fit within the context window.

There is no single way to implement long-term memory either. LangChain states that different applications need different types of memory and that there is no one-size-fits-all solution for long-term memory. In the Generative Agents paper, agents with access only to raw observation records may struggle to generalize or draw inferences, which also shows the distinction between keeping a record in memory and drawing an inference from it.

Ready to start?

Download the app and continue from your phone.

End of Entry