Human-in-the-Loop
Also known as: HITL, approval gate, interrupt
- Who decides
- A person
- Shape
- Gate
Intent
Stop an agent at a few chosen points, save its full state, and let a person approve, edit or reject the proposed action, or answer a question, before the run continues. Code decides where the run stops, the person decides what happens there, and the run resumes from exactly where it paused with that decision added.
Problem
You run a customer-support agent for an online homeware shop. It can look up orders, issue refunds and send emails. A customer writes: “Order 981 arrived broken, refund my $480.” Shop policy says support can refund up to $250; anything above goes to finance.
The agent reads the order, sees a damaged item and calls issue_refund(order=981, amount=480). Every step was reasonable; the agent simply did not know the cap. The two obvious designs both fail:
- No gates: the mistake cannot be taken back. The refund goes out over the cap. Once money is sent or an email is out, a better next step cannot undo it.
- A gate on every call: people stop reading. The reviewer is asked about every order lookup and every draft, dozens of times an hour. They learn to click yes, and the $480 refund gets through like the rest.
- Nobody owns the decision. When finance asks who approved the refund, there is either no record or a click that nobody remembers.
What is missing is a few well-placed stops that a person can judge quickly.
Solution
Add three things around the agent loop. A policy, written in code, decides which proposed actions need a person. A durable pause saves the whole run and returns, so nothing keeps running while the person thinks. A resume loads the run, applies the decision exactly once and continues. The agent itself does not change.
On the refund request, a run looks like this:
- The agent proposes. The model emits
issue_refund(order=981, amount=480). Nothing runs yet. - The policy decides. Ordinary code, not the model, checks the call: order lookups run freely, refunds above $250 need finance, and deleting an account is denied outright. This refund goes to finance.
- Pause and save. The harness builds an approval request (what will happen, why, and that it cannot be undone), saves the messages and the pending call under the thread id
ticket-981, and returns. The agent tells the customer the refund is waiting for approval. - Notify and wait. The request lands in the finance channel and may sit there for hours. If nobody decides before a deadline, the default is to not refund.
- The person decides. Alice from finance edits the amount to 250 and adds a comment: “policy cap, offer store credit for the rest”.
- Resume. The app calls the run again with the same thread id and Alice’s decision. The harness checks that the order was not refunded in the meantime, runs
issue_refund(order=981, amount=250)with the request id as idempotency keyIdempotency key A unique id sent with a request. The system remembers it and ignores a second request with the same id, so a retry cannot refund twice., and the model reads the comment and offers store credit. - Record. The request, the decision, who made it and what actually ran go to the audit log.
The nearest pattern, Evaluator–Optimizer, judges an output after it exists; an approval gate stops an action before it has any effect, which is what matters for payments, sent messages and deletes.
Structure
Hover over a number, or tap it, to read what it is.
- Agent loop. Any agent or workflow, such as a ReAct loop or a plan executor. It proposes a tool call, a plan, a draft or a question.
- Policy gate. Code outside the model that maps each proposal to allow, ask or deny. A denied call goes back to the model as an error with a reason, so it can try another way.
- Tool runs. Allowed calls run at once, approved or edited calls after resume. Each gated call carries an idempotency key, so a retry or a double click cannot run it twice.
- Pause and save. Builds the approval request and saves the messages and pending calls to a durable store under a thread id. Then it returns; no process waits.
- Reviewer. A person with the authority and context to judge the request, reached where they already work. A deadline applies a safe default if nobody answers.
- Resume. Loads the run by thread id, matches the decision to the stored request, re-checks that the action still makes sense, applies it once and gives the result to the model.
- Audit log. A record of each request, the decision, the reviewer, the time and what actually ran.
Use it when / Avoid it when
Use it when
- Actions cannot be undone or cost a lot. Payments, external messages, deletes and deploys cannot be fixed by a better next step.
- The agent lacks something only a person has. A business rule, a preference, an access right or the authority to commit the company.
- Someone must answer for the decision. Legal, safety or reputational stakes, or a regulation, require a named person to approve or stop the system.
- The agent is new in production. Start gated, watch how often each kind of request is approved, and relax the ones that always are.
Avoid it when
- Actions are read-only or can be undone. Let them run in a sandbox and review the result afterwards instead.
- Reviewers approve nearly everything without reading. Replace that gate with an allowlist, a sandbox or an automated check, since a click nobody thinks about protects nothing.
- No person can answer in time. Use plain code that applies a safe default, such as refusing or queueing the action.
- The risky step can be designed away. Use a prompt chain that writes a draft instead of sending it.
How it fails
- Approval fatigue
-
Reviewers approve in a second without reading; Anthropic reports that users accept 93% of Claude Code’s permission prompts.
Mitigation: gate fewer, higher-stakes actions and run the rest in a sandbox.
- Uninformative request
-
The reviewer sees “Allow tool run?” and raw JSON, cannot judge it, and clicks yes.
Mitigation: show a summary, the agent’s reason, the impact, a preview and whether the action can be undone.
- Double execution on resume
-
Two clicks, a retry, or a crash after the tool ran but before the state was saved sends the refund twice.
Mitigation: give every gated call an idempotency key the payment system checks.
- Stale approval
-
Approved yesterday, run today on a world that changed: a colleague already refunded the order.
Mitigation: let requests expire, and re-check preconditions when the run resumes.
Related patterns
- ReAct (tool-use loop): the most common place for a gate, between the model proposing a tool call and the harness running it.
- Plan-and-Execute: a person approves or edits the plan once, and the steps then run without asking at each one.
- Evaluator–Optimizer: a person can be the evaluator, or the escalation when the loop reaches its limit without passing.
- Swarm / Handoffs: a handoff target can be a person, as when a support agent passes the conversation to staff.
Try it
List the agent’s tools and say which run freely, which need a person and which are forbidden. The generator writes the system prompt section on gates and rejections, plus the approval request a reviewer sees.
Open the generator on this pattern
Further reading
- Human-in-the-loop (OpenAI Agents SDK docs, 2026): tool approval, saving run state, and how to avoid running an approved call twice.
- Claude Code auto mode (Anthropic, 2026): why most prompts get approved, and a classifier that replaces many of them.
- Levels of Autonomy for AI Agents (Feng, McDonald and Zhang, 2025): five roles for the user, from operator to observer.