DEV Community

Cover image for Adding Human-in-the-Loop (HITL) to Your AI Agent with LangGraph
arunagri82
arunagri82

Posted on

Adding Human-in-the-Loop (HITL) to Your AI Agent with LangGraph

Modern AI agents are powerful, but you don’t always want them to act fully on their own.
Sometimes you need a human to review, approve, or correct what the AI is doing — especially for publishing content, sending emails or notifications, or taking actions in production systems.
This pattern is called Human-in-the-Loop (HITL).

In this guide, we’ll see how to build a simple HITL workflow using LangGraph (built on top of LangChain). By the end, you’ll have a working example that lets the AI draft a response, sends that draft to a human for review, and either finalizes or revises the response based on human feedback, looping until the human is happy.

What is LangGraph?
LangGraph is a framework on top of LangChain for building stateful, multi-step workflows with LLMs, tools/APIs, and humans. Instead of writing one long function, you define a graph where each node is a step in your workflow, and edges control the flow. You can add conditions, loops, and retries, which makes it natural to add HITL into your AI agent.
Example: Human Review for AI Responses
We’ll build a small workflow where the user asks a question, the AI drafts an answer, a human reviews the draft, and then either approves or requests changes. If they approve, we finalize the answer. If they suggest changes, the AI revises the answer and sends it back for review, repeating until they approve.

Step 1: Define the Workflow State
We define a shared state object that flows through the graph. Each node reads from and writes to this state. In our case, the state tracks: the user’s original question, the AI’s draft, the human feedback, and the final approved response.
Python state definition:

class HumanInTheLoopState(TypedDict):
    question: str           # User's original question
    ai_draft: str           # AI's current draft
    human_feedback: str     # Human review / comments
    final_response: str     # Final approved response
Enter fullscreen mode Exit fullscreen mode

Step 2: Define the Workflow Nodes
We define separate functions for each step in the process:

  1. draft_response – AI drafts the initial answer.
  2. get_human_feedback – human reviews and either approves or suggests changes.
  3. decide_next_step – decides whether to revise or finalize.
  4. revise_response – AI updates the answer based on feedback.
  5. finalize_response – produces the final, approved response. Each of these functions receives the current state (a dict), prints some helpful information, and returns a dict with the updated fields. Step 3: Wire It Up with LangGraph

We then use StateGraph from LangGraph to connect all the nodes and control execution order. We add nodes, add edges to define the flow, and add a conditional edge from human_review to either revise or finalize, based on human_feedback.
The flow looks like this:
[draft] → [human_review] → (decide)

revise? → [revise] → back to [human_review]
approve? → [finalize] → END

Step 4: Running the Workflow

After compiling the graph using graph.compile(), we call workflow.invoke(initial_state) with an initial state. The system will automatically run through the nodes according to the edges, calling the human review step whenever needed, and looping until the human types 'approve'.
At the end, the final approved response is available in final_state['final_response'], which you can send back to the user or store.
Summary:
In this document we covered:

  • What Human-in-the-Loop (HITL) means.
  • How LangGraph lets you build stateful workflows with clear steps and branching.
  • How to define state, nodes, and conditional edges for a HITL approval loop.
  • How to adapt the console-based example into a real application UI.

Top comments (0)