LangChain and LangGraph · Chapter 17 of 25

Human in the loop

By the end of this chapter you can make a graph stop in the middle of a run and wait for a person, read what it is waiting for, resume it with the person's answer, let that person approve, reject or change what the graph was about to do, and explain why a graph can only pause when it is checkpointed. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: LangGraph
  • 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

Tanvi wants the assistant to run whole move-outs: the tenant gives notice, the assistant works out the deposit refund and pays it. Faisal agrees, with one condition from the policy, rule 2.4: "A refund above ₹25,000, or any deduction above ₹5,000, needs the property manager's approval." A refund of ₹30,000 must not go out until Mrunal says yes.

Everything you have built so far runs from start to end in one go: a graph run takes a second, and the program finishes. Mrunal answers in minutes or hours, sometimes the next morning. The run cannot keep a Python program waiting all night. It has to stop, be saved, and continue later with her answer, maybe in another program. LangGraph has two small pieces for this, interrupt and Command, and they rest on something you already know from chapter 16: the checkpointer.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    pause.py: a refund that waits for the manager

    Policy 2.4: a deposit refund above 25,000 rupees needs the property manager's approval. Build a graph whose state (a TypedDict) holds tenant_id, refund and decision, with two nodes: estimate puts the refund from roomraja.deposit_refund_estimate(tenant_id) into the state; manager calls interrupt with a dictionary of the tenant_id and the refund, and returns what interrupt gives back as decision. START, estimate, manager, END. Compile it with an InMemorySaver as graph, at the top level of the file.

    Under if __name__ == "__main__":, run it for T1036 on the thread refund-T1036 and print two lines: Manager sees: and the interrupt's value, then Paused at: and the next field of the thread's state. The check also runs your graph for other tenants and resumes it.

  2. Task 2

    decide.py: approve, reject, or no manager at all

    Build a graph whose state holds tenant_id, refund_id, refund, status, note and reply. request calls roomraja.request_deposit_refund(tenant_id) and keeps its refund_id, refund and status. After it, go to manager only when the status is pending_approval, otherwise to tell_tenant. manager calls interrupt with a dictionary of the refund_id, tenant_id and refund; the manager answers with a dictionary holding approved (True or False) and maybe a note; pass them to roomraja.approve_refund and keep the status and note it returns. tell_tenant asks lab-chat with the system message You are RoomRaja's tenant assistant. Summarise the update for the tenant in two sentences. and the human message Your deposit refund RF8001 of 30000 rupees is approved. (that refund's id, amount and status), followed, when there is a note, by a space, Your property manager's note: and the note; it keeps the reply's text as reply. Compile with an InMemorySaver as graph.

    Under if __name__ == "__main__":, use one thread per tenant (refund- and the tenant id): approve T1036, reject T1056 with the note Room damage must be checked first., and run T1002, which needs no approval. Print one line per tenant: the tenant id, a space, the final status, a colon, a space and the reply.

  3. Task 3

    edit.py: edit, then approve

    Start from task 2's graph. The manager now answers with a dictionary whose action is approve, edit or reject, and maybe a note. approve approves the refund as it is; edit approves it with its refund in place of the state's; reject rejects it. The manager node returns the refund (edited or not), the status from roomraja.approve_refund and the note, and tell_tenant tells the tenant the refund in the state.

    Under if __name__ == "__main__":, on the thread refund-T1012, run T1012 and print Manager sees: and the interrupt's value; then resume with an edit to 27000 with the note 2000 rupees for a broken chair. and print T1012, a space, the status, a space, the state's refund, a colon, a space and the reply. The check resumes your graph with each action for other tenants.

  4. Task 4 · interview question

    Interview: fix_me.py, the refund that cannot resume

    Mrunal's approval program pauses T1036's refund and prints what the manager sees, then resumes it with her decision. Run it: the pause looks fine, then resuming stops with an error.

    Fix it so that start(tenant_id) pauses a tenant's refund and decide(tenant_id, approved) resumes that same refund, for any tenant, printing Waiting: and the interrupt's value, then Decided:, the status and the refund. Keep graph, start and decide. The check calls start and decide for other tenants.

    The interviewer will ask why a graph must be checkpointed to pause, and what the thread id does.

What you will learn

  • interrupt: a node stops the graph and says what it is waiting for.
  • What a paused run looks like: the __interrupt__ key and the thread's state.
  • Command(resume=...): continuing the run with the person's answer.
  • Why a pause needs a checkpointer and a thread id.
  • The node runs again: where to put the work around a pause.

More in LangGraph

Build "Human in the loop" 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.