LangChain Mastery

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)

Python
cfg = {"configurable": {"thread_id": "cust-42"}}snapshot = agent.get_state(cfg)             # latest checkpoint for the threadfor m in snapshot.values["messages"]:    print(m.type, "|", m.content[:80])      # human | ..., ai | ..., tool | ...for past in agent.get_state_history(cfg):   # newest first    print(past.config["configurable"]["checkpoint_id"], len(past.values["messages"]))
  • get_state returns 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 the checkpoint_id).
  • get_state_history lists every checkpoint, one per step. You can resume from an older one by passing its checkpoint_id in 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

Python
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

Python
# RunnableWithMessageHistory era (deprecated since langchain-core 1.3.3)get_history("cust-42").messages     # BaseChatMessageHistory -> list of messages# ConversationBufferMemory era (langchain-classic, deprecated)memory.load_memory_variables({})    # {"history": "Human: ...\nAI: ..."} or a message listmemory.chat_memory.messages         # the underlying list

Both 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 human and ai messages without tool calls; users should not see raw tool output.
  • For export or analytics — messages_to_dict(messages) from langchain_core.messages gives 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; use RemoveMessage to delete specific messages.