ai newsDi., 1. Dez.

Human-in-the-Loop Approval for Vercel AI SDK Agents

Your agent is about to refund $48. Nobody is watching. It's 2am. A customer writes in about an order that never arrived. Your support agent reads the thread, checks the shipment, decides a refund is fair, and calls issueRefund . Maybe that was the right call. Maybe the order shipped and the trackin

dev.toFreitag 08:19 · 27.11.5 Min. Lesezeit
Bild: dev.to

It's 2am. A customer writes in about an order that never arrived. Your support agent reads the thread, checks the shipment, decides a refund is fair, and calls issueRefund.

Maybe that was the right call. Maybe the order shipped and the tracking just hadn't updated. You'll find out in the morning.

This post shows how to put a real human in front of that tool call in a Vercel AI SDK agent — in about three lines — and why most approval setups quietly fail.

The usual pattern is to post an approval request to Slack and wait for a thumbs up.

It works during business hours. Outside them it fails for one boring reason: notifications are muted. Slack is muted on most phones most of the time, and so is email. An agent waiting on a Slack message at 2am is an agent waiting until 9am.

So you end up with one of two bad outcomes. Either the agent blocks for hours on something urgent, or someone gets tired of that and removes the approval step.

The approval isn't the hard part. Reaching the human is.

M91 rings the approver's phone at full volume — through silent mode, through Do Not Disturb — and escalates to a voice call if nobody picks up. The approver taps Approve or Reject on their phone, and that answer goes straight back to your agent.

It's free, and it's now in the Vercel AI SDK Tools Registry.

pnpm add @msg91comm/m91 ai Enter fullscreen mode Exit fullscreen mode Create a channel in the M91 app (iOS or Android), invite the people who should be able to approve, and copy the channel's send link. Put it in your.env:

M91_SEND_LINK = your_send_link_here Enter fullscreen mode Exit fullscreen mode The send link is a secret. Treat it like an API key — don't commit it.

Here's a complete agent with one gated tool. The refund is simulated, so you can run this safely:

import { generateText, isStepCount, jsonSchema, tool } from 'ai'; import { M91Client } from '@msg91comm/m91'; import { createM91ToolApproval } from '@msg91comm/m91/integrations/vercel-ai'; // The send link comes from a channel in the M91 app. Keep it secret. const client = new M91Client ( process. env. M91_SEND_LINK! ); const issueRefund = tool ( { description: 'Simulate a refund approval; no payment is issued.', inputSchema: jsonSchema < { orderId: string; amount: number } > ( { type: 'object', properties: { orderId: { type: 'string' }, amount: { type: 'number' } }, required: [ 'orderId', 'amount' ], } ), // Replace this simulation with your payment API before issuing real refunds. execute: async ( { orderId, amount } ) => ( { simulated: true, orderId, amount } ), } ); const { text } = await generateText ( { model: 'openai/gpt-5-mini', tools: { issueRefund }, // issueRefund rings a phone first and runs only if a person taps Approve. // A rejection, no answer within 10 minutes, or an M91 error denies it. toolApproval: createM91ToolApproval ( client, { tools: [ 'issueRefund' ], timeoutSeconds: 600, } ), stopWhen: isStepCount ( 5 ), prompt: 'Order ORD-40122 never arrived.

Refund the customer 48 USD.', } ); console. log ( text ); Enter fullscreen mode Exit fullscreen mode The only new part is toolApproval. The AI SDK already has the hook; createM91ToolApproval fills it with a phone call.

Anything not named in tools runs as normal. Gate the dangerous ones, leave the reads alone.

The agent reasons about the refund and decides to call issueRefund. Then it stops.

Your phone rings — properly rings, even on silent — showing the tool name and the arguments the model picked. You tap Approve, and the tool executes with exactly those arguments. You tap Reject, and the model is told the call was denied; it carries on and usually explains to the customer that the refund needs review.

Silence is never treated as a yes. If your alerting provider goes down, the worst case is that a refund doesn't happen — not that one happens unsupervised.

Point a client at a bad send link and you can watch it happen:

{ "type": "denied", "reason": "Could not get human approval via M91, so the action was not run. error: [NETWORK_ERROR] Could not reach M91 at https://m91-api.msg91.com/s/*** fetch failed" } Enter fullscreen mode Exit fullscreen mode Note that the send link is masked in the error. It never reaches the model.

Test all four before you ship. Especially the timeout: start the agent, ignore your phone for ten minutes, and watch it deny.

Channels have a severity level that decides how hard M91 tries:

For money-moving tools, HIGH or CRITICAL is the sane choice. For a deploy that can wait, MEDIUM is fine.

The same idea is wired into the frameworks you're probably also using:

All of them share the same fail-closed decision function underneath.

What if nobody approves? The tool is denied after the timeout. The agent continues and handles the denial like any other tool failure.

Can more than one person approve? Yes. Invite several people to the channel and whoever answers first settles it — the other phones stop ringing.

Does it really ring on silent? Yes, that's the whole point. It's a native iOS and Android app, not a push notification.

Is it free? Free forever, including the SDK (MIT).

Does the model see the send link? No. The link lives in your environment and is used server-side by the client.

If you're running agents in production, I'd like to hear how you handle approvals today. Drop a comment.

Quelle: dev.to (dev.to). Original öffnen