15. Persistence

Chapter 15 of 18 · 20 min

Persistence beyond development checkpointing requires a durable storage backend. LangGraph's checkpointer interface supports multiple backends: MemorySaver (development), SqliteSaver (single-machine production), and PostgresSaver (multi-machine production). The same code pattern works across all backends—swap the checkpointer and the rest of your code is unchanged.

# Development
from langgraph.checkpoint.memory import MemorySaver
checkpointer = MemorySaver()

# Single-machine production
from langgraph.checkpoint.sqlite import SqliteSaver
checkpointer = SqliteSaver.from_conn_string("./checkpoints.db")

# Multi-machine production
from langgraph.checkpoint.postgres import PostgresSaver
checkpointer = PostgresSaver.from_conn_string(os.environ["DATABASE_URL"])

To persist session state across server restarts with SQLite:

app = builder.compile(checkpointer=SqliteSaver.from_conn_string("./sessions.db"))

# Save a session
config = {"configurable": {"thread_id": "user_42_session_1"}}
app.invoke({"task": "research the new LLM releases"}, config=config)

# Resume on next server start
resume_config = {"configurable": {"thread_id": "user_42_session_1"}}
app.invoke(Command(resume={}), config=resume_config)

For true long-term memory beyond the current session, store final state artifacts in a dedicated database (Postgres, SQLite, or a vector store) and read them in at the start of new sessions:

def load_historical_context(thread_id: str) -> dict:
    conn = sqlite3.connect("sessions.db")
    cursor = conn.execute(
        "SELECT state FROM sessions WHERE thread_id = ? ORDER BY checkpoint_id DESC LIMIT 1",
        (thread_id,)
    )
    row = cursor.fetchone()
    return json.loads(row[0]) if row else {}

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Compile a graph with SqliteSaver. Run it with a thread_id, shut down and restart Python, then resume the same thread from disk. Verify the checkpoint history is present after restart.