AI Hints
Home›LangGraph Tutorial›Persistence and Deployment
LangGraph · Lesson 106 of 120

Saving to Disk

SQLite checkpointer.

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

The in memory saver forgets everything when the program stops. Real apps restart all the time. A database checkpointer keeps conversations, and paused approvals, safe across restarts. SQLite is the easiest one to start with.

Saving to Disk
From the video: Saving to Disk
  • Memory forgets on restart
  • Apps restart often
  • Save to a database

Surviving a restart

The make graph function builds the visit counter with a SQLite saver, pointing at a file. We build it once, and visit. Then we build a brand new graph object, as if the program had restarted, and visit again on the same thread. Run it. The count continues from the file, so the second graph sees two visits.

sqlite_saver.pypython
import operator, sqlite3
from typing import Annotated, TypedDict
from langgraph.checkpoint.sqlite import SqliteSaver
from langgraph.graph import StateGraph, START

class State(TypedDict):
    visits: Annotated[int, operator.add]

def make_graph():
    saver = SqliteSaver(sqlite3.connect("shop.db", check_same_thread=False))
    b = StateGraph(State); b.add_node("visit", lambda s: {"visits": 1}); b.add_edge(START, "visit")
    return b.compile(checkpointer=saver)

config = {"configurable": {"thread_id": "sam"}}
print("before restart:", make_graph().invoke({}, config)["visits"])
print("after restart: ", make_graph().invoke({}, config)["visits"])
Output
before restart: 1
after restart:  2

Checkpointers

There's a checkpointer for every stage. In memory, for tests, and learning. SQLite, for small apps on one machine. And Postgres, for production, with many servers sharing the same conversations. They all work the same way.

CheckpointerUse for
InMemorySavertests, learning
SqliteSaversmall apps, one machine
PostgresSaverproduction, many servers

Summary

A SQLite checkpointer saves state to a file, so conversations and approvals survive restarts. Postgres is the production choice. Next, serving a graph as a web API.

Key takeaways
  • SqliteSaver
  • Survives restarts
  • Postgres in production
Next up: Serving a Graph — FastAPI