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.
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_agentfrom langgraph.checkpoint.memory import InMemorySaveragent = 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.
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.
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.