Frameworks & tools

Vercel AI SDK human approval, without the queue

Wrap the tools you hand to generateText or streamText, and the calls that matter wait for a person. Everything routine clears in milliseconds, and you don't build a review queue to make it happen.

cb.guard({ action }, fn)request_clearancerefund.createemail.sendcontent.publish
cb.guard({ action }, fn) policy v12 cleared sealed to record

The problem

A tool's `execute` runs the instant the model picks it. Set `maxSteps` and the loop keeps going on its own, which is the whole appeal. It's also where it bites you: a `refund.create` or `email.send` fires mid-run, and `experimental_telemetry` tells you about it after the money's gone. There's no natural place to stop and ask.

You could thread an "approved yet?" flag through every `execute`, park the pending call somewhere, ping someone, then resume the run. Do that and you've built a queue, a notifier, a resume path and an audit log inside your agent code, sitting on top of the streaming the SDK already handles. That's a side project. Vercel AI SDK human approval shouldn't cost you one.

How ClearedBy handles it

  1. 01

    Guard the tool before you pass it

    Run your tool through guard(), then hand it to generateText or streamText as usual. The type signature doesn't change, so nothing else in the loop has to.

  2. 02

    The call checks in before execute runs

    When the model invokes a guarded tool, ClearedBy sees the action and its arguments first. Your execute body waits on a verdict, so the SDK pauses on that one step and nowhere else.

  3. 03

    Policy decides: clear or hold

    In-policy calls clear in milliseconds and execute() runs untouched. A £4,000 refund, a send to your whole list, anything past a threshold or just unusual — held, and routed to a named person in the queue, Slack or their phone.

  4. 04

    Cleared, then sealed

    Approve it and the tool runs on a short-lived credential. Reject it and execute() hands the model a tool result it can reason over instead of crashing the run. Either way the verdict lands on a tamper-evident record bound to whoever signed.

agent.ts
import { ClearedBy } from '@clearedby/sdk'
import { generateText, tool, stepCountIs } from 'ai'
import { z } from 'zod'

const cb = new ClearedBy({ apiKey: process.env.CLEAREDBY_API_KEY! })

const issueRefund = tool({
  description: 'Refund an order',
  inputSchema: z.object({ order: z.string(), amount: z.number() }),
  execute: ({ order, amount }) =>
    cb.guard(
      { action: 'refund.create', params: { order, amount } },
      () => shopify.refund(order, amount),        // runs only once cleared
    ),
})

await generateText({ model, tools: { issueRefund }, stopWhen: stepCountIs(5) })

FAQ

Does this work with streamText, or only generateText?

Both. guard wraps the tool's execute, not the call you make, so it behaves the same whether you stream, do a one-shot generation, or run a multi-step loop with stopWhen or maxSteps.

What does the model get back when a call is rejected?

A normal tool result — something like "rejected" or "held for review" — so the model handles it on its next step rather than throwing. On approval it gets the real result, exactly as if guard weren't there.

Won't waiting on a human stall my streamed response?

Only the guarded call waits. The rest of the run streams as normal, and since routine calls clear in milliseconds, most steps never pause at all.

Can the model dodge the gate by reaching for a different tool?

No. The real action only runs once it's cleared, because the credential to perform it is issued after approval. A model that tries another route just hits another gated tool with no way through.

Do I have to guard every tool I register?

No — guard the ones with consequences (refund.create, email.send, content.publish) and leave reads and low-stakes tools as plain tool() calls. Start in shadow mode with no card and watch what would have been held before you enforce anything.

Related use cases

Let your agents run. Inside the rules you set.

Wrap the tools you hand to generateText or streamText, and the calls that matter wait for a person. Everything routine clears in milliseconds, and you don't build a review queue to make it happen.