Recipe · LangChain

LangChain human in the loop, with the humans included

To add a human in the loop to a LangChain agent, put HumanInTheLoopMiddleware on create_agent, list the tool that needs a person in interrupt_on, and resume with Command(resume={"decisions": [...]}) once you have an answer. LangChain gives you the pause. It does not give you the person. This recipe gives the agent an ask_human tool, sends each paused call to verified US people through GetABrain, and resumes with what they said.

GetABrain: human feedback for any AI project, from verified real people. How answers are verified

  1. 1.Get a test key ($5 sandbox credit)

    Sign up as a requestor and mint a test-mode key. It is a normal gab_k_ / gab_s_ pair that returns simulated answers, paid from the $5 sandbox credit first, so you can wire the whole loop before any real person sees a question.

  2. 2.Install

    LangChain v1, LangGraph (for the checkpointer) and the GetABrain Python SDK.

    pip install langchain langgraph getabrain
  3. 3.Give the agent an ask_human tool and gate it

    The middleware checks every tool call against interrupt_on. Allowing only the respond decision makes ask_human a pure 'ask a person' tool: its body never runs, the human's reply becomes the tool result. A checkpointer is required, because the agent's state has to survive the pause.

    from langchain.agents import create_agent
    from langchain.agents.middleware import HumanInTheLoopMiddleware
    from langchain.tools import tool
    from langgraph.checkpoint.memory import InMemorySaver
    
    @tool
    def ask_human(question: str, options: list[str]) -> str:
        """Ask real people to pick one of the options. A human answers this."""
        return ""
    
    agent = create_agent(
        model="anthropic:claude-sonnet-5",
        tools=[ask_human],
        middleware=[
            HumanInTheLoopMiddleware(
                interrupt_on={"ask_human": {"allowed_decisions": ["respond"]}},
            ),
        ],
        checkpointer=InMemorySaver(),  # use a persistent saver in production
    )
  4. 4.Run until it pauses, ask GetABrain, resume

    With version="v2", invoke() returns the pending calls on result.interrupts. Turn the tool call into a multiple-choice question, wait for 3 people, and resume the same thread_id with a respond decision. The agent reads the majority as the tool's answer and keeps going. Other answer formats are in query types.

    from getabrain import GetABrain
    from langgraph.types import Command
    
    gab = GetABrain(api_key="gab_k_...", api_secret="gab_s_...")
    config = {"configurable": {"thread_id": "invoice-subject-1"}}
    
    result = agent.invoke(
        {"messages": [{"role": "user", "content": "Pick a subject line for the March invoice email. Ask real people."}]},
        config=config, version="v2",
    )
    args = result.interrupts[0].value["action_requests"][0]["args"]
    options = [{"id": chr(97 + i), "text": t} for i, t in enumerate(args["options"])]
    
    query = gab.queries.create(
        type="multiple_choice",
        title="Which subject line would you open?",
        content_data={"question": args["question"], "options": options},
        required_responses=3,
        bid_amount_cents=25,
    )
    answers = gab.queries.wait_for_responses(query["id"], min_responses=3, timeout_s=900)
    picks = [a["response_data"]["selected_ids"][0] for a in answers]
    winner = max(set(picks), key=picks.count)
    text = next(o["text"] for o in options if o["id"] == winner)
    
    agent.invoke(
        Command(resume={"decisions": [{"type": "respond", "message": f"{picks.count(winner)} of {len(picks)} people picked: {text}"}]}),
        config=config, version="v2",
    )
  5. 5.What came back (sandbox run, simulated answers)

    We ran the GetABrain half of this on 2026-12-03 with a test-mode key, so these three answers are simulated, not real people. The shape is exactly what live answers look like: selected_ids holds the option the person picked.

    question: "You get two emails from a software company. Which subject line would you open first?"
      a: "Your March invoice is ready"
      b: "Quick question about your March invoice"
    
    response_data from 3 answers:
      {"selected_ids": ["b"]}
      {"selected_ids": ["a"]}
      {"selected_ids": ["a"]}
    
    resume message: "2 of 3 people picked: Your March invoice is ready"
  6. 6.Gating a risky tool instead

    Same middleware, different decision. For a tool that does something (send_email, execute_sql), allow approve and reject, ask GetABrain a yes/no question about the proposed call, and resume with {"type": "approve"} or {"type": "reject", "message": "..."}. The LangGraph recipe does this with raw interrupt(), and the MCP server guide skips the plumbing entirely.

Sources (2)
  • HumanInTheLoopMiddleware with interrupt_on, decision types approve/edit/reject/respond, checkpointer required: docs.langchain.com · checked 2026-12-03
  • invoke(..., version="v2") returns result.interrupts; resume with Command(resume={"decisions": [...]}) on the same thread_id: docs.langchain.com · checked 2026-12-03
Add HumanInTheLoopMiddleware to create_agent, map the tool names that need a person in interrupt_on, and pass a checkpointer. invoke() then stops before those tool calls and returns them as interrupts. Resume the same thread_id with Command(resume={"decisions": [...]}).
approve runs the tool as proposed, edit runs it with changed arguments, reject skips it and tells the model why, and respond skips it and returns the human's message as the tool result. Use respond for ask-a-person tools, never to deny an action.
Underneath, yes: the middleware calls LangGraph's interrupt() and saves state through LangGraph persistence. The middleware adds the tool-call policy and the decision format. If you write your own graph, use our LangGraph human-in-the-loop recipe instead.
Verified US workers on GetABrain, paid per answer. Test-mode keys never reach them: you get simulated answers in the same format, so you can build the loop first.
Test mode answers are simulated and paid from the $5 sandbox credit. Live yes/no answers start at $0.05 each plus a 20% platform fee, prepaid, no subscription required.