LangGraph Memory Example
LangGraph is a library created by LangChain for building stateful, multi-agent applications. This example demonstrates using Zep for LangGraph agent memory.
A complete Notebook example of using Zep for LangGraph Memory may be found in the Zep Python SDK Repository.
The following example demonstrates a LangGraph agent using Zep for memory. Facts from Zep’s memory graph are used to personalize agent responses based on information learned from prior conversations.
The agent implements:
- persistance of new chat turns to Zep and recall of relevant Facts using the most recent messages.
- an in-memory MemorySaver to maintain agent state. We use this to add recent chat history to the agent prompt. As an alternative, you could use Zep for this.
You should consider truncating MemorySaver’s chat history as by default LangGraph state grows unbounded. We’ve included this in our example below. See the LangGraph documentation for insight.
Install dependencies
Configure Zep
Ensure that you’ve configured the following API key in your environment. We’re using Zep’s Async client here, but we could also use the non-async equivalent.
Using Zep’s Search as a Tool
This is an example of a simple Tool that searches Zep for facts related to a user, irrespective which chat session. We also include relevant facts from the current session in the Agent’s prompt.
Chatbot Function Explanation
The chatbot uses Zep to provide context-aware responses. Here’s how it works:
-
Context Retrieval: It retrieves relevant facts for the user’s current conversation (session). Zep uses the most recent messages to determine what facts to retrieve.
-
System Message: It constructs a system message incorporating the facts retrieved in 1., setting the context for the AI’s response.
-
Message Persistence: After generating a response, it asynchronously adds the user and assistant messages to Zep. New Facts are created and existing Facts updated using this new information.
-
Messages in State: We use LangGraph state to store the most recent messages and add these to the Agent prompt. We limit the message list to the most recent 3 messages for demonstration purposes.
This approach enables the chatbot to maintain context across interactions and provide personalized responses based on the user’s history and preferences stored in Zep.
We could also use Zep to recall the chat history, rather than LangGraph’s MemorySaver.
See memory.get
in the Zep API documentation.
Setting up the Agent
This section sets up the Agent’s LangGraph graph:
-
Graph Structure: It defines a graph with nodes for the agent (chatbot) and tools, connected in a loop.
-
Conditional Logic: The
should_continue
function determines whether to end the graph execution or continue to the tools node based on the presence of tool calls. -
Memory Management: It uses a MemorySaver to maintain conversation state across turns. This is in addition to using Zep for facts.
Our LangGraph agent graph is illustrated below.
Running the Agent
We generate a unique user name and thread id (session id) and add these to Zep, associating the Session with the new User.
Hello! How are you feeling today? I’m here to listen and support you.
I’m sorry to hear that you’ve been feeling stressed. Work can be a significant source of pressure, and it sounds like your dog might be adding to that stress as well. If you feel comfortable sharing, what specifically has been causing you stress at work and with your dog? I’m here to help you through it.
I’m really sorry to hear that your dog is sick. It’s completely understandable to feel worried about her; our pets are like family. Have you been able to take her to the vet? Sometimes getting a professional opinion can help ease some of that worry. I’m here to support you through this.
Viewing All Conversation Facts
Fact: Daniel’s dog is also contributing to his stress.
Fact: Daniel’s stress is mostly work-related.
Fact: Daniel has been feeling stressed lately.
That sounds really frustrating, especially on top of everything else you’re dealing with. It’s tough when our pets do things that add to our stress, especially when it involves something valuable. It’s okay to feel upset about it. How are you managing with everything?
Let’s now test whether the Agent is correctly grounded with facts from the prior conversation.
We were discussing the stress you’ve been feeling lately, particularly related to work and your dog’s health. You mentioned that your dog has been sick and also chewed up your expensive shoes, which can be really frustrating. If there’s something else on your mind or if you’d like to talk about a different topic, I’m here to listen!
Let’s go even further back to determine whether context is kept by referencing a user message that is not currently in the Agent State. Zep will retrieve Facts related to the user’s job.
You’ve mentioned that you’ve been feeling stressed lately, and it seems that your job is a significant part of that stress. If you’d like to share more about what’s been going on at work, I’m here to listen and help however I can.