LangChain and LangGraph · Chapter 21 of 25
Persistence in Postgres
By the end of this chapter you can keep a graph's threads in Postgres so they survive a restart, pause a run in one program and finish it in another, read a thread's whole history, run a graph again from an earlier checkpoint or change the past and run on from there, and say what a checkpoint holds. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.
Why you need this
Chapter 16 gave the assistant memory with InMemorySaver, and chapter 17 used it to pause refunds for Mrunal. Both live in the program's memory. RoomRaja's server restarts with every release, and when it does, every tenant's conversation and every refund waiting for Mrunal is gone. Worse, Mrunal approves refunds from her phone, which talks to another server: the program that paused the refund is not the one that hears her answer.
Sid moves the checkpoints into Postgres, the database already running in your lab. Nothing else in the graphs changes: a checkpointer is a place to save state, and LangGraph has one for Postgres.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
durable.py: a conversation that survives a restart
Build a chat graph on MessagesState with one node, chat, that asks lab-chat with the system message You are RoomRaja's tenant assistant. followed by every message of the state, and adds the reply. START, chat, END.
Under if __name__ == "__main__":, take the tenant id and the message from the command line (python3 durable.py T1088 "My name is Pranav"). Read the database's address from the environment variable DB_URI, with postgresql:///roomraja as the default; open a PostgresSaver on it, call its setup, compile the graph with it, and run the message as a human message on the thread tenant- and the tenant id. Print the reply's text, then Messages in thread:, a space and how many messages the thread holds.
The check runs it twice, in two processes, with a database of its own in DB_URI: the second run must carry the first run's conversation.
- Task 2
resume.py: approve tomorrow, in another program
Build a graph whose state holds tenant_id, refund, status and refund_id. estimate puts the refund from roomraja.deposit_refund_estimate into the state. manager calls interrupt with a dictionary of the tenant_id and the refund; the answer is a dictionary with approved, True or False, and status becomes approved or rejected. Only when approved, pay calls roomraja.request_deposit_refund for the tenant (and roomraja.approve_refund for its refund_id when its status is pending_approval) and keeps the refund_id.
Under if __name__ == "__main__":, take an action (start, approve or reject) and a tenant id from the command line, and use a PostgresSaver on DB_URI as in task 1 with the thread refund- and the tenant id. start runs the graph for the tenant and prints Waiting:, a space and the interrupt's value. approve and reject resume it and print the tenant id, a space and the status, and for approved a colon, a space and the refund_id (T1036 approved: RF8001). The check runs each action in a new process.
- Task 3
replay.py: replay from an earlier step
helpdesk.py (given) has chapter 18's help desk graph as builder, not compiled. Compile it with a PostgresSaver on DB_URI as in task 1, run it on the thread replay-deposit for the message When will I get my deposit back?, then find in the thread's history the checkpoint whose next node is answer, and run the graph again from that checkpoint.
Print First:, a space and the first run's answer; Replayed:, a space and the replay's answer; then Checkpoints:, a space and how many checkpoints the thread's history holds after the replay.
- Task 4 · interview question
Interview: checkpoint.py, what is in a checkpoint?
Compile helpdesk.py's builder with a PostgresSaver on DB_URI as in task 1, and run it once on the thread anatomy-guests for the message Can my parents stay overnight?
Print Checkpoints:, a space and how many checkpoints the thread's history holds; then one line per checkpoint, oldest first: its step and its source from the metadata separated by a space, a colon, a space, next, a space and the next nodes joined by a comma and a space (or nothing), a semicolon, a space, values, a space and the state's keys joined the same way (or none).
The interviewer will ask what a checkpoint holds, when one is saved, and what you would need to rebuild the run from it.
What you will learn
- PostgresSaver: the checkpointer that saves to Postgres, and setup.
- A durable thread: a conversation that survives the program.
- Resuming in another process, from the thread id alone.
- History and time travel: get_state_history, replaying from a checkpoint, forking with update_state.
- What a checkpoint holds, in Python and in the tables.
More in Production
Build "Persistence in Postgres" yourself
RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.