Recipe · Vercel AI SDK

AI SDK tool approval, decided by real people

To add a human in the loop to the Vercel AI SDK, set toolApproval: { yourTool: 'user-approval' } on generateText or streamText. The call finishes without running the tool and returns a tool-approval-request; you add a tool-approval-response to the messages and call again. The SDK leaves the deciding to you. This recipe hands the decision to three verified US people through GetABrain and passes their verdict back in.

GetABrain: human feedback for any AI project, from verified real people. How answers are verified

  1. 1.Get a test key ($5 sandbox credit)

    Sign up as a requestor and mint a test-mode key. It is a normal gab_k_ / gab_s_ pair that returns simulated answers, paid from the $5 sandbox credit first, so you can wire the whole loop before any real person sees a question.

  2. 2.Install

    The AI SDK, zod and the GetABrain Node SDK.

    npm install ai zod @getabrain/sdk
  3. 3.Mark the tool for approval

    toolApproval sits on generateText, streamText or ToolLoopAgent. 'user-approval' means: emit an approval request and wait for an explicit answer. The older needsApproval property on tool() still works but is deprecated.

    import { generateText, tool, type ModelMessage, type ToolApprovalResponse } from 'ai'
    import { z } from 'zod'
    
    const pruneSubscribers = tool({
      description: 'Delete newsletter subscribers who have not opened an email in N months',
      inputSchema: z.object({ months: z.number() }),
      execute: async ({ months }) => deleteInactiveSubscribers(months),
    })
    
    const settings = {
      model: 'anthropic/claude-sonnet-5.5',
      tools: { pruneSubscribers },
      toolApproval: { pruneSubscribers: 'user-approval' as const },
    }
    const messages: ModelMessage[] = [{ role: 'user', content: 'Clean up the newsletter list.' }]
    const first = await generateText({ ...settings, messages })
    messages.push(...first.responseMessages)
  4. 4.Ask GetABrain, send the approval back

    Every tool-approval-request in result.content becomes a yes/no question for 3 people. A majority yes approves it; the reason carries the count for your logs, and a denial reason goes to the model so it can explain itself. The quickstart covers keys and the other answer formats.

    import { GetABrain } from '@getabrain/sdk'
    
    const gab = new GetABrain({ apiKey: process.env.GAB_KEY!, apiSecret: process.env.GAB_SECRET! })
    const approvals: ToolApprovalResponse[] = []
    
    for (const part of first.content) {
      if (part.type !== 'tool-approval-request' || part.isAutomatic) continue
      const query = await gab.queries.create({
        type: 'yes_no',
        title: 'Approve this agent action?',
        content_data: {
          question: `An AI agent wants to run ${part.toolCall.toolName} with ${JSON.stringify(part.toolCall.input)}. Should it go ahead?`,
          require_explanation: true,
        },
        required_responses: 3,
        bid_amount_cents: 25,
      })
      const answers = await gab.queries.waitForResponses(query.id, { minResponses: 3, timeoutMs: 900_000 })
      const yes = answers.filter((a) => a.response_data.answer === 'yes').length
      approvals.push({
        type: 'tool-approval-response',
        approvalId: part.approvalId,
        approved: yes * 2 > answers.length,
        reason: `${yes} of ${answers.length} people said yes`,
      })
    }
    
    messages.push({ role: 'tool', content: approvals })
    const second = await generateText({ ...settings, messages })
  5. 5.What came back (sandbox run, simulated answers)

    We asked this on 2026-12-03 with a test-mode key, so the answers are simulated, not real people, and we wrote the question in plain words. The shape matches live answers. Sandbox explanations are placeholder text, so we show only the votes.

    question: "An AI agent wants to delete 214 newsletter subscribers who have
               not opened an email in 12 months. Should it go ahead?"
    
    response_data.answer from 3 answers: "yes", "no", "yes"
    tool-approval-response: { approved: true, reason: "2 of 3 people said yes" }
  6. 6.When the agent needs an answer, not a yes

    The AI SDK docs say the approval reason is not a channel for passing data into the tool. If the agent needs a value from a person, give it a tool whose execute calls GetABrain and returns the answer, or connect the GetABrain MCP server. Python agents: see the LangGraph and LangChain recipes.

Sources (2)
  • toolApproval on generateText/streamText/ToolLoopAgent with 'user-approval'; result.content holds tool-approval-request parts (approvalId, toolCall, isAutomatic): ai-sdk.dev · checked 2026-12-03
  • Respond with { type: 'tool-approval-response', approvalId, approved, reason } in a role 'tool' message, then call again; needsApproval is deprecated: ai-sdk.dev · checked 2026-12-03
Pass toolApproval: { toolName: 'user-approval' } to generateText, streamText or ToolLoopAgent. The call returns tool-approval-request parts instead of running the tool. Push a { type: 'tool-approval-response', approvalId, approved } into the messages and call again; the tool runs if approved.
The needsApproval property on tool() is deprecated in the current AI SDK docs. Existing code still works; new code should put the policy in toolApproval, which also takes a function for input-dependent approval.
No. It completes and returns the approval requests, so approval takes two model calls: one that proposes the tool call, one after your answer. Nothing has to stay running while people answer.
Verified US workers on GetABrain, paid per answer. Test-mode keys never reach them: you get simulated answers in the same format.
Test mode answers are simulated and paid from the $5 sandbox credit. Live yes/no answers start at $0.05 each plus a 20% platform fee, prepaid, no subscription required.