Courses / Agentic AI from Scratch (Python)
Session 25 of 40
Saved progress: checkpoints, threads that survive a restart, and durable execution
Notes
How to Save and Resume a LangGraph Conversation With a SQLite Checkpointer and a Thread ID
Agentic AI Foundations · Session 25 — Saved progress: a checkpointer, a thread id and a conversation that survives a restart
Before you start: what do I need before I save a LangGraph conversation to a file?
You need the code folder for this session open in your terminal. It holds saved_graph.py, the carried-over files it uses (tools.py, helpdesk_rag.py, search.py, build_index.py, load_policies.py, make_tickets_db.py), the prompts in model_prompts/, the sample data in data/ and one build prompt in prompts/. Install the libraries with pip install langchain langchain-openai langchain-chroma langchain-community langchain-text-splitters langgraph langgraph-checkpoint-sqlite; the SQLite checkpointer is a separate package from LangGraph itself. Set LLM_BASE_URL, LLM_API_KEY, LLM_MODEL and EMBED_MODEL as in earlier sessions, then build the two things the graph reads with python build_index.py and python make_tickets_db.py. The checkpoint file copilot_checkpoints.db is not in the lesson files: the first run creates it. Recap of what this session assumes: the Copilot is a graph whose nodes share a state, and a conditional edge routes each question to the policy, ticket or fallback path (sessions 23 and 24). Have paper or a notes app ready for the Flashcards and the Model answer.
Summary: what are LangGraph checkpoints and thread ids?
A graph that keeps its state only in the running program forgets everything when the program stops. A checkpointer fixes that by saving the graph’s state after each step as a checkpoint, and a thread id names one conversation so that the same id later loads the same saved state. In this session the routed Copilot of session 24 is compiled with a SQLite checkpointer that writes to a file, gets a history list that gains one line per finished turn, and uses that history in its first node to turn a short follow-up into a full question. You run it as two separate programs on one thread id to see the first turn already saved, then run a different thread id to see it start empty. The same saves are what let a graph pause and resume in the next session.
Key concepts: LangGraph checkpoint and thread terms explained in plain English
| Term | Definition |
|---|---|
| Checkpoint | A snapshot of the graph’s state, saved at a step boundary so it can be loaded later. |
| Checkpointer | The component passed to compile that saves checkpoints; an in-memory one for experiments and a SQLite or Postgres one that keeps them in a database. |
SqliteSaver |
The checkpointer that writes checkpoints to a SQLite file; it comes in the separate langgraph-checkpoint-sqlite package. |
InMemorySaver |
The checkpointer that keeps checkpoints in the program’s memory, so they are lost when the program restarts. |
| Thread | The series of checkpoints that make up one conversation. |
| Thread id | The label in the config that tells the checkpointer which saved conversation to load or extend. |
get_state |
The graph method that returns the latest saved state for a thread, without running anything. |
| Reducer | A rule for how a state key is updated; operator.add adds new list items to the saved ones instead of replacing the list. |
history |
The state key in saved_graph.py holding one line for each finished turn. |
| Super-step | One round of the graph in which the scheduled nodes run; a checkpoint is written at each boundary. |
| Idempotent step | A step that has the same effect when it runs twice. |
Explanations: how do checkpoints and thread ids work in LangGraph?
What does this session add to the Copilot?
Session 24’s graph decides which path a question takes, but it keeps nothing once the program ends. Today it gains a file to remember in. In the drawing below, the parts marked NEW are today’s:
START --> receive --> sort --> policy_node ---+
^ | --> ticket_node --+--> save_turn --> END
reads the saved | --> fallback_node -+ |
history (NEW) +- session 24's routing |
adds a line to history (NEW)
Compiled with a checkpointer (NEW): the state is saved to a file.
The three paths are unchanged. Today adds a way in (receive, which now reads the saved history), a way out (save_turn, which adds to it) and a file (copilot_checkpoints.db). Think of a help desk with a filing cabinet: each caller has a folder, and a clerk files a copy of the case after each round of work.
What is a checkpointer, and how do you add one?
Failure first. An employee asks about carrying over leave days and gets an answer. The program is then restarted, and the employee types “And who do I ask about it?”. The word “it” now points at nothing: the earlier question lived only in the old program’s memory, so a model reading this message alone has to guess or ask again.
Think of a video game’s save slot: you quit, come back and carry on from the last save. Or a clerk who files a copy of the case after each round of work, so another clerk can pick up the folder tomorrow. A checkpointer does this for a graph: it saves the graph’s state after each step, and each saved snapshot is a checkpoint. LangGraph’s documentation says it creates a checkpoint at each super-step boundary; in saved_graph.py each node is its own step, so a checkpoint is written as each node finishes.
Adding one is a single change to how the graph is compiled:
CHECKPOINT_FILE = Path(__file__).parent / "copilot_checkpoints.db"
saver = SqliteSaver(sqlite3.connect(CHECKPOINT_FILE,
check_same_thread=False))
graph = builder.compile(checkpointer=saver)
sqlite3.connect opens a database file next to the script, and the first run creates it. SqliteSaver writes the checkpoints into that file. The nodes themselves do not change: they contain no saving code.
There is a choice of saver. InMemorySaver stores checkpoints in RAM, and when the process restarts, they are all lost. SqliteSaver writes to a file, so a restart finds them again. The documentation describes the SQLite one as suited to experiments and local workflows, and the Postgres one as the choice for production.
| Question | InMemorySaver |
SqliteSaver |
|---|---|---|
| Where are checkpoints kept? | In the program’s memory | In a file on disk |
| After the program restarts | They are gone | They are still in the file |
| Good for | A quick experiment | Local work you want to keep |
| Installed with | LangGraph itself | pip install langgraph-checkpoint-sqlite |
What is a thread id, and how do you read a saved thread?
A save slot needs a name, or conversations would overwrite one another. Picture a patient number on a folder: it brings out one patient’s pages and nobody else’s. Or two save slots, priya and sam, in one game file. A thread id is that name for a conversation. The documentation says the checkpointer uses it as the primary key for storing and retrieving checkpoints, so the same id later loads the same saved state, and a new id starts with empty state.
The id travels in the config, and a graph that has a checkpointer must be given one:
config = {"configurable": {"thread_id": thread_id}}
saved = graph.get_state(config).values.get("history", [])
graph.stream({"question": question}, config, stream_mode="updates")
graph.get_state(config) returns the latest saved state for that thread, and .values is the state itself, so history is the list of saved turns. Because run reads the thread before it runs the graph, you can see the saved conversation come back. The same config is then passed to graph.stream, which runs the question on that thread. If you forget the thread id, the call raises an error that names the missing thread_id key.
One caution from the documentation: reusing a thread id does not by itself make an unfinished run continue. It loads the saved state; whether the new message starts a fresh turn on that state is up to the graph.
What does the Copilot save, and how does it use it?
A checkpointer saves whatever is in the state, so the state must hold what the next turn needs. saved_graph.py adds a history key and a last node that fills it:
class CopilotState(TypedDict):
question: str
kind: str
answer: str
history: Annotated[list, operator.add]
def save_turn(state):
return {"history": [f"question: {state['question']} | "
f"answer: {state['answer']}"]}
The reducer operator.add matters. Without it, each turn would replace the list; with it, a new one-line list is added to the saved lines, so the history grows turn after turn. The policy, ticket and fallback paths all join save_turn before the end, so each finished turn is recorded.
Saving is half the job. The first node reads the history back:
def receive(state):
question = " ".join(state["question"].split())
earlier = state.get("history", [])
if earlier:
turns = "\n".join(earlier[-6:])
question = model.invoke([("system", standalone_text),
("human", f"Earlier turns:\n{turns}\n\n"
f"New message: {question}")]).content.strip()
return {"question": question}
With no history, the question passes through, only tidied. With history, one model call, guided by model_prompts/standalone.md, rewrites the follow-up as one complete question, or hands back a question unchanged if it already stands alone. It sees only the last six saved lines. The rewritten question then goes to sort and down the usual path, so the policy path answers a full question instead of “who do I ask about it?”.
Think of a receptionist who glances at the case folder before taking the next call, or a doctor who reads yesterday’s notes before today’s visit. The notes only help because someone reads them.
How do you run it twice, and what should you look for?
Build the data first, then run two separate programs on one thread:
python build_index.py
python make_tickets_db.py
python saved_graph.py priya "How many unused annual leave days can I carry over to next year?"
python saved_graph.py priya "And who do I ask about it?"
The program ends between the two commands, so nothing is left in its memory. In the second run, look for the run listing the first turn as already saved before it does anything else, and for the receive step showing the follow-up turned into a complete question. Then try a different thread:
python saved_graph.py sam "And who do I ask about it?"
Look for the run reporting no saved turns for sam: the thread is new, so there is nothing to rewrite the follow-up from. Run priya again afterwards and her conversation is still there, untouched by sam.
What about a run that stops partway, and why must writing steps be safe to repeat?
saved_graph.py saves at the end of finished steps, which is also what lets a run be continued. LangGraph’s documentation describes durable execution: if a run fails, invoking the graph again with no new input and the same config loads the latest checkpoint and retries the unfinished work. It also describes three durability modes: exit saves only when the run exits, so a crash mid-run cannot be recovered; async saves while the next step runs, with a small risk that a checkpoint is missed; sync writes each checkpoint before the next step starts, with the most durability and some slowdown. This lesson’s code uses the default and does not set a mode.
The documentation also warns that a retried node runs its whole function again, which gives at-least-once behaviour. A step that creates a ticket could therefore create it twice. Picture a clerk who re-stamps a form after a power cut: harmless for a stamp that can be repeated, costly for a payment. This is why the ticket-creating tool, create_ticket, is a problem for the next session, which puts a person’s approval in front of it and keeps the writing in its own node.
What goes wrong without saved state, and what does it mean for the design?
Before the design table, three situations you will meet:
- A follow-up gets a vague reply after a deploy. The graph used no checkpointer, or the in-memory one, so the restart wiped the earlier turns.
- Priya’s question gets an answer about Sam’s topic. Both used the same thread id, so they shared one saved conversation.
- A returning user starts with no context. The program passed a fresh thread id, and a new id starts empty.
What these mean for the design:
| What you learned | The design decision | Where it shows |
|---|---|---|
| A restart wipes memory-only state | Save to a file | SqliteSaver in saved_graph.py |
| One id means one conversation | Derive the id from the user and keep it stable | thread_id in run |
| Saved state is not used by itself | Read history in a node |
receive and standalone.md |
| The checkpointer saves what is in the state | Put each finished turn into it | save_turn |
Basic assignment: run saved_graph.py twice on one thread and once on a new one. Write down, for each run, how many turns were already saved and what receive did with the question.
Project step: change one thing and see. Delete copilot_checkpoints.db and run the follow-up again: what does the Copilot do? Replace SqliteSaver(...) with InMemorySaver() from langgraph.checkpoint.memory, run twice and compare. Ask three questions on one thread, then print graph.get_state(config).values["history"]. Keep only the last two turns in receive and decide when that matters. The build prompt is prompts/1-write-saved-graph.md. Stop when you can say which of the three things (file, thread id, reading node) each result depended on.
Flashcards: test yourself on checkpoints and thread ids
Say the answer aloud before revealing it. Speaking is a different skill from reading, so practise both.
1. What does a checkpointer do?
It saves the graph's state after each step as a checkpoint, so the state can be loaded again later. In `saved_graph.py` it is passed to `compile` as `checkpointer=saver`.2. What is a thread id?
The label that names one conversation. The checkpointer uses it as the key for saving and loading, so the same id loads the same saved state.3. Why did a chat lose its place after a restart?
The state lived only in the program's memory, or the graph used the in-memory saver, which keeps checkpoints in RAM. Both are gone when the process restarts; a SQLite file is still there.4. What does the config need to carry when a graph has a checkpointer?
A `thread_id` inside `configurable`. Without it the checkpointer has no key to save or load the state under, and the call raises an error.5. What happens when you use a new thread id?
It starts with empty state, because no checkpoints exist under that id. That is why the follow-up on thread `sam` has nothing to be rewritten from.6. What does `operator.add` do on `history`?
It is the reducer: new items are added to the saved list instead of replacing it. That is why the history grows by one line each turn.7. Why is `save_turn` the last node on all three paths?
The checkpointer saves what is in the state, so each finished turn must be added to `history` before the run ends. Joining all three paths to it records policy, ticket and fallback turns alike.8. What does `receive` do with the history?
If the thread has earlier turns, it makes one model call that rewrites a short follow-up as a complete question. With no history, the question passes through only tidied.9. What does `get_state(config).values` give you?
The latest saved state for that thread, as a dictionary. It lets you read the saved `history` without running the graph.10. Why should a writing step be safe to repeat?
A retried or resumed node runs its whole function again, so a write before a failure can happen twice. A check before writing, or a stable key, stops a duplicate.Model answer: how do I explain saving and resuming a LangGraph conversation in an interview?
The core question this session answers: “A teammate asks: a user’s chat vanished when we restarted the server. How do we fix it?”
Four beats a teammate listens for, in this order: save each step · one thread per conversation · put the turn in the state · read it back. Skip any of these four and the answer is incomplete.
- Save each step: compile with a checkpointer that writes to a file.
- One thread per conversation: the thread id names which one to load.
- Put the turn in the state: a last node adds each finished turn to
history. - Read it back: a node uses the history, and a new id starts empty.
The full answer, said aloud:
(Save each step) “I compile the graph with a SQLite checkpointer, so the state is written to a file after each step instead of being held only in memory.
(One thread per conversation) Each conversation has a thread id in the config. The same id loads the same saved state, and a different id is a separate conversation.
(Put the turn in the state) A last node adds each finished turn to a
historylist, so the saved state holds what the next turn needs.(Read it back) The first node reads that history to turn a follow-up into a full question. A new thread id starts empty, which is how two users stay apart.“
Missing a beat is a failure state. The follow-up question lands where you went quiet: leave out the last beat and the next question is how the saved turns are actually used. Practise until the four come out without prompting, then stop talking and let the questions come.
Why this matters: why do saved threads matter at work?
In a conversation with a teammate or manager: “what happens when we deploy?” is a routine question for any assistant with users. A saved thread lets you answer without apologising.
On the job: support chats, multi-step requests and approvals last longer than one program run. Saved checkpoints let them survive restarts, and thread ids keep one person’s conversation out of another’s.
In the rest of this course: session 26 uses the same saves to pause the graph for a person’s approval, session 27 adds memory that outlives one thread, and session 37 covers deploying the Copilot.
After this unit: what do I do after this lesson, and what comes next?
Assignment. Run saved_graph.py twice on one thread and once on a new one, and say what each run showed. The brief for the project work, changing one thing and seeing the effect, is the project file. Keep saved_graph.py and your checkpoint file: the next session builds on them.
What’s next. Session 26 pauses the graph for a person’s approval: the ticket path drafts a request, stops with interrupt(), and resumes on the same thread with the person’s answer to create the ticket or not.
Want help building this with someone?
Tell us what you are working on and we will write back about a mentor-led cohort, live sessions or 1:1 training. It is only an enquiry: you choose whether to go further.
Ask about mentor-led trainingTechEazy Consulting training material — not affiliated with or endorsed by OpenAI, Anthropic, LangChain or any other company named in this course.