Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 32 ยท Memory and State

Persistent Checkpoints and Long-Term Memory

Persist agent state with SQLite or Postgres checkpointers, replay and fork threads, and add long-term memory with a store. Stand-ins run in Python with sqlite3.

InMemorySaver is perfect for learning and tests, but it forgets everything when the process stops. Production agents need two kinds of persistence. Short-term memory (a thread's conversation) lives in a database-backed checkpointer, so users can come back tomorrow and a crashed server can resume. Long-term memory (facts about a user that matter across conversations) lives in a separate store that every thread can read and write. This lesson explains both and rebuilds them with Python's sqlite3.

LangChain and LangGraph are not available on this site, so real code is read-only. The runnable examples are stand-ins that use the standard library's in-memory sqlite3, which does run in the browser's Python.

Database-backed checkpointers

LangGraph ships checkpointers as separate packages so you install only what you need. The usual ones are SQLite (single machine, small apps) and Postgres (shared, production).

Python
# Real LangGraph (read-only)
from langgraph.checkpoint.sqlite import SqliteSaver
from langchain.agents import create_agent

with SqliteSaver.from_conn_string("agent_memory.db") as checkpointer:
    agent = create_agent(
        model="anthropic:claude-sonnet-4-5",
        tools=[],
        checkpointer=checkpointer,
    )
    config = {"configurable": {"thread_id": "alice:chat-1"}}
    agent.invoke({"messages": [{"role": "user", "content": "Remember: I prefer metric units."}]}, config)

Restart the program, open the same database, use the same thread_id, and the conversation continues. For Postgres you use PostgresSaver (with a connection string and a one-time setup() call that creates its tables). Async versions exist for async web servers.

What a checkpoint is

A checkpoint is a snapshot of the graph state after a step: the messages, any custom fields, and which node runs next. Every step creates a new checkpoint, linked to the one before it. This gives you three useful abilities.

  • Resume: continue after a crash, a restart or an approval pause (human in the loop).
  • Time travel: list a thread's history of checkpoints, pick an earlier one and re-run from there with different input ("what if the user had said X?").
  • Debug: inspect exactly what the state was at each step.
Python
# Real LangGraph (read-only)
for snapshot in agent.get_state_history(config):
    print(snapshot.config["configurable"]["checkpoint_id"], len(snapshot.values["messages"]))

A stand-in checkpointer with sqlite3

This stand-in saves a snapshot after each "step" into a table, and can load the latest one or an older one:

Python Stand-in: a checkpoint table in SQLite

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.

Each step is a row; "latest" is just the highest step; time travel is loading an older row. Real checkpointers do the same with richer metadata.

Short-term versus long-term memory

A thread's checkpoints are scoped to one conversation. Telling the agent "I prefer metric units" in chat 1 will not help in chat 2, because chat 2 is a different thread. Facts that should follow the user belong in a store: a key-value database organized by namespace (for example ("users", "alice")) and key.

Python
# Real LangGraph (read-only)
from langgraph.store.memory import InMemoryStore

store = InMemoryStore()
store.put(("users", "alice"), "preferences", {"units": "metric", "language": "en"})

item = store.get(("users", "alice"), "preferences")
print(item.value)                       # {'units': 'metric', 'language': 'en'}

hits = store.search(("users", "alice"))  # list items in a namespace

Pass the store to the agent (create_agent(..., store=store)) and tools can read and write it. A "remember this" tool and a "recall preferences" tool turn it into long-term memory. In production use a database-backed store (Postgres is the common choice); stores can also support semantic search over memories by embedding them, the same idea as retrieval.

A stand-in store

Python Stand-in: namespaced long-term memory shared across threads

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.

The namespace is the privacy boundary: Bob's namespace is empty even though Alice's data lives in the same store.

What to remember

Long-term memory is easy to overdo. Good candidates: stable preferences, profile facts the user gave you, recurring corrections ("never use emojis"), and outcomes of past tasks. Poor candidates: whole transcripts (that is what summaries are for), sensitive data you do not need, and guesses the model made about the user.

  • Store facts with a source, so you can show or delete them.
  • Let users view and delete their memories.
  • Decide who writes: only on explicit request, or automatically after each conversation by a background step that extracts facts.
  • Expire or revisit old facts; people change.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which package lets an agent keep its conversation across process restarts?

    Database-backed checkpointers persist each step's state so threads survive restarts.
  2. 2. Why is a preference told in chat 1 not available in chat 2?

    Thread state belongs to one thread_id; cross-thread facts need a store.
  3. 3. What does time travel with checkpoints let you do?

    Each step is saved, so you can load an earlier checkpoint and branch from it.
  4. 4. What is the purpose of a namespace in a long-term memory store?

    Namespaces such as ('users', user_id) scope reads and writes and protect privacy.

Summary

  • Persist threads with a SQLite or Postgres checkpointer; same thread_id resumes the conversation.
  • Checkpoints enable resume, time travel and debugging.
  • Use a namespaced store for long-term facts that must outlive a single thread.
  • Remember little, keep sources, and let users inspect and delete their data.

Next: Graphs, State, Nodes and Edges