AI Hints
Home›LangChain Tutorial›Prompts, Streaming, Memory
LangChain · Lesson 48 of 120

Agent Memory

Threads and checkpoints.

⏱ 2 min read▶ Video at 52:54🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

By default, each agent call starts fresh. To remember a conversation, we add a checkpointer. It saves the agent's state after every step, under a thread ID.

Agent Memory
From the video: Agent Memory
  • Each call starts fresh
  • Add a checkpointer
  • Saved under a thread ID

Remembering a conversation

We create the agent with an in memory saver as its checkpointer. Each call passes a config with a thread ID. First, Priya tells the agent her favourite genre. Then she asks for a recommendation, in the same thread. Run it. The agent remembers the genre. A different thread ID starts a completely separate conversation.

memory.pypython
from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver

agent = create_agent("google_genai:gemini-flash-lite-latest", tools=[],
                     system_prompt="You help Riverside Books customers. Be brief.",
                     checkpointer=InMemorySaver())

def chat(text, thread):
    cfg = {"configurable": {"thread_id": thread}}
    out = agent.invoke({"messages": [{"role": "user", "content": text}]}, cfg)
    print(f"[{thread}] {text}\n   -> {out['messages'][-1].text}")

chat("Hi, I'm Priya and I love science fiction.", "priya-1")
chat("What genre do I like? Answer in five words.", "priya-1")
chat("What genre do I like? Answer in five words.", "new-thread")
Output
[priya-1] Hi, I'm Priya and I love science fiction.
   -> Hi Priya! Nice to meet you. Are you looking for any sci-fi recommendations today?
[priya-1] What genre do I like? Answer in five words.
   -> You like science fiction.
[new-thread] What genre do I like? Answer in five words.
   -> You like mystery and thriller.

Checkpointers

In memory saver keeps everything in memory, so it's lost when the program stops. That's perfect for learning and tests. For real apps, use a database checkpointer, like SQLite or Postgres, so conversations survive restarts. The thread ID is usually a user or session ID.

keyvalue
InMemorySaverlearning and tests
SQLite / Postgresreal apps
thread_idone per conversation
Savedafter every step

Summary

A checkpointer saves an agent's state per thread, so it remembers conversations. Use in memory saver for tests, and a database for production. Next, structured answers from agents.

Key takeaways
  • checkpointer=InMemorySaver()
  • thread_id in config
  • Separate threads, separate memories
Next up: Structured Agent Answers — response_format