Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 32 ยท LangGraph Workflows

Human in the Loop

Pause a LangGraph run for human approval with interrupt(), resume with Command, and approve, edit or reject tool calls. Runnable Python stand-ins with generators.

Some actions should never run without a person saying yes: sending an email, refunding a payment, deleting records, publishing a post. Human in the loop means the agent pauses before such a step, shows what it is about to do, waits for a decision, and then continues exactly where it stopped. This is possible because LangGraph saves the full state in a checkpoint (persistence lesson): the process can even exit while waiting, and resume hours later.

LangGraph is not available on this site, so real code is read-only. The runnable examples use Python generators as a stand-in for interrupt(): a generator can pause at yield and be resumed with a value, which is the same control flow.

interrupt and Command

Inside any node you can call interrupt(payload). LangGraph stops the run, saves a checkpoint and returns the payload to your code. Later you resume by invoking the graph with Command(resume=value); that value becomes the return value of interrupt(), and the node continues.

Python
# Real LangGraph (read-only)
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.types import interrupt, Command
from langgraph.checkpoint.memory import InMemorySaver

class State(TypedDict):
    to: str
    body: str
    status: str

def approval(state: State) -> dict:
    decision = interrupt({
        "question": "Send this email?",
        "to": state["to"],
        "body": state["body"],
    })
    return {"status": "approved" if decision == "yes" else "rejected"}

def send(state: State) -> dict:
    return {"status": "sent"} if state["status"] == "approved" else {}

builder = StateGraph(State)
builder.add_node("approval", approval)
builder.add_node("send", send)
builder.add_edge(START, "approval")
builder.add_edge("approval", "send")
builder.add_edge("send", END)
graph = builder.compile(checkpointer=InMemorySaver())     # a checkpointer is required

config = {"configurable": {"thread_id": "email-1"}}
first = graph.invoke({"to": "[email protected]", "body": "Hi!", "status": ""}, config)
print(first["__interrupt__"])           # the payload: the run is paused here

final = graph.invoke(Command(resume="yes"), config)   # same thread_id
print(final["status"])                  # sent

Rules to remember:

  • A checkpointer and a thread id are required; without them there is nowhere to save the paused state.
  • When resumed, the interrupted node starts again from its beginning, and interrupt() returns the resume value. So keep side effects before an interrupt() out of that node, or make them safe to repeat.
  • You can resume with any JSON-friendly value: a string, a boolean, an edited draft.
sequenceDiagram
    participant A as App
    participant G as Graph
    participant H as Human
    A->>G: invoke(input, thread)
    G-->>A: paused, interrupt payload
    A->>H: show payload
    H-->>A: decision
    A->>G: invoke(Command(resume=decision), thread)
    G-->>A: final result

Pause, ask, resume.

Pause and resume with a generator

A generator reproduces the control flow. The node yields its question and receives the answer back through send:

Python Stand-in: pause at yield, resume with send

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the real system the generator's local variables are replaced by a database checkpoint, which is what lets the pause survive a restart.

Review patterns

The decision does not have to be yes or no. Useful patterns:

Pattern Resume value Effect
Approve or reject "yes" / "no" Continue or cancel
Edit A corrected draft or arguments Use the human's version
Provide missing info A string The agent continues with the answer
Choose One of several options Route accordingly

An agent can also ask the human a clarifying question this way: a tool whose only job is return interrupt({"question": ...}).

Approving tool calls in agents

For create_agent, LangChain offers middleware that pauses before chosen tools run, so you do not write the graph yourself. Its configuration names which tools need approval and which decisions are allowed (approve, edit, reject). The exact class and option names have changed across versions, so check the middleware documentation for your installed release.

Python
# Real LangChain (read-only; check the docs for your version's exact names)
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langgraph.checkpoint.memory import InMemorySaver

agent = create_agent(
    model="anthropic:claude-sonnet-4-5",
    tools=[search_docs, send_email, delete_record],
    middleware=[
        HumanInTheLoopMiddleware(
            interrupt_on={"send_email": True, "delete_record": True, "search_docs": False}
        )
    ],
    checkpointer=InMemorySaver(),
)

Safe read-only tools run freely; risky ones stop for review. Put this next to the guardrails from agent errors and guardrails.

Python Stand-in: approve, edit or reject a tool call

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

UX and operations

  • Show the human exactly what will happen (recipient, amount, SQL), not the model's explanation of it.
  • Time out and escalate: a paused run that nobody answers should expire, not wait forever.
  • Log who approved what and when; that is your audit trail.
  • Keep approvals idempotent: if a user double-clicks "approve", the action must still happen once.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must you provide for interrupt() to work?

    The paused state is saved in a checkpoint, found again by thread id.
  2. 2. How do you resume an interrupted graph?

    The resume value becomes interrupt()'s return value and the node continues.
  3. 3. Why should side effects not come before interrupt() in the same node?

    On resume the node starts again from the top, so earlier side effects may run twice.
  4. 4. Which tools usually need human approval?

    Risky, hard-to-undo actions are the ones to gate.

Summary

  • interrupt() pauses a run and returns a payload; Command(resume=...) continues it.
  • Pausing relies on checkpoints, so use a checkpointer and a thread id.
  • Decisions can approve, edit, reject, answer or choose.
  • Gate risky tools, show exact details to the reviewer, and keep an audit log.

Next: Multi-Agent Systems and Supervisors