Day 4: Production Patterns - Persistence and Human-in-the-Loop
Source: Dev.to
Persistence (Memory)
Standard scripts lose all state once they finish. LangGraph provides a built‑in Checkpointer that saves the workflow state to a database (or in‑memory store) after every step, enabling:
- Long‑running conversations – e.g., ChatGPT‑style threads.
- Resume on failure – reload the state after a crash and continue.
- Time travel – jump back to a previous step and explore alternative paths.
import { MemorySaver } from "@langchain/langgraph";
// In production you’d typically use PostgresSaver or RedisSaver
const checkpointer = new MemorySaver();
const app = workflow.compile({ checkpointer });
// Identify the session with a thread_id
const config = { configurable: { thread_id: "session-123" } };
Human‑in‑the‑Loop
Sometimes you need a human to review or edit an agent’s output before it proceeds (e.g., reviewing a drafted tweet).
Interrupting the graph
const app = workflow.compile({
checkpointer,
// Stop execution right before the "human_review" node
interruptBefore: ["human_review"],
});
Execution flow (Mermaid diagram)
graph TD
Start[__start__] --> Agent
Agent --> HumanReview[Human Review]
HumanReview -->|Interrupt| Pause((PAUSE))
Pause -->|Resume| End[__end__]
Step‑by‑step
Run
The user requests a tweet. The agent drafts it, then the graph pauses at human_review.
Pause
The script exits or yields, leaving the draft in the current state.
Inspect
The UI (or user) fetches the state to display the draft.
const state = await app.getState(config);
console.log("Draft:", state.values.messages.at(-1).content);
Resume
After the user approves or edits the draft, the graph is resumed.
import { Command } from "@langchain/langgraph";
// Resume execution with a command
await app.invoke(
new Command({ resume: "Approved" }),
config
);
Full‑stack Agentic Architecture Recap
| Day | Focus |
|---|---|
| Day 1 | Grounded knowledge with Retrieval‑Augmented Generation (RAG) |
| Day 2 | Decision making using StateGraph |
| Day 3 | Specialization via multi‑agent routing |
| Day 4 | Reliability through persistence and human control |
These patterns form a blueprint for modern AI applications: you’re no longer just prompting models—you’re architecting robust, cognitive systems.