"""A three-node LangGraph agent whose state survives restarts in SQLite. triage -> draft_reply -> approve (pauses for a person) -> END Every step is saved by SqliteSaver under a thread_id. Stop the process at the approval pause, start it again with the same thread_id, and the run continues from the saved checkpoint. The model is a stand-in function so the kit runs with no API key. Replace fake_llm() with a real chat model call when the flow works for you. Run: python agent.py start --thread demo-1 --message "My invoice is wrong" python agent.py resume --thread demo-1 --decision approve """ import argparse import os from typing import TypedDict os.environ.setdefault("LANGGRAPH_STRICT_MSGPACK", "true") from langgraph.checkpoint.sqlite import SqliteSaver # noqa: E402 from langgraph.graph import END, START, StateGraph # noqa: E402 from langgraph.types import Command, interrupt # noqa: E402 DB_PATH = "checkpoints.sqlite" class State(TypedDict, total=False): message: str category: str draft: str decision: str sent: bool def fake_llm(prompt: str) -> str: """Stand-in for a model call: fixed rules, no network.""" text = prompt.lower() if prompt.startswith("CLASSIFY:"): return "billing" if ("invoice" in text or "charge" in text) else "general" return "Thanks for writing in. A teammate will check this and reply today." def triage(state: State) -> State: return {"category": fake_llm("CLASSIFY: " + state["message"])} def draft_reply(state: State) -> State: return {"draft": fake_llm("REPLY: " + state["message"])} def approve(state: State) -> State: # interrupt() saves a checkpoint and stops the run here. The value # passed to Command(resume=...) comes back as the return value. decision = interrupt({"category": state["category"], "draft": state["draft"]}) return {"decision": decision, "sent": decision == "approve"} def build(checkpointer): g = StateGraph(State) g.add_node("triage", triage) g.add_node("draft_reply", draft_reply) g.add_node("approve", approve) g.add_edge(START, "triage") g.add_edge("triage", "draft_reply") g.add_edge("draft_reply", "approve") g.add_edge("approve", END) return g.compile(checkpointer=checkpointer) def main(): p = argparse.ArgumentParser() p.add_argument("action", choices=["start", "resume"]) p.add_argument("--thread", required=True) p.add_argument("--message", default="My invoice is wrong") p.add_argument("--decision", choices=["approve", "reject"], default="reject") a = p.parse_args() config = {"configurable": {"thread_id": a.thread}} with SqliteSaver.from_conn_string(DB_PATH) as saver: graph = build(saver) if a.action == "start": out = graph.invoke({"message": a.message}, config) else: out = graph.invoke(Command(resume=a.decision), config) if "__interrupt__" in out: print("PAUSED for approval:", out["__interrupt__"][0].value) else: print("FINISHED:", {k: out[k] for k in ("category", "decision", "sent")}) print("next nodes:", graph.get_state(config).next) if __name__ == "__main__": main()