Memory Across Turns
A chatbot that remembers.
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.

- 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.
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())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.
| key | value |
|---|---|
| Send | only new messages |
| History | grows each turn |
| Long chats | trim or summarise |
| Production | database 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