Course Content
LangChain Mastery
7 sections · 109 lessons
How do you retrieve memory from a LangChain conversation?
What you need to know
From a checkpointer (agents and LangGraph apps)
1cfg = {"configurable": {"thread_id": "cust-42"}}23snapshot = agent.get_state(cfg) # latest checkpoint for the thread4for m in snapshot.values["messages"]:5 print(m.type, "|", m.content[:80]) # human | ..., ai | ..., tool | ...67for past in agent.get_state_history(cfg): # newest first8 print(past.config["configurable"]["checkpoint_id"], len(past.values["messages"]))get_statereturns a snapshot:.values(the state),.next(the node that would run next — non-empty if the run is paused, for example waiting for human approval), and.config(including thecheckpoint_id).get_state_historylists every checkpoint, one per step. You can resume from an older one by passing itscheckpoint_idin the config — this is "time travel", useful for replaying a bad step.- The messages include tool calls and tool results, not only the visible chat.
From a long-term store
item = store.get(("users", "u-881"), "preferences") # exact keyitem.value if item else None # {"language": "hi", ...}hits = store.search(("users", "u-881"), query="delivery address", limit=3)search does semantic search if the store was created with an embedding index.
Legacy APIs, for reading old code
1# RunnableWithMessageHistory era (deprecated since langchain-core 1.3.3)2get_history("cust-42").messages # BaseChatMessageHistory -> list of messages34# ConversationBufferMemory era (langchain-classic, deprecated)5memory.load_memory_variables({}) # {"history": "Human: ...\nAI: ..."} or a message list6memory.chat_memory.messages # the underlying listBoth return plain message lists, so migrating is mostly a matter of reading from get_state instead. Do not write new code against them.
Turning messages into something useful
- For display — filter to
humanandaimessages without tool calls; users should not see raw tool output. - For export or analytics —
messages_to_dict(messages)fromlangchain_core.messagesgives JSON you can store or send. - For a handover to a human agent — pass the last N turns plus a short model-written summary.
A real-life example
A help-centre support bot hands difficult chats to human agents. The first version sent the human only the customer's last message, and agents had to ask "Can you explain again?" — the top complaint in customer surveys.
The fix reads the thread with agent.get_state(cfg), keeps human and AI messages (dropping 30 to 40 tool messages per long chat), and prepends a 3-line summary made by a small model. The human agent sees the whole story in one screen. Average handling time for handed-over chats fell from 9 minutes to 6.
The same team uses get_state_history when investigating complaints: they open the checkpoint just before a wrong answer and see exactly what the model saw.
Follow-up questions to expect
- "How do you show a user their past conversations?" — Store a list of thread IDs per user in your own database, then load each with
get_state; the checkpointer does not index threads by user for you. - "What is in
snapshot.next?" — The nodes that will run when the thread resumes; empty when the run finished. - "Can you edit memory?" — Yes:
update_state(cfg, {"messages": [...]})writes a new checkpoint; useRemoveMessageto delete specific messages.