Agents · Module 05
Human-in-the-Loop & Persistence
Pause an agent for a person to approve or correct it, and keep its state across restarts: interrupts, checkpointers, threads, state edits and time travel.
Học xong bạn làm được
- Pause and resume a graph with interrupts
- Choose between in-memory, SQLite and Postgres checkpointers and persist state with SQLite
- Manage threads for separate conversations
- Edit state and replay from an earlier checkpoint
Bài học
- Why human-in-the-loop?
Human review is needed for high-stakes decisions (critical, financial, legal, irreversible), for quality control (accuracy, brand, tone, errors), for compliance (GDPR, HIPAA, SOC 2, ISO 27001, PCI-DSS, audit trails) and for user feedback. Typical use cases are content approval, financial transactions, medical recommendations, legal advice and code deployment.
- Interrupts in LangGraph
An interrupt pauses a graph at a set point, saves its whole state, waits for a human, and resumes from the checkpoint. Interrupts are set at compile time with interrupt_before and interrupt_after, which need a checkpointer, and a conditional edge can send only some runs to the approval node. A run goes through five stages - run until the interrupt, save a checkpoint, return control, human review, resume with the same config.
- Persistence and checkpoint savers
Persistence lets workflows run for days, survive crashes, keep an audit trail and continue conversations across sessions. LangGraph has four kinds of checkpoint savers - InMemorySaver for development, SQLiteSaver for small apps, PostgresSaver for production and custom savers built on BaseCheckpointSaver. InMemorySaver checkpoints after every node, isolates threads, and loses everything on restart.
- SQLite and Postgres checkpointers
SqliteSaver stores checkpoints in a database file, so they survive restarts, can be backed up and keep a full history per thread. Its schema has a checkpoints table and a writes table. PostgresSaver is the production checkpointer - created from a connection string or a connection pool, it scales to thousands of threads, is shared by many app instances and has ACID durability.
- Thread management
A thread_id in the config identifies one conversation - a simple id, one built from the user and session ids, or one with metadata. Separate threads keep parallel conversations, user sessions and their state isolated. A thread is created by its first call, continued with the same id, inspected with get_state_history, and archived or deleted when it ends.
- State updates and checkpoint history
update_state changes the saved state of a thread - one field or several, merged through the reducers - and as_node makes the update count as if a given node had run, which can skip an approval. get_state_history lists every checkpoint with its id, parent, values, next nodes and metadata; get_state with a checkpoint_id reads one, and streaming from it replays the run from there.
- Memory patterns, practice and summary
Short-term memory lives in the checkpointer - the current conversation and session state, fast but thread-scoped and ephemeral. Long-term memory - preferences, summaries, learned patterns, the user profile - lives in external storage such as a vector store, recalled and written by nodes. Two exercises add a FAISS cache tool and confirmation interrupts before ticket and booking tools.
- Assignment: Human-in-the-Loop & Persistence
Extend the multi-agent chatbot with human confirmation before sensitive tools, FAISS response caching and a SQLite checkpointer.