Human-in-the-Loop Approval
Some actions need a human’s sign-off before they run — a shell command, a file
write, a payment. .withApprovalPolicy({ mode: "detach" }) hands those calls to
a person, who answers with agent.approveRun(runId) / agent.denyRun(runId, reason). Where the run waits depends on whether you also turned on durable runs:
Without .withDurableRuns() (live) |
With .withDurableRuns() (durable) |
|
|---|---|---|
| The run while waiting | Stays alive in this process, blocked at the gate | Pauses; its checkpoint goes to SQLite; the process may exit |
runStream() |
Emits ApprovalRequested, stays open, continues after the answer |
Ends with StreamCompleted + pendingApproval |
run() |
Waits; resolves when the run ends | Returns status: "awaiting-approval" |
| Who can answer | Code in the same process | Any process, even after a restart |
approveRun resolves with |
The run’s final result | The resumed run’s result |
You need durable runs only when the answer may come from another process (a separate worker, a web server that does not own the run), or the wait must survive a crash or restart. For an interactive app, a chat UI or a CLI where the process that runs the agent also receives the human’s answer, the live wait is enough and needs no store.
The durable backend is built on the same RunStore as crash-resume: the decision and the paused checkpoint live in SQLite, so approve/deny works across process and machine boundaries.
Live approval (no durable store)
Section titled “Live approval (no durable store)”import { ReactiveAgents } from "reactive-agents";
const agent = await ReactiveAgents.create() .withProvider("anthropic") .withTools({ builtins: ["file-write"] }) .withApprovalPolicy({ tools: ["file-write"], mode: "detach" }) .build();
for await (const event of agent.runStream("clean up the temp files")) { if (event._tag === "ApprovalRequested") { // Show event.toolName / event.args to the user, then answer. // Do not await here: approveRun resolves when the run ends, // and the run's events keep arriving on this stream meanwhile. void agent.approveRun(event.runId); // or agent.denyRun(event.runId, "not now") }}ApprovalRequestedarrives at both stream densities.agent.listPendingApprovals()lists live waits too, so arun()caller (no stream) can find them.run(task, { onApproval })answers them for you.- Parallel sub-agents ask under the root run’s id, so
approveRun(runId)alone answers the oldest wait. Each pending item carriesgateId,subAgent,toolNameandargs; pass{ gateId }to answer exactly one:agent.approveRun(runId, { gateId })/agent.denyRun(runId, reason, { gateId }). handle.stop()while waiting denies the call (the run then finishes);handle.terminate()cancels the run. Nothing stays pending either way.- A live wait lives in the agent object: it is gone if the process exits. Use durable runs when that matters.
- An
onApprovecallback is ignored indetachmode (live or durable); usemode: "block"for a programmatic decider. timeoutMsbounds the wait:.withApprovalPolicy({ tools, mode: "detach", timeoutMs: 60_000 }). No decision in time denies the call, and the model reads why. It also bounds amode: "block"onApprovecallback that never settles. Without it a live wait lasts until answered, stopped or terminated.
Asking the user a question (live)
Section titled “Asking the user a question (live)”.withUserInteraction() lets the model call request_user_input to ask for a
choice, a confirmation or a small form. It uses the same two backends as
approvals. Without .withDurableRuns() the question waits live and the same
run continues with the answer:
const agent = await ReactiveAgents.create() .withProvider("anthropic") .withReasoning() .withUserInteraction({ timeoutMs: 120_000 }) // optional wait bound .build();
for await (const event of agent.runStream("help me pick a plan")) { if (event._tag === "InteractionRequested") { // event.kind: "choice" | "confirmation" | "form"; event.prompt; event.schema void agent.respondToInteraction(event.runId, event.interactionId, "basic"); }}- The answer becomes the tool result the model reads (
The user responded: "basic"). With no answer intimeoutMs, or afterstop(), the model readsThe user did not answer: <reason>and the run continues. agent.listPendingInteractions()lists live questions too, so arun()caller can find and answer them.- With
.withDurableRuns()the run instead pausesawaiting-interactionandrespondToInteractionresumes it from any process (no wait bound applies).
Multi-part questions
Section titled “Multi-part questions”The model can also pass questions: a list of typed questions in one call.
Each has id, prompt, type (text, single-choice, multi-choice,
boolean, number) and optional options ({ label, value, description }),
required (default true), default, min/max, maxLength and allowOther
(free text for a choice). The old kind/prompt/schema call still works.
A structured call puts the same list on event.questions (InteractionRequested)
and on each listPendingInteractions() entry; answer with an id-keyed map via
respondToInteraction(runId, interactionId, { [questionId]: value }). A
legacy call has no questions.
@reactive-agents/tools exports the pure helpers a host uses with it:
parseRequest(args) normalizes either shape, and validateAnswers(request, answers) checks an answer map keyed by question id. It returns a typed result
(status of answered, partial, skipped or timeout, plus a per-question
status and value), or per-question errors (RequiredMissing, NotInOptions,
OutOfRange, TooLong, WrongType, UnknownQuestion) that the model reads to
correct itself. timeoutAnswer(request) builds the timeout result.
Enabling it
Section titled “Enabling it”.withApprovalPolicy() names which tool calls require approval. mode: "detach"
(the default once .withDurableRuns() is set) makes a gated call wait for a
person; with .withDurableRuns() the wait is a durable pause.
The data half of a policy (mode, tools, commands) has a declarative
AgentConfig key, approvalPolicy, and round-trips through toConfig(). The
onApprove / requireFor handlers are functions, not JSON, so supply them in
code; a stored block-mode policy with no onApprove refuses every gated call.
import { ReactiveAgents } from "reactive-agents";
const agent = await ReactiveAgents.create() .withProvider("anthropic") .withModel("claude-sonnet-4-6") .withTools({ tools: [/* ... */] }) .withDurableRuns() .withApprovalPolicy({ tools: ["shell-execute", "file-write"], // names that must pause mode: "detach", // durable pause (default with durable runs) }) .build();You can also gate by predicate instead of (or in addition to) a name list:
.withApprovalPolicy({ requireFor: ({ toolName, iteration }) => toolName.startsWith("delete-") || iteration > 10, mode: "detach",})What a policy gates
Section titled “What a policy gates”Once an agent has any approval policy, these calls wait for approval:
- tools named in
tools, and any callrequireForreturnstruefor; - every registered tool whose definition sets
requiresApproval: true: built-ins such ascode-executeandfile-write, theshell-executeterminal tool and your own tools. You do not list them by hand; - MCP tools the server annotates as writing (
destructiveHint: trueorreadOnlyHint: false). These hints can only narrow, so they count withouttrustAnnotations; .withAgentTool()sub-agent tools. TheirrequiresApprovaldefaults totrue; set it tofalsefor unattended delegation;- tools added after build with
agent.registerTool()that setrequiresApproval: true.
MCP tools, sub-agent tools and registerTool() tools are registered after
build(), so each reasoning pass reads the live tool registry and adds them to
the gated set. This holds under every strategy and in both block and detach
mode. An agent with no approval policy gates nothing.
Not gated by default: an MCP tool with no annotation and no operator readOnly
listing (effect unknown, see
Tool effect),
and a custom tool that sets mutatesExternalState: true but not
requiresApproval. Gate one by name (MCP tools are named server/tool) or
with requireFor:
.withApprovalPolicy({ tools: ["github/create_issue"], mode: "detach" })Changed in 0.19. Before 0.19 the gated set held only the tools the builder could see at
build(). MCP tools that write,.withAgentTool()sub-agent tools andregisterTool()tools withrequiresApproval: trueran without approval under every strategy, even with a policy set.
Code-action and approval
Section titled “Code-action and approval”The code-action strategy runs model-written code in a sandbox worker. That
code calls tools one after another with no point where a person can step in,
so code-action cannot honor a gate on a tool its sandbox can call. It refuses
the run before any model call when any of these holds:
- a tool the sandbox can call is in the gated set (above);
commandsrules are set and the sandbox can callshell-execute(command rules gate by arguments the script picks at run time);requireForis set (a predicate cannot be checked ahead of the script).
Otherwise it runs: no policy, or a policy that gates only tools the sandbox
cannot call. The mode (block or detach) does not change the answer. The
refusal starts with code-action cannot honor .withApprovalPolicy( and names
the gated tools it can call. For gated tools use the reactive, reflexion,
plan-execute-reflect or tree-of-thought strategy.
To warn before a run, codeActionApprovalConflict (exported from
reactive-agents and @reactive-agents/runtime) applies the same rule:
import { codeActionApprovalConflict } from "reactive-agents";
const verdict = codeActionApprovalConflict({ callableTools: ["file-write", "web-search"], policy: { tools: new Set(["file-write"]) },});// verdict.refuse === true, verdict.gatedCallable === ["file-write"]It checks only the tools and policy you pass. Tools registered after build (MCP tools that write, sub-agent tools) are not in a config-only check; the strategy’s own check at run start sees them.
Shell command rules
Section titled “Shell command rules”commands gates shell calls by what they run, not by tool name:
const gitAgent = await ReactiveAgents.create() .withProvider("anthropic") .withTools({ builtins: ["git"], workspace: process.cwd() }) .withApprovalPolicy({ commands: ["git push", "gh pr create"], onApprove: ({ args }) => { // Your approval UI here; `args.command` is the full command line. console.log("approve?", args); return false; }, }) .build();- A rule is a word prefix.
"git push"matchesgit push origin main, notgit pull. A trailing*changes nothing ("git push *"="git push"); a rule with no words, such as a bare"*", matches nothing. - The command is split into segments on
&&,||,;,|and newlines. A call needs approval when any segment matches a rule. LeadingNAME=valueassignments and a baresudo/env/command/exec/nohupare skipped, and/usr/bin/gitcounts asgit.git -C dir pushdoes not match"git push"; the read-only preset refuses it. - Rules apply to
shell-executecalls only (also under the removedgit-cli/gh-clinames). - The read-only
gitpreset (builtins: ["git"]) normally refuses every git/gh write. Withcommands, the shell runs a refused write that matches a rule. It runs only after the approval gate let the call through: refused with noonApprovein block mode, paused in detach mode. Output redirects (>) stay refused. - The shell gets the rules only from the approval policy. A
terminal.approvalCommandsset by hand is ignored. - An approved
git pushstill needs credentials, and the git preset passes git none: its config holds only your user name and email (no credential helper), and the shell’s HOME is a sandbox directory with no SSH keys or agent socket. A push to an HTTPS or SSH remote that needs authentication fails; a push to a local path works.ghcommands use yourghlogin (GH_TOKEN/GITHUB_TOKENor theghconfig directory).
Migration (2026-11):
mode: "detach"no longer requires.withDurableRuns(), andbuild()no longer throws without it. A detach policy on a non-durable agent now waits live in process (see Live approval). Agents that already pair detach with durable runs behave as before.
Block mode (decided in code)
Section titled “Block mode (decided in code)”mode: "block" is the default whenever no mode is given and
.withDurableRuns() is not set. A gated call is decided in process by your
onApprove callback, with no person in the loop and no pause/resume round trip:
const agent = await ReactiveAgents.create() .withProvider("anthropic") .withModel("claude-sonnet-4-6") .withApprovalPolicy({ tools: ["shell-execute", "file-write"], mode: "block", onApprove: async ({ toolName, args, iteration }) => { // Your approval UI/logic here, e.g. a Slack prompt, a CLI confirm, a policy check. return true; }, }) .build();onApprove returns boolean | { approve, reason }, sync or async. A
throw/rejection denies (fail-closed).
Deny-by-default. A gated call in block mode with no
onApprovesupplied is refused — the tool does not run, and the agent’s loop sees the refusal rather than the tool’s result. Earlier versions treated"block"as inert (nothing read it, so gated tools ran unattended); if you rely on that behavior, addonApprove, or switch tomode: "detach"to have a person decide (live, or durable with.withDurableRuns()).A sub-agent (
.withAgentTool()/.withDynamicSubAgents()) always runs in block mode and inherits the parent’sonApprove— it has no durable store of its own, so it cannot pause for cross-process approval.
Pausing (durable)
Section titled “Pausing (durable)”With .withDurableRuns(), when the agent hits a gated call, the run pauses
durably and hands control back.
Use whichever entrypoint you already use — run() or runStream().
With run() the result carries status: "awaiting-approval" and a
pendingApproval descriptor:
const result = await agent.run("clean up the temp files");if (result.status === "awaiting-approval") { const { runId, toolName, args } = result.pendingApproval!; // The process can now exit. The pause is persisted under runId.}With runStream() the terminal event carries the same pendingApproval:
for await (const event of agent.runStream("clean up the temp files")) { if (event._tag === "StreamCompleted" && event.pendingApproval) { const { runId, toolName, args } = event.pendingApproval; }}Same-process convenience: onApproval
Section titled “Same-process convenience: onApproval”For interactive/CLI use, pass an onApproval callback — run() decides every
gated call through it and returns the final result, durable or live. You
never touch the runId:
const result = await agent.run("clean up the temp files", { onApproval: async ({ toolName, args }) => { // return true to approve, false to deny, or { approve, reason } return confirm(`Run ${toolName}(${JSON.stringify(args)})?`); },});Approving or denying — from any process
Section titled “Approving or denying — from any process”A fresh process (or the same one) lists what is waiting and decides:
const waiting = await agent.listPendingApprovals();// → [{ runId, gateId, toolName, args, task, updatedAt }] (empty if nothing is paused)
const next = waiting[0];if (next) { // Approve → the agent executes the gated call, then runs to completion: const result = await agent.approveRun(next.runId);
// Deny → the agent observes the denial and continues WITHOUT running the call: // await agent.denyRun(next.runId, "not allowed in production");}approveRun resumes from the exact checkpoint and executes the same call the
human reviewed — no fresh LLM step is taken for the gated action, so what is
approved is what runs. denyRun injects the denial as an observation and lets the
agent react on the next step.
Calling approveRun/denyRun on a run with no pending approval throws
ApprovalStateError (already decided, completed, or never paused). A run
waiting live in this process is always answered first; the durable store is
checked only when no live run matches.
Lifecycle
Section titled “Lifecycle”run() / runStream() ──▶ gated call ──▶ status: awaiting-approval ──▶ process may exit │ approveRun / denyRun ◀──────┘ (any process) │ ▼ resume from checkpoint ──▶ status: completedScope notes
Section titled “Scope notes”- Durable pauses work on both
run()andrunStream().approveRun/denyRunresume from the exact paused checkpoint; a re-pause on resume is persisted too (multi-gate). TheonApprovalcallback is sugar over this loop for same-process use. - Gate triggers are the
toolslist, therequireForpredicate and every registered tool that needs approval (requiresApproval: true, MCP tools annotated as writing,.withAgentTool()sub-agents). The live and durable gates use the same set; see What a policy gates. - One pending gate at a time: if a single step proposes several gated calls, the first pauses; the rest re-surface after the resume.
Runnable example
Section titled “Runnable example”A complete, runnable demo lives at
apps/examples/src/advanced/durable-hitl.ts.
With a provider key it drives the real gate (pause → approve → deny); offline it
shows the policy wiring.
ANTHROPIC_API_KEY=sk-ant-... bun run apps/examples/src/advanced/durable-hitl.tsSee also
Section titled “See also”- Durable Execution — crash-resume, the foundation HITL builds on.
- Builder API —
withApprovalPolicy,approveRun,denyRun,listPendingApprovals.
Install
Bun (recommended)bun add reactive-agentsNode 22.5+npm install reactive-agentsGet notified on new releases