Detent

AI agents

Human-in-the-Loop Approvals for AI Agents in React (AI SDK)

Make an agent ask before it refunds, sends or deletes anything. Three small changes with the AI SDK, and what a good request shows.

  • Looked up order #4471 · Shopify1.1s
  • Tracked the parcel · DHL0.8s
  • Checked the refund policy · Help center0.4s
Refund $86.00 to Lucía · Stripe Needs you
DenyApprove

An agent that can refund an order, send an email or delete a record should ask first. With the AI SDK that takes three small changes: mark the tool on the server, show the request in the chat, and send the answer back.

The demo below is the real thing. Ask about order #4471: the agent looks it up, then the refund stops and waits for you. Money takes a second press, and a no sends it looking for another way.

Harbor Support

Mark the tool

Tools that only read run as usual. The refund gets needsApproval: true, so when the model calls it, the stream ends with the call waiting instead of running execute. The model can still propose the refund; only a person can carry it out.

app/api/chat/route.ts
refund: tool({
  description: 'Refund an order to the card it was paid with',
  inputSchema: z.object({ orderId: z.string(), amount: z.number() }),
  needsApproval: true, // the model can ask; only a person can run it
  execute: async ({ orderId, amount }) => stripe.refund(orderId, amount),
}),

Pass the tools to convertToModelMessages as well, so answered calls go back to the model in the right shape. The flag can also be a function of the input, for rules like refunds over $50 needing a person.1

Ask in the chat

Every tool call arrives as a message part whose type is tool- plus the tool’s name. When it needs a person its state is approval-requested, and it carries an approval.id. Show the request, and answer with that id.

app/support/Chat.tsx
const { messages, addToolApprovalResponse } = useChat({
  sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
})

// a refund waiting for a person
if (p.type === 'tool-refund' && p.state === 'approval-requested') {
  addToolApprovalResponse({ id: p.approval.id, approved: true })
}

Send the answer back

The sendAutomaticallyWhen option above resends the conversation once every waiting call has a yes or a no.2 On a yes, the server runs execute and the model reads the result. On a no, the model gets a denied result and should say so plainly.

What a good request shows

The two buttons are the easy part. What makes people trust an agent is what the request says before they press.

The action, with the number. “Refund $86.00”, not “Confirm action”. The button repeats it, so the press is the decision.

Only the inputs that matter. The amount, who it goes to, which order. The person is checking the money and the target, not the JSON.

Why it needs a yes. One short reason, like “Moves money” or “Sends as you”, so the person knows what to look for.

Friction that matches the risk. Money and deletes take two presses. Low-risk writes take one.

What happens after a no. The agent says it stopped and offers the next step: “I haven’t refunded anything. I can open a claim with DHL instead.”

A record in the run. Who approved what, and when, sits next to the tool call, so the run reads as a history afterwards.

A ready-made screen

The demo at the top is Detent’s Agent chat block. It runs on the same useChat and the same route, and draws the request, the second press for money and the run beside the chat. You describe each tool once, for example the refund:

app/support/Agent.tsx
refund: {
  risk: 'high', // money takes a second press
  reason: 'Moves money',
  confirm: (i: { amount: number }) => `Refund ${money(i.amount)}`,
},

The keys match the tool names in your route. Agent chat is part of Detent Pro. The free Diff review does the same ask-first job for code an agent wants to change.

Choosing a library for the rest of the chat? See AI Elements alternatives, six libraries compared, and what the AI Elements deprecation means for code you already copied.

Where it breaks

Approve does nothing. Without sendAutomaticallyWhen the answer is stored but never sent. Add the helper, or call sendMessage() yourself.

The request never shows. The part type is tool- plus the tool’s key on the server. Rename the tool in the route and the client stops matching it.

A no still runs the tool. Only the server decides. Keep needsApproval on the tool itself, never as a check in the browser before sending.

The demo runs in production. Blocks that ship a scripted demo should start still. Detent’s default to autoplay={false}, so a real page never presses approve on its own.

Notes

  1. 1. needsApproval takes a boolean or a function of the tool’s input. Tool approval arrived in AI SDK 6; everything here is checked against 7.0. ↩
  2. 2. lastAssistantMessageIsCompleteWithApprovalResponses ships in the ai package. Without it you call sendMessage() after the last answer yourself. ↩