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.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.Install
The AI SDK, zod and the GetABrain Node SDK.
npm install ai zod @getabrain/sdk3.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.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.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.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