Suryanand/ Bootcamp
suryanand.com ↗Let's talk
← AgentsAI guideIntermediate#agents#human-in-the-loop#approvals#safety
Frame · ai-engineering / agents / 09-human-in-the-loop.mdGuide 033 / 103

Human-in-the-loop and approvals

The fastest way to make an agent safe enough to ship is often not a smarter model, it is a well-placed "are you sure?". Human-in-the-loop (HITL) means the agent pauses at defined points, shows a person what it intends to do, and continues only once they approve, edit or reject. Done well it feels like a good colleague checking in; done badly it is a stream of pop-ups that people learn to click through.

Why it matters

Agents act on the world: they send emails, merge code, issue refunds, change records. Some of those actions are irreversible or expensive, and models still make confident mistakes, misread ambiguous instructions, or get manipulated by prompt injection in content they read. An approval gate turns a potential incident into a rejected suggestion.

HITL also builds trust incrementally. You can launch with approvals on every write action, measure how often humans change or reject the proposal, and relax the gate for categories where the agent is consistently right. That gives you a data-driven path from "copilot" to "autopilot" instead of a leap of faith.

How it works

There are a few distinct interaction types, and it helps to name them:

  • Approve or reject a specific tool call ("Send this email to 340 customers?").
  • Edit the proposed arguments before execution (fix the recipient, change the amount).
  • Answer a question when the agent is blocked or the request is ambiguous.
  • Review a plan before execution starts (often the highest-leverage gate, see the planning doc in this track).
  • Review output before it leaves the system (a drafted reply, a generated report).

Mechanically, a pause has to survive time. A human might respond in ten seconds or tomorrow morning, so the run's state must be serialised and stored, and resumption must not repeat side effects that already happened. That is why frameworks tie HITL to persistence:

  • In the OpenAI Agents SDK, a tool can declare needsApproval. The run stops with interruptions, you call state.approve() or state.reject(), and resume with run(agent, state). The RunState can be serialised to a string and restored later.
  • In LangGraph, a node calls interrupt(payload). This requires a checkpointer and a thread_id. The payload surfaces to the caller under __interrupt__, and you resume by invoking the graph with Command(resume=value), which becomes the return value of interrupt(). Note that the node re-runs from its start on resume, so code before the interrupt must be safe to repeat.
Rendering diagram…

Reference architecture

Rendering diagram…

The risk policy is code, not a prompt: tool name, argument values (amount over a threshold, external recipient), user role and environment decide whether to pause. Rejections go back to the model with the reviewer's reason so it can try a different approach.

Code

An OpenAI Agents SDK (TypeScript) example where refunds above a threshold need approval, and the run state is saved so a reviewer can decide later.

import { z } from "zod";
import fs from "node:fs/promises";
import { Agent, run, tool, RunState } from "@openai/agents";

const issueRefund = tool({
  name: "issue_refund",
  description: "Refund an order. Amount in USD.",
  parameters: z.object({ orderId: z.string(), amount: z.number() }),
  needsApproval: async (_ctx, { amount }) => amount > 100,
  execute: async ({ orderId, amount }) => `Refunded $${amount} on ${orderId}`,
});

const agent = new Agent({
  name: "Support agent",
  instructions: "Help customers with orders. Use issue_refund when a refund is justified.",
  tools: [issueRefund],
});

// 1. Start the run. It pauses if approval is needed.
let result = await run(agent, "Order A-77 arrived broken, please refund the $240.");
if (result.interruptions?.length) {
  await fs.writeFile("pending-run.json", JSON.stringify(result.state), "utf-8");
}

// 2. Later, in the reviewer's request handler
async function review(approved: boolean) {
  const saved = await fs.readFile("pending-run.json", "utf-8");
  const state = await RunState.fromString(agent, saved);
  for (const item of state.getInterruptions()) {
    approved ? state.approve(item) : state.reject(item);
  }
  const resumed = await run(agent, state);
  console.log(resumed.finalOutput);
}

await review(true);

In production, store the state in a database keyed by a review ID instead of a file, and show the reviewer the tool name and arguments in a clear card.

Trade-offs and pitfalls

  • Approval fatigue. If everything needs approval, people stop reading. Gate by risk, and make low-risk actions automatic.
  • Unclear requests. "Approve tool call #3?" is useless. Show what will happen, to whom, and why, in plain language, plus a diff for edits.
  • Non-idempotent resume. If resuming re-runs code that already sent a message, you get duplicates. Put side effects after the pause, or make them idempotent with keys.
  • Lost state. In-memory checkpointers vanish on deploy. Use a durable store for anything a human might answer later.
  • Approvals as a security boundary. A human approving a malicious-but-plausible action is still a breach. Combine HITL with least-privilege tools and sandboxing; do not rely on it alone.
  • Timeouts. Decide what happens when no one answers: cancel, escalate, or proceed with a safe default. Never silently proceed with the risky action.
  • Do not let the agent ask itself. Approval must come from a verified human channel, never from text the agent read in a tool result.

Checklist

  • Write a risk policy in code that decides which actions pause.
  • Approval requests show the action, target, arguments and reason clearly.
  • Paused runs are serialised to durable storage with an ID and expiry.
  • Resumption is idempotent and does not repeat completed side effects.
  • Rejections return the reviewer's reason to the agent.
  • Every decision is written to an audit log with reviewer identity.
  • Track approval and edit rates per action type to tune the policy.

Further reading

Why this exists README

I learn best by explaining things, so I record the explanations. Everything here is free, with no sign-up. If a lesson helps you ship something, I'd love to hear about it.