AI Hints
Home›LangGraph Tutorial›Checkpointers and Threads
LangGraph · Lesson 42 of 120

Memory Across Turns

A chatbot that remembers.

⏱ 2 min read▶ Video at 44:17🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Remember our chatbot, where we carried the history ourselves? With a checkpointer, LangGraph carries it. Each turn sends only the new message. The saved messages are added automatically.

Memory Across Turns
From the video: Memory Across Turns
  • No more carrying history
  • Send only the new message
  • LangGraph adds the rest

A chatbot with memory

The chat node is the same as before. We compile with a checkpointer. Each turn sends just one new message, on the same thread. Run it. In the second turn, the bot remembers the book Tom mentioned, because the first turn was saved.

chat_memory.pypython
from langchain.chat_models import init_chat_model
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import MessagesState, StateGraph, START

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)

def chat(s: MessagesState):
    return {"messages": [model.invoke(s["messages"])]}

b = StateGraph(MessagesState); b.add_node(chat); b.add_edge(START, "chat")
graph = b.compile(checkpointer=InMemorySaver())
config = {"configurable": {"thread_id": "tom"}}

for text in ["I just finished reading Dune. Reply in one sentence.", "Which book did I just finish? Title only."]:
    out = graph.invoke({"messages": [{"role": "user", "content": text}]}, config)
    print("Tom:", text)
    print("Bot:", out["messages"][-1].text.strip())
Output
Tom: I just finished reading Dune. Reply in one sentence.
Bot: Welcome to Arrakis, where the spice must flow, water is life, and you now
understand why desert power changes everything.
Tom: Which book did I just finish? Title only.
Bot: Dune

Memory tips

With a checkpointer, the history grows every turn. For long chats, trim old messages, or summarise them, in a node before the model call. And for real apps, swap the in memory saver for a database, so memory survives a restart.

keyvalue
Sendonly new messages
Historygrows each turn
Long chatstrim or summarise
Productiondatabase saver

Summary

With a checkpointer and a thread ID, a chatbot remembers. You send only the new message. Next, reading the saved state of a thread.

Key takeaways
  • Only new messages
  • Saved history added
  • Same thread_id
Next up: Reading State — get_state