ConversationMemoryManagementSystem

Open in ColabOpen in GitHub

Overview

In modern AI systems, memory management is essential for crafting personalized and context-aware user experiences. Without the ability to recall prior messages, an AI assistant would quickly become repetitive and less engaging. This updated code demonstrates a robust approach to handling both short-term and long-term memory in a conversational setting, by integrating:

  • A central Configuration class for managing runtime parameters (such as user_id and model name)

  • An upsert_memory function for storing or updating user data in a memory store

  • A call_model function that retrieves context-relevant memories and incorporates them into the system prompt for the model

  • A store_memory function that persists newly identified memories and tool calls

  • A StateGraph that orchestrates the entire conversation flow, connecting nodes like call_model and store_memory to streamline user interactions

By leveraging these components, your conversation agent can maintain deep context over multiple turns, provide more accurate and engaging responses, and seamlessly update its memory when new information arises. This design illustrates a scalable way to build conversational AI systems that dynamically remember, reason, and respond according to user needs.

What is Memory?

Memory refers to the capability of an AI system to store, retrieve, and use information. In conversational AI, this typically involves recalling the user’s previous statements, preferences, or relevant context—leading to more personalized and adaptive interactions.

Short-term Memory

Short-term memory lets your application remember previous interactions within a single thread or conversation. A thread organizes multiple interactions in a session, similar to the way email groups messages in a single conversation.

LangGraph manages short-term memory as part of the agent's state, persisted via thread-scoped checkpoints. This state can normally include the conversation history along with other stateful data, such as uploaded files, retrieved documents, or generated artifacts. By storing these in the graph's state, the bot can access the full context for a given conversation while maintaining separation between different threads.

Since conversation history is the most common form of representing short-term memory, in the next section, we will cover techniques for managing conversation history when the list of messages becomes long.

Long-term Memory

Long-term memory extends an AI system's capability to recall information across multiple conversations or sessions. Unlike short-term memory, which focuses on maintaining the context of a single thread, long-term memory stores persistent information such as user preferences, key facts, and important events. This enables the system to create a seamless and personalized user experience over time.

Long-term memory is typically used to:

  • Recall User Preferences: For example, remembering a user prefers movie recommendations in a specific genre.

  • Track Progress: Storing milestones or progress made in ongoing projects or discussions.

  • Adapt Over Time: Learning about the user’s changing interests or requirements.

In this system, long-term memory is managed through memory upserts that save critical user information to a persistent data store. This allows the agent to access, update, and retrieve information beyond a single conversational thread.

How Long-term Memory Works in LangGraph

In LangGraph, long-term memory is implemented as part of a persistent data layer, decoupled from the conversational state. Key components include:

  1. Memory Store: A database or key-value store where long-term memory records are saved.

  2. Memory Retrieval: Mechanisms to fetch relevant memories based on the current conversation context.

  3. Memory Updates: Processes to modify or append new data to existing memory entries when user information evolves.

By linking long-term memory with call_model and store_memory functions, the system can ensure that the language model has access to relevant, long-term context. This makes interactions more coherent and reduces repetitive queries.

Challenges with Long-term Memory

  • Scalability: As the number of users and stored records grows, querying and retrieving relevant memories efficiently becomes a challenge.

  • Relevance Filtering: Not all past information is useful for every interaction. Filtering out irrelevant data while retaining critical context is key.

  • Data Privacy: Long-term memory must comply with privacy regulations, ensuring sensitive data is securely handled and stored.

Short-term vs Long-term Memory

Type

Scope

Purpose

Example

Short-term Memory

Single conversational thread

Maintains recent interactions to provide immediate context

Last few user prompts in the current conversation

Long-term Memory

Multiple threads & sessions

Stores key information and summarized data to maintain continuity across broader conversations

User preferences, important facts, or conversation history

  • Short-term Memory: Helps the system focus on the latest messages for immediate context.

  • Long-term Memory: Enables the agent to recall past sessions and user-specific details, creating a more persistent experience over time.

Why use LangGraph's checkpointer?

  1. Session Memory & Error Recovery

    • Lets you roll back to a previous checkpoint if an error occurs or if you want to resume from a saved state

    • Maintains context across conversations for a more seamless user experience

  2. Flexible Database Options & Scalability

    • Supports in-memory, SQLite, Postgres, and more, allowing easy scaling as your user base grows

    • Choose the storage method that best fits your project’s needs

  3. Human-in-the-Loop & Time Travel

    • Pause workflows for human review, then resume where you left off

    • Go back to earlier states (“time travel”) to debug or create alternative paths

  4. Ecosystem & Customization

    • LangGraph v0.2 offers separate checkpointer libraries (e.g., MemorySaver, SqliteSaver, PostgresSaver)

    • Easily build or adapt custom solutions for specific databases or workflows

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.

[Note] This is not necessary if you've already set the required API keys in previous steps.

Define System Prompt and Configuration

This section introduces the SYSTEM_PROMPT and the Configuration class. They are essential for setting up the system’s behavior and managing environment variables (for example, choosing which language model to use). You can think of Configuration as the single source of truth for any settings your application might need.

Initialize LLM and Define State Class

In this part, we configure the ChatOpenAI model (using model and temperature settings) and introduce a State class. The State class holds the conversation messages, ensuring that context is retained and can be easily passed around. This lays the foundation for a conversational agent that genuinely “remembers” what has been said.

Memory Upsert Function

Here, we focus on the upsert_memory function. This function is responsible for storing or updating (upserting) user-specific data. By preserving user context across conversations—like interests, preferences, or corrections—you can give your application a more persistent and personalized feel.

Implement Conversation Flow

Next, we implement two important functions for our conversation flow:

  1. call_model: Takes the current conversation State, retrieves relevant memories, and then sends them along with user messages to the LLM.

  2. store_memory: Processes the model’s tool calls—in this case, requests to store data—and updates the memory store accordingly.

By combining these two functions, the model not only uses past context but also augments it with new information in real time.

Define Conditional Edge Logic

Since our memory agent handles both retrieving past information and storing new memories, we need to establish conditions that guide the system through these steps dynamically.

The function route_message is responsible for evaluating the latest message and deciding whether to:

  • Store a new memory: If the AI generates a response that includes tool calls, meaning it intends to save new information about the user, we direct the flow to store_memory.

  • Finish the process: If there are no tool calls, we end the conversation turn.

This ensures that memory storage occurs only when necessary while allowing the model to generate responses without unnecessary interruptions. This logic helps keep the conversation flow efficient and natural.

Build and Execute StateGraph

In this section, we construct a StateGraph to define the flow of the conversation. We specify which node (for instance, call_model) leads to which next step (for example, store_memory). Once the graph is set, we run sample conversations to see how the system dynamically manages user input, retrieves relevant memories, and updates them when necessary.

png

Verify Results and View Stored Memories

Finally, we examine the stored memories to confirm that our system has correctly captured user’s context. You can look into the final conversation state (using graph.get_state) and see how messages and memories have been organized. This is a great point to do some debugging if anything seems amiss, ensuring that your memory mechanism works just as intended.

Last updated