Area 03 · AI Agents · Concept 16 of 20

Human in the Loop

The agent stops and waits for a person to approve or edit before a risky step.

11–20 · Production levelPhase 3 · Weeks 11–14≈ 1 hour

The idea

Some actions should never run on the model’s judgement alone: sending emails, spending money, deleting data, changing production. Human-in-the-loop inserts an approval step: the agent proposes the action, execution pauses, a person approves, edits or rejects, and the run continues.

Implement it at the tool layer, not in the prompt. Mark tools as needing approval; when the model calls one, save state (checkpoint), notify a human with the exact arguments, and resume with their decision as the tool result.

Show approvers exactly what will happen ("Send email to anna@x.com with subject ..."), not a vague summary.

Why it matters

It lets you deploy useful agents before you trust them fully, and it is often a compliance requirement.

Picture it

Words to know

  • InterruptA deliberate pause in a run waiting for outside input.

Try it without code

Classify your tools

  1. List every tool your agent has.
  2. Mark each as read-only, reversible or irreversible.
  3. Irreversible ones need approval.

Use it in code

Approval gate around risky tools.

python
NEEDS_APPROVAL = {"send_email", "delete_file", "create_payment"}

def execute(call, handlers, ask_human):
    if call.name in NEEDS_APPROVAL:
        decision = ask_human(f"Agent wants to run {call.name} with {call.input}. Approve? [y/N/edit] ")
        if decision.lower().startswith("edit"):
            call.input.update(parse_edit(decision))      # your own parser, e.g. "edit subject=New title"
        elif decision.lower() != "y":
            return {"status": "rejected by human", "reason": decision}
    return handlers[call.name](**call.input)

# CLI version: ask_human = input. In a web app: checkpoint, notify, resume on click.
PRACTICE · TICK IT AFTER THIS

Add an approval gate to a send_email tool (print instead of sending) and test approve, reject and edit.

Common mistakes

  • Relying on the prompt ("ask before sending") instead of code.
  • Approval screens that hide the real arguments.

Go deeper