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 building an agent using LangGraph. Zep is 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

$pip install zep-cloud langchain-openai langgraph ipywidgets python-dotenv

Configure Zep

Ensure that you’ve configured the following API keys in your environment. We’re using Zep’s Async client here, but we could also use the non-async equivalent.

$ZEP_API_KEY=your_zep_api_key_here
>OPENAI_API_KEY=your_openai_api_key_here
1import os
2import uuid
3import logging
4from typing import Annotated, TypedDict
5
6from zep_cloud.client import AsyncZep
7from zep_cloud import Message
8
9from langchain_core.messages import AIMessage, SystemMessage, HumanMessage, trim_messages
10from langchain_core.tools import tool
11from langchain_openai import ChatOpenAI
12from langgraph.checkpoint.memory import MemorySaver
13from langgraph.graph import END, START, StateGraph, add_messages
14from langgraph.prebuilt import ToolNode
15
16# Optional: Load environment variables from .env file
17# from dotenv import load_dotenv
18# load_dotenv()
19
20# Set up logging
21logging.basicConfig(level=logging.INFO)
22logger = logging.getLogger(__name__)
23
24# Initialize Zep client
25zep = AsyncZep(api_key=os.environ.get('ZEP_API_KEY'))

Define State and Setup Tools

First, define the state structure for our LangGraph agent:

1class State(TypedDict):
2 messages: Annotated[list, add_messages]
3 first_name: str
4 last_name: str
5 thread_id: str
6 user_name: str

Using Zep’s Search as a Tool

These are examples of simple Tools that search Zep for facts (from edges) or nodes. Since LangGraph tools don’t automatically receive the full graph state, we create a function that returns configured tools for a specific user:

1def create_zep_tools(user_name: str):
2 """Create Zep search tools configured for a specific user."""
3
4 @tool
5 async def search_facts(query: str, limit: int = 5) -> list[str]:
6 """Search for facts in all conversations had with a user.
7
8 Args:
9 query (str): The search query.
10 limit (int): The number of results to return. Defaults to 5.
11
12 Returns:
13 list: A list of facts that match the search query.
14 """
15 result = await zep.graph.search(
16 user_id=user_name, query=query, limit=limit, scope="edges"
17 )
18 facts = [edge.fact for edge in result.edges or []]
19 if not facts:
20 return ["No facts found for the query."]
21 return facts
22
23 @tool
24 async def search_nodes(query: str, limit: int = 5) -> list[str]:
25 """Search for nodes in all conversations had with a user.
26
27 Args:
28 query (str): The search query.
29 limit (int): The number of results to return. Defaults to 5.
30
31 Returns:
32 list: A list of node summaries for nodes that match the search query.
33 """
34 result = await zep.graph.search(
35 user_id=user_name, query=query, limit=limit, scope="nodes"
36 )
37 summaries = [node.summary for node in result.nodes or []]
38 if not summaries:
39 return ["No nodes found for the query."]
40 return summaries
41
42 return [search_facts, search_nodes]
43
44# We'll create the actual tools after we have a user_name
45llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

Chatbot Function Explanation

The chatbot uses Zep to provide context-aware responses. Here’s how it works:

  1. Context Retrieval: It retrieves relevant facts for the user’s current conversation (thread). Zep uses the most recent messages to determine what facts to retrieve.

  2. System Message: It constructs a system message incorporating the facts retrieved in 1., setting the context for the AI’s response.

  3. 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.

  4. 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.

We could also use Zep to recall the chat history, rather than LangGraph’s MemorySaver.

See thread.get_user_context in the Zep SDK documentation.

1async def chatbot(state: State):
2 memory = await zep.thread.get_user_context(state["thread_id"])
3
4 system_message = SystemMessage(
5 content=f"""You are a compassionate mental health bot and caregiver. Review information about the user and their prior conversation below and respond accordingly.
6 Keep responses empathetic and supportive. And remember, always prioritize the user's well-being and mental health.
7
8 {memory.context}"""
9 )
10
11 messages = [system_message] + state["messages"]
12
13 response = await llm.ainvoke(messages)
14
15 # Add the new chat turn to the Zep graph
16 messages_to_save = [
17 Message(
18 role="user",
19 name=state["first_name"] + " " + state["last_name"],
20 content=state["messages"][-1].content,
21 ),
22 Message(role="assistant", content=response.content),
23 ]
24
25 await zep.thread.add_messages(
26 thread_id=state["thread_id"],
27 messages=messages_to_save,
28 )
29
30 # Truncate the chat history to keep the state from growing unbounded
31 # In this example, we going to keep the state small for demonstration purposes
32 # We'll use Zep's Facts to maintain conversation context
33 state["messages"] = trim_messages(
34 state["messages"],
35 strategy="last",
36 token_counter=len,
37 max_tokens=3,
38 start_on="human",
39 end_on=("human", "tool"),
40 include_system=True,
41 )
42
43 logger.info(f"Messages in state: {state['messages']}")
44
45 return {"messages": [response]}

Setting up the Agent

This function creates a complete LangGraph agent configured for a specific user. This approach allows us to properly configure the tools with the user context:

1def create_agent(user_name: str):
2 """Create a LangGraph agent configured for a specific user."""
3
4 # Create tools configured for this user
5 tools = create_zep_tools(user_name)
6 tool_node = ToolNode(tools)
7 llm_with_tools = ChatOpenAI(model="gpt-4o-mini", temperature=0).bind_tools(tools)
8
9 # Update the chatbot function to use the configured LLM
10 async def chatbot_with_tools(state: State):
11 memory = await zep.thread.get_user_context(state["thread_id"])
12
13 system_message = SystemMessage(
14 content=f"""You are a compassionate mental health bot and caregiver. Review information about the user and their prior conversation below and respond accordingly.
15 Keep responses empathetic and supportive. And remember, always prioritize the user's well-being and mental health.
16
17 {memory.context}"""
18 )
19
20 messages = [system_message] + state["messages"]
21
22 response = await llm_with_tools.ainvoke(messages)
23
24 # Add the new chat turn to the Zep graph
25 messages_to_save = [
26 Message(
27 role="user",
28 name=state["first_name"] + " " + state["last_name"],
29 content=state["messages"][-1].content,
30 ),
31 Message(role="assistant", content=response.content),
32 ]
33
34 await zep.thread.add_messages(
35 thread_id=state["thread_id"],
36 messages=messages_to_save,
37 )
38
39 # Truncate the chat history to keep the state from growing unbounded
40 state["messages"] = trim_messages(
41 state["messages"],
42 strategy="last",
43 token_counter=len,
44 max_tokens=3,
45 start_on="human",
46 end_on=("human", "tool"),
47 include_system=True,
48 )
49
50 logger.info(f"Messages in state: {state['messages']}")
51
52 return {"messages": [response]}
53
54 # Define the function that determines whether to continue or not
55 async def should_continue(state, config):
56 messages = state["messages"]
57 last_message = messages[-1]
58 # If there is no function call, then we finish
59 if not last_message.tool_calls:
60 return "end"
61 # Otherwise if there is, we continue
62 else:
63 return "continue"
64
65 # Build the graph
66 graph_builder = StateGraph(State)
67 memory = MemorySaver()
68
69 graph_builder.add_node("agent", chatbot_with_tools)
70 graph_builder.add_node("tools", tool_node)
71
72 graph_builder.add_edge(START, "agent")
73 graph_builder.add_conditional_edges("agent", should_continue, {"continue": "tools", "end": END})
74 graph_builder.add_edge("tools", "agent")
75
76 return graph_builder.compile(checkpointer=memory)

Our LangGraph agent graph is illustrated below.

Agent Graph

Running the Agent

We generate a unique user name and thread id, add these to Zep, and create our configured agent:

1first_name = "Daniel"
2last_name = "Chalef"
3user_name = first_name + uuid.uuid4().hex[:4]
4thread_id = uuid.uuid4().hex
5
6# Create user and thread in Zep
7await zep.user.add(user_id=user_name, first_name=first_name, last_name=last_name)
8await zep.thread.create(thread_id=thread_id, user_id=user_name)
9
10# Create the agent configured for this user
11graph = create_agent(user_name)
12
13
14def extract_messages(result, user_name):
15 output = ""
16 for message in result["messages"]:
17 if isinstance(message, AIMessage):
18 name = "assistant"
19 else:
20 name = user_name
21 output += f"{name}: {message.content}\n"
22 return output.strip()
23
24
25async def graph_invoke(
26 message: str,
27 first_name: str,
28 last_name: str,
29 user_name: str,
30 thread_id: str,
31 ai_response_only: bool = True,
32):
33 r = await graph.ainvoke(
34 {
35 "messages": [HumanMessage(content=message)],
36 "first_name": first_name,
37 "last_name": last_name,
38 "thread_id": thread_id,
39 "user_name": user_name,
40 },
41 config={"configurable": {"thread_id": thread_id}},
42 )
43
44 if ai_response_only:
45 return r["messages"][-1].content
46 else:
47 return extract_messages(r, user_name)

Let’s test the agent with a few messages:

1r = await graph_invoke(
2 "Hi there?",
3 first_name,
4 last_name,
5 user_name,
6 thread_id,
7)
8
9print(r)

Hello! How are you feeling today? I’m here to listen and support you.

1r = await graph_invoke(
2 """
3 I'm fine. But have been a bit stressful lately. Mostly work related.
4 But also my dog. I'm worried about her.
5 """,
6 first_name,
7 last_name,
8 user_name,
9 thread_id,
10)
11
12print(r)

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.

Viewing The Context Value

1memory = await zep.thread.get_user_context(thread_id=thread_id)
2
3print(memory.context)

The context value will look something like this:

FACTS and ENTITIES represent relevant context to the current conversation.
# These are the most relevant facts and their valid date ranges
# format: FACT (Date range: from - to)
<FACTS>
- Daniel99db is worried about his sick dog. (2025-01-24 02:11:54 - present)
- Daniel Chalef is worried about his sick dog. (2025-01-24 02:11:54 - present)
- The assistant asks how the user is feeling. (2025-01-24 02:11:51 - present)
- Daniel99db has been a bit stressful lately due to his dog. (2025-01-24 02:11:53 - present)
- Daniel99db has been a bit stressful lately due to work. (2025-01-24 02:11:53 - present)
- Daniel99db is a user. (2025-01-24 02:11:51 - present)
- user has the id of Daniel99db (2025-01-24 02:11:50 - present)
- user has the name of Daniel Chalef (2025-01-24 02:11:50 - present)
</FACTS>
# These are the most relevant entities
# ENTITY_NAME: entity summary
<ENTITIES>
- worried: Daniel Chalef (Daniel99db) is feeling stressed lately, primarily due to work-related issues and concerns about his sick dog, which has made him worried.
- Daniel99db: Daniel99db, or Daniel Chalef, is currently experiencing stress primarily due to work-related issues and concerns about his sick dog. Despite these challenges, he has shown a desire for interaction by initiating conversations, indicating his openness to communication.
- sick: Daniel Chalef, also known as Daniel99db, is feeling stressed lately, primarily due to work-related issues and concerns about his sick dog. He expresses worry about his dog's health.
- Daniel Chalef: Daniel Chalef, also known as Daniel99db, has been experiencing stress recently, primarily related to work issues and concerns about his sick dog. Despite this stress, he has been feeling generally well and has expressed a desire to connect with others, as indicated by his friendly greeting, "Hi there?".
- dog: Daniel99db, also known as Daniel Chalef, mentioned that he has been feeling a bit stressed lately, which is related to both work and his dog.
- work: Daniel Chalef, also known as Daniel99db, has been experiencing stress lately, primarily related to work.
- feeling: The assistant initiates a conversation by asking how the user is feeling today, indicating a willingness to listen and provide support.
</ENTITIES>
1r = await graph_invoke(
2 "She ate my shoes which were expensive.",
3 first_name,
4 last_name,
5 user_name,
6 thread_id,
7)
8
9print(r)

That sounds really frustrating, especially when you care so much about your belongings and your dog’s health. It’s tough when pets get into things they shouldn’t, and it can add to your stress. How are you feeling about that situation? Are you able to focus on her health despite the shoe incident?

Let’s now test whether the Agent is correctly grounded with facts from the prior conversation.

1r = await graph_invoke(
2 "What are we talking about?",
3 first_name,
4 last_name,
5 user_name,
6 thread_id,
7)
8
9print(r)

We were discussing your concerns about your dog being sick and the situation with her eating your expensive shoes. It sounds like you’re dealing with a lot right now, and I want to make sure we’re addressing what’s on your mind. If there’s something else you’d like to talk about or if you want to share more about your dog, 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.

1r = await graph_invoke(
2 "What have I said about my job?",
3 first_name,
4 last_name,
5 user_name,
6 thread_id,
7)
8
9print(r)

You’ve mentioned that you’ve been feeling a bit stressed lately, primarily due to work-related issues. If you’d like to share more about what’s been going on at work or how it’s affecting you, I’m here to listen and support you.