To implement persistent memory in LangGraph, import the memory saver checkpointer from langgraph.checkpoint.memory and configure it during graph compilation by adding a configurable thread ID, which enables the model to retain conversation context across multiple invocations; without this configuration, the model loses context between messages.
LangGraph Memory Agents: Python Code Tutorial
Added:Till this point we just learned how to make a graph. Now let's learn how to make a persistent memory.
So doing that is actually very easy.
The one way is very easy that I'll teach you. So you just need to import in the same code langraph dot checkpoint dot memory. Let's import memory saver and we are going to use the same state and same builder.
Let's use same model also. Let's use same node. This is also going to be the same. The only thing that is going to change is when you do a compilation.
Then you can define your memory saver checkpointer.
This is what we are saying. We are saying add the persistent sense with the memory. So let's make a thread.
Thread one and it is going to be in this format is going to be you need to define uh the configurable and the thread ID. So it can be a UU ID.
You can provide uh any ID. It should be unique one.
Now the way you are going to invoke your uh mod is going to be different. So let's comment this out and till thread it we just added the thread one and let's bring our first AI message.
So here you can see we are calling just the same thing. Hi, my name is Jack and this is the thread ID. So if you remember, if you invoke again the model will lost the context of previous message.
Let me run it now without this thread. If you if you're going to run it and then if you make an another invocation, let's make it result two.
And we'll ask what was my name?
And we as long as we provide the same config variable then uh we can we can ensure that model will have this memory context with it. So let's run it and see if it works.
Perfect. Hi, my name is Jack. This is the human message. Hi Jack, nice to meet you. I How can I help you? Now, this is the second message. You said your name was Jack.
That's perfect. Now, what happen if I uh do not pass this thread and then run it?
All right, we need to remove this as well. We need to compile it without checkpoint. You'll see um you'll see the difference.
This is the first message. After that, we'll have second message if we are interested. What was my name? I don't know. you haven't told me your name in this conversation and I don't have any X just the difference is we we just passed the configurable memory savers thread point and that is the only difference that it made for the memory management. This is the one of the ways of uh doing the memory management and it's very helpful if you want uh your uh model to retain the context of the conversation like in a chatbot system.
So this is how you are going to do it.
Thank you.
Up Next

Deploy AI Agents with LangServe: Complete Technical Guide
@ColeMedin
12.7K views•2024-09-02

IFS Therapy Demonstration: Complete Session with Unburdening
@IFSCA
95.9K views•2021-01-13

FastAPI vs Flask vs Django: Choosing the Right Python Web Framework
@TechWithTim
302.5K views•2024-05-26

Game of Thrones Opening Credits: A Cinematic Analysis
@gameofthrones
46.3M views•2011-04-18
Related Study Plans & Knowledge Roadmaps
Structured learning paths in General & Interdisciplinary Studies
![Learn 8 Python Important Concepts Simplified [ Intermediate Python ]](https://i.ytimg.com/vi/qeOcZiSp3tE/maxresdefault.jpg)





































