AI Hints
Home›LangChain Tutorial›Agent Practice and Approval
LangChain · Lesson 56 of 120

Human Approval

Pausing for a yes.

⏱ 2 min read▶ Video at 1:02:31🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Some actions are too important to leave to a model alone. Cancelling an order, or issuing a refund. Human in the loop middleware pauses the agent before those tools run, and waits for a person to approve.

Human Approval
From the video: Human Approval
  • Some actions are risky
  • Cancel, refund
  • Pause for a person

Approving a cancellation

We mark the cancel order tool as needing approval. The agent needs a checkpointer, so it can pause and resume. When the customer asks to cancel, the agent stops, and returns an interrupt, describing the action. A staff member approves, and we resume with a command. Run it. First we see the pending request, then the cancellation goes through.

approval.pypython
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langchain.tools import tool
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.types import Command

@tool
def cancel_order(order_id: str) -> str:
    """Cancel a customer's order."""
    return f"{order_id} cancelled"

agent = create_agent("google_genai:gemini-flash-lite-latest", tools=[cancel_order],
    checkpointer=InMemorySaver(),
    middleware=[HumanInTheLoopMiddleware(interrupt_on={"cancel_order": True})])
cfg = {"configurable": {"thread_id": "t1"}}
out = agent.invoke({"messages": [{"role": "user", "content": "Cancel order RB-1234."}]}, cfg)
print("waiting for:", out["__interrupt__"][0].value["action_requests"][0]["args"])
out = agent.invoke(Command(resume={"decisions": [{"type": "approve"}]}), cfg)
print(out["messages"][-1].text)
Output
waiting for: {'order_id': 'RB-1234'}
Order RB-1234 has been successfully cancelled.

Decisions a human can make

The reviewer has several choices. Approve runs the tool as planned. Edit changes the arguments first, like fixing an order number. Reject stops the tool, with a message explaining why. Together, they let people stay in control of anything risky.

keyvalue
approverun as planned
editchange the arguments
rejectdon't run it
Needsa checkpointer

Summary

Human in the loop middleware pauses risky tools for approval, and a command resumes the agent with the decision. Next, a quiz on tools and agents.

Key takeaways
  • interrupt_on={tool: True}
  • __interrupt__
  • Command(resume=...)
Next up: Agents Quiz — test yourself