Session 8· 08· 20 min

Human-in-the-Loop

What you'll learn
  • Add HumanInTheLoopMiddleware to require approval for dangerous tools
  • Handle approve, edit, and reject decisions
  • Understand the interrupt/resume flow with checkpointer

Why this matters

When an agent can delete files, send emails, or execute SQL, you want a human to approve before it acts. HumanInTheLoopMiddleware pauses the agent at dangerous tool calls and waits for your decision: approve, edit the args, or reject.

The interrupt flow
Agent
wants to delete
HITL
pauses
Human
reviews
Approve?
yes/no/edit
Resume
agent continues
08_hitl.py
from langchain.agents.middleware import HumanInTheLoopMiddleware  ①

agent = create_agent(
    model=f"openai:{model_name}",
    tools=[read_file, delete_file],
    middleware=[
        HumanInTheLoopMiddleware(
            interrupt_on={
                "read_file": False,                               ②
                "delete_file": True,                              ③
            },
        ),
    ],
    checkpointer=InMemorySaver(),                                 ④
)

# Agent pauses when it tries to delete
result = agent.invoke(
    {"messages": [...]},
    config=config,
    version="v2",                                                 ⑤
)

# Approve the pending action
agent.invoke(
    Command(resume={"decisions": [{"type": "approve"}]}),         ⑥
    config=config,
    version="v2",
)
HumanInTheLoopMiddleware inspects tool calls before execution.
False = auto-approve (no interruption for reads).
True = require approval (pauses on delete).
Checkpointer is REQUIRED — state must persist during the pause.
version="v2" enables the interrupt/resume API.
Command(resume=...) sends your decision. Options: approve, edit, reject.
$ python 08_hitl.py
Checkpointer is mandatory
Without a checkpointer, the agent's state is lost during the pause. Interrupt/resume only works with persistence.
Knowledge Check
What are the three human decisions available during an interrupt?
Recap — what you just learned
  • HumanInTheLoopMiddleware pauses the agent at dangerous tool calls
  • Three decisions: approve (run as-is), edit (modify args), reject (cancel + feedback)
  • Requires a checkpointer + version="v2"
  • interrupt_on maps tool names to True (require) or False (auto-approve)
Next up: 09 — Multi-Agent Handoff