Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 32 ยท Memory and State

Conversation Memory Basics

Language models are stateless. Learn how LangChain adds conversation memory with message history, thread ids and checkpointers, with a runnable Python stand-in.

A language model remembers nothing between calls. When a chatbot seems to remember your name, the application is sending the earlier messages again with every request. "Memory" in LangChain is therefore not a feature of the model; it is the job of storing the conversation and replaying it. This lesson explains that idea, shows how modern LangChain does it with a checkpointer and a thread id, and rebuilds the mechanism in plain Python.

LangChain is not available on this site, so real code is read-only. The runnable examples are stand-ins with a scripted "model" so you can see the mechanics without a network.

The model is stateless

Two calls to the same model are independent:

Python
# Real LangChain (read-only)
model.invoke("My name is Mira.")
model.invoke("What is my name?")   # the model has no idea

To make the second call work, send both messages together:

Python
# Real LangChain (read-only)
model.invoke([
    {"role": "user", "content": "My name is Mira."},
    {"role": "assistant", "content": "Nice to meet you, Mira."},
    {"role": "user", "content": "What is my name?"},
])

The model now sees the whole conversation and can answer "Mira". Every memory technique is a way of building that list, and of keeping it from growing too long (the next lesson).

flowchart LR
    U["New user message"] --> L["Load history for thread"]
    L --> M["Model sees history + new message"]
    M --> S["Save updated history"]
    S --> R["Reply"]

Memory is load, call, save.

Short-term memory with a checkpointer

In LangChain 1.x, agents are built on LangGraph, and short-term memory is a checkpointer: an object that saves the agent's state (including its messages) after each step, keyed by a thread id. Same thread id means same conversation.

Python
# Real LangChain (read-only)
from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver

agent = create_agent(
    model="anthropic:claude-sonnet-4-5",
    tools=[],
    checkpointer=InMemorySaver(),
)

config = {"configurable": {"thread_id": "user-42"}}

agent.invoke({"messages": [{"role": "user", "content": "My name is Mira."}]}, config)
reply = agent.invoke({"messages": [{"role": "user", "content": "What is my name?"}]}, config)
print(reply["messages"][-1].content)   # mentions Mira

other = {"configurable": {"thread_id": "user-43"}}
agent.invoke({"messages": [{"role": "user", "content": "What is my name?"}]}, other)  # a new, empty thread

Notice that you only send the new message each time. The checkpointer loads the stored history for the thread, appends your message, runs the agent, and saves the result. InMemorySaver keeps everything in process memory, so it vanishes on restart; persistent checkpointers fix that.

Rebuilding it in plain Python

This stand-in has a dictionary acting as the checkpointer, keyed by thread id, and a scripted "model" that can answer a name question only if the name is in the history it receives.

Python Stand-in: thread ids and a history store

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Thread user-43 knows nothing because its history is empty. That isolation is why one deployed agent can serve thousands of users: each conversation is just a different key.

What the state contains

A checkpoint holds more than text. For an agent it includes the complete messages list (user, AI with tool calls, tool results) plus any other fields you add to the state. Because tool calls and results are stored too, the model can refer back to what a tool returned three turns ago.

You can also read a thread's state back, which is handy for debugging and for showing chat history in a UI:

Python
# Real LangChain (read-only)
state = agent.get_state(config)
for message in state.values["messages"]:
    print(message.type, ":", message.content)

Old-style memory classes

Older tutorials use classes like ConversationBufferMemory, ConversationSummaryMemory and ConversationChain. These are legacy; the LangChain docs now steer you to LangGraph persistence (checkpointers) and message-history utilities instead. If you see memory= in a code sample, it is probably from the older API. The ideas are the same (buffer, window, summary); only the plumbing changed.

Thread ids in practice

Choose thread ids deliberately.

  • One thread per conversation, not per user, if users can have several chats.
  • Include the user id in the key (f"{user_id}:{chat_id}") so a bug cannot leak one user's history to another.
  • Generate unguessable ids (UUIDs) if they ever appear in URLs.
  • Delete threads on request: history often contains personal data.
Python Stand-in: isolate users by thread key

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a chatbot appear to remember earlier messages?

    Models are stateless; memory means replaying stored history.
  2. 2. What identifies a conversation to a LangGraph checkpointer?

    State is saved and loaded per thread_id passed in config['configurable'].
  3. 3. What is lost when you use InMemorySaver and restart the process?

    InMemorySaver is for development; use a database-backed checkpointer for persistence.
  4. 4. When resuming a thread, what do you send to agent.invoke?

    The checkpointer loads the stored history; you send just the new message.

Summary

  • Models are stateless; memory means storing and replaying the message history.
  • A checkpointer plus a thread_id gives an agent short-term memory.
  • Send only the new message; the saved state supplies the rest.
  • Legacy Conversation*Memory classes map to the same ideas; prefer checkpointers.
  • Key threads carefully and delete them when users ask.

Next: Trimming and Summarizing History