Skip to content
AI Agent How-To

Home ›Framework how-tos

LangGraph: How to Add Memory (Checkpointers)

Intermediate ~6 min read Last tested November 2026

Updated 2026-11-24

LangGraph memory is short-term, thread-scoped persistence through a checkpointer. Pass a thread_id on every call so the graph reloads prior messages and tool results. This guide extends the baseline ReAct agent from LangGraph: build an agent with InMemorySaver for local dev and points to Postgres for production. Last tested 2026-11-24.

Prerequisites

Docs used:

Steps

1. Install LangGraph (checkpointer included)

python -m venv .venv && source .venv/bin/activate
pip install langgraph langchain-openai
export OPENAI_API_KEY="sk-..."

InMemorySaver ships in the langgraph package (langgraph.checkpoint.memory).

2. Create a checkpointer and compile the agent with it

# memory_agent.py
from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.prebuilt import create_react_agent

def get_weather(city: str) -> str:
    """Return demo weather for a city."""
    return f"It's sunny and 72°F in {city}."

checkpointer = InMemorySaver()
model = ChatOpenAI(model="gpt-4.1-mini")
agent = create_react_agent(model, [get_weather], checkpointer=checkpointer)

create_react_agent accepts checkpointer= the same way a hand-built StateGraph does after builder.compile(checkpointer=checkpointer).

3. Pass a stable thread_id on every invoke

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

first = agent.invoke(
    {"messages": [{"role": "user", "content": "Remember my name is Alex."}]},
    config,
)
print(first["messages"][-1].content)

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

The second turn should answer from the saved checkpoint because thread_id matches.

Use a different thread_id per user or chat session. Keep values under 255 characters when you later move to Postgres (persistence troubleshooting).

4. Stream intermediate steps with the same config

for event in agent.stream(
    {"messages": [{"role": "user", "content": "Weather in Oslo?"}]},
    config,
    stream_mode="values",
):
    if event.get("messages"):
        print(event["messages"][-1].content)

config must stay on every invoke, stream, and stream_events call or memory will not attach.

5. Move to Postgres for production persistence

InMemorySaver is lost when the process exits. For durable checkpoints:

pip install "psycopg[binary,pool]" langgraph-checkpoint-postgres
from langgraph.checkpoint.postgres import PostgresSaver

DB_URI = "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable"
with PostgresSaver.from_conn_string(DB_URI) as checkpointer:
    checkpointer.setup()  # first run only
    agent = create_react_agent(model, [get_weather], checkpointer=checkpointer)
    agent.invoke({"messages": [{"role": "user", "content": "hi"}]}, config)

Run checkpointer.setup() once per database. See Add memory for async variants.

6. Cap runaway multi-turn loops

Memory lets conversations grow. Pair it with step limits from Fix: LangGraph recursion limit or lower recursion_limit in config when tool loops spiral.

Common errors

Second turn forgets the first. thread_id changed, or you called invoke without config. Log config["configurable"]["thread_id"] before each run.

InMemorySaver empty after restart. Expected. Swap in PostgresSaver or SqliteSaver for disk-backed checkpoints (persistence guide).

Postgres thread_id too long error. Hash or truncate IDs to under 255 characters before passing them in configurable.

Checkpointer tables missing. Call checkpointer.setup() after PostgresSaver.from_conn_string.

FAQ

What is the difference between a checkpointer and a LangGraph store?

Checkpointers save graph state per thread (conversation memory). Stores hold application key-value data across threads (user preferences). Most agents start with a checkpointer only (persistence overview).

Does create_react_agent support memory?

Yes. Pass checkpointer= when you build the agent, then supply {"configurable": {"thread_id": "..."}} on each call.

When should I use InMemorySaver?

Local development and unit tests. Never rely on it for production restarts or horizontal scale.

How does this relate to the raw Python loop?

Build your first AI agent in Python keeps history in your own list. LangGraph checkpoints do that with versioned state you can inspect and resume.