AI Engineer Roadmap All lessons Interactive Quiz GitHub

Human in the Loop

Intermediate · 7 min read · Updated · By Saurabh Pathak

In short: Human in the loop (HITL) means the agent pauses at chosen points so a person can approve, edit, or reject what it is about to do.

Play the interactive lesson: Human in the loopRead on GitHub
On this page
  1. Explain it like I'm new
  2. Watch it
  3. How it works
  4. Code example
  5. When to use it / when not to
  6. Common mistakes
  7. Recap
  8. Interview questions
  9. Go deeper

Level: Intermediate · Reading time: 7 min · Phase: 4 · Agentic AI

Before you start: Agent Loops and Function Calling. You should know how the loop runs tools that the model requests.

Explain it like I'm new

At many companies, a junior employee can draft a payment, but a manager must click "approve" before money moves. The junior does the work; the manager signs off on the risky part. That's human in the loop.

Agents make mistakes: wrong tool, wrong arguments, misunderstood goal, or being tricked by injected text. For actions that are cheap and reversible (searching, reading), mistakes don't matter much. For actions that are irreversible or high-impact (sending an email to a customer, deleting files, paying money, merging code), a human check is the most reliable safety net.

Forms of HITL:

Words to know:

Watch it

Human in the loop: The agent pauses so a person can approve risky steps.
Human in the loop: The agent pauses so a person can approve risky steps.

It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.

How it works

  1. Classify tools by risk in code: read-only, reversible write, irreversible / external.
  2. When the agent requests a risky tool, pause before running it.
  3. Show the human a clear preview: what will happen, with what exact arguments, and why.
  4. The human approves, edits, or rejects.
  5. Resume: run the (possibly edited) action, or return "rejected by user: <reason>" to the agent as the tool result so it can adjust.
  6. Log who approved what and when.
sequenceDiagram
    participant Agent
    participant Code as Your code (gate)
    participant Human
    participant Tool as send_email
    Agent->>Code: call send_email(to, body)
    Code->>Code: risky? yes
    Code->>Human: preview: to=..., body=...
    Human-->>Code: approve (or edit / reject)
    Code->>Tool: send_email(...)
    Tool-->>Code: sent
    Code-->>Agent: tool result: "sent"

Worked example

The pipeline from agent_multi.py writes a draft and then wants to run send_email, which is marked risky. Two runs with the same goal (agents and email it), different human answers:

Human saysWhat the code doesReal result
Yeslogs human approved send_email, runs the toolstatus done, log ends send_email -> EMAIL SENT: ...
Nologs human rejected send_email, skips the toolstatus rejected, no email sent

The draft is written in both runs, because writing text is not risky. Only the irreversible step waits for a person.

For long waits (hours or days), the agent's state must be saved so it can resume later; frameworks call this checkpointing or interrupts.

Code example

From agent_multi.py: the worker asks an approver callback before any tool in risky_tools. Tests pass a stub; the CLI version asks on the console.

def worker(step, draft, guard, approver, log):
    tool = step["tool"]
    guard.check(tool)                              # guardrails first: allowlist + max steps
    if guard.needs_approval(tool):
        preview = draft if step["arg"] is None else step["arg"]
        if not approver(tool, preview):
            log.append(f"human rejected {tool}")
            return draft, False                    # do NOT run the tool
        log.append(f"human approved {tool}")
    out = TOOLS[tool](draft if step["arg"] is None else step["arg"])
    log.append(f"{tool} -> {out}")
    return out, True

def console_approver(tool, preview):
    return input(f"Allow {tool}? Preview: {preview!r} [y/N] ").strip().lower() == "y"
from agent_multi import run_pipeline
res = run_pipeline("agents and email it", approver=lambda tool, preview: False)
print(res["status"])   # rejected  -> the email was never sent

Note the default is No: if the human just presses Enter, nothing risky happens.

In real projects, LangGraph supports pausing a graph with an interrupt and resuming it later from a checkpoint; see the LangGraph docs for human-in-the-loop.

When to use it / when not to

Action typeExampleApproval?
Read-onlysearch, read fileusually no
Reversible writecreate draft, add labeloptional / batch review
Irreversible or externalsend email, pay, delete, deployyes

Common mistakes

Recap

If you remember only three things:

Interview questions

1. Which agent actions should require human approval? <details><summary>Answer</summary> Irreversible, external, or high-impact actions: sending messages, spending money, deleting or modifying important data, deploying, or anything with legal or safety impact. Read-only actions usually don't. </details>

2. Why enforce approval in code rather than in the prompt? <details><summary>Answer</summary> A model can ignore or be tricked out of prompt instructions (for example by prompt injection). A code gate around the tool runs regardless of what the model says. </details>

3. What is approval fatigue and how do you avoid it? <details><summary>Answer</summary> When people approve so many prompts that they stop reading them. Avoid it by gating only risky actions, showing clear, concise previews, batching low-risk reviews, and tracking how often approvals are edited or rejected. </details>

Go deeper

Prev: Multi-Agent Systems · Next: Agent Evaluation · Back to phase: Phase 4