Human in the LoophumanInTheLoopMiddleware, and gating the todoist agent
No narration yet
Module 5, Lesson 225 min

humanInTheLoopMiddleware, and gating the todoist agent

On a createAgent (module 2), you do not hand-write the interrupt node. You attach humanInTheLoopMiddleware.

Gating specific tools with humanInTheLoopMiddleware
import { humanInTheLoopMiddleware } from "langchain";

const agent = createAgent({
  model, tools: [fileTask, closeTask, graduateTask], checkpointer,
  middleware: [
    humanInTheLoopMiddleware({
      interruptOn: {
        closeTask:    { allowedDecisions: ["approve", "edit", "reject"] },
        graduateTask: { allowedDecisions: ["approve", "edit", "reject"] },
      },
    }),
  ],
});

interruptOn names the tools that pause. allowedDecisions are the three outcomes: approve runs the tool call as-is; edit runs it with modified arguments (you change the labels or project before it fires); reject does not run it, and feeds the rejection back into the loop. fileTask is not listed, so it runs freely. closeTask and graduateTask pause. That is the exact policy your bridge wants: file auto-applies, destructive waits.

Practice

Try it yourself

Quiz

edit vs reject

What is the concrete difference between edit and reject in terms of what the graph does next?

Recall

Middleware vs a hand-written interrupt node

This is a trade, not an upgrade. The middleware is not strictly better than the hand-rolled version.

What do you gain and lose using humanInTheLoopMiddleware instead of hand-writing interrupt()?

Do

Gate close and graduate behind interrupts

Extend the todoist agent with destructive powers, safely gated.

Tick every step to confirm you did it.

Check

All three HITL decisions resume correctly

Confirm approve, edit, and reject each produce the correct outcome.

You should see

approve executes the tool call as originally proposed; edit executes it with your modified arguments; reject does not execute it and the rejection is fed back into the loop. A pending approval also survives a process restart.