Human-in-the-Loop

Human-in-the-Loop

Large language models are amazing in what they can do but aren’t perfect. You don’t have to use them for very long before you find them hallucinating, confidently telling you something you know to be false. While letting LLMs make some decisions is fine, it’d be unwise to naively accept everything an LLM chooses to do. That’s why it’s important to keep a human in the loop when it comes to sensitive decisions.

Here are just a few examples where you’d want a human making the final decision:

  • Medical diagnosis and treatment: A mistake here could cost a life.
  • Financial transactions: You might want to have a chance to confirm the purchase before your AI travel agent buys you a plane ticket.
  • Legal issues: That generated contract might look good, but maybe you should have your lawyer look it over, too.
  • App string translation: An ambiguous phrase could use some human eyes before you blindly push the changes to all your users.

Thankfully, LangGraph enables human-in-the-loop (HIL) interactions. The way to give the human a chance to make a decision is to set a breakpoint in the graph workflow. You can set it either before or after a node execution. Checkpoints must be enabled for breakpoints to work. You’re pausing execution, so LangGraph needs that saved state to resume execution from where it left off.

Here’s how you’d go about setting up a breakpoint:

memory = MemorySaver()
app = graph.compile(checkpointer=memory, interrupt_before=["node_3"])

interrupt_before will stop execution before node_3. If you wanted to stop after node_3, you’d change interrupt_before to interrupt_after.

While the graph is paused, a human can give feedback, which can be used to update the graph state or change the flow of execution.

You restart a paused graph by calling invoke again with None for the input state:

app.invoke(None, config=thread)

In the following demo, you’ll create a simple graph with a breakpoint, execute it, and resume execution after pausing.

See forum comments
Download course materials from Github
Previous: Structured Output Demo Next: Human-in-the-Loop Demo