Back to The Perch

Developer series, part 4

The check inside your framework

In part 1 our code called the refund function, so wrapping it in govern() was easy. Once a framework decides which tool runs and when, the check has to live where the framework makes that call. That's the job of an Enforce adapter.

When the framework holds the call

With the Vercel AI SDK, you hand the model a set of tools, and AI SDK calls a tool's execute when the model asks for it. You never write the line that calls billing.refund, so there's nowhere obvious to put govern().

You could wrap each execute by hand, and for a single tool that works. The adapter earns its place because it knows the framework's shape. For AI SDK it checks the generateText run before the request goes to the provider and again before the output is released. It checks each tool you build through it before the tool runs and before its result goes back to the model. It reports the run's token totals across every step, so VeriProof can price it. And it handles a detail that's easy to miss by hand: AI SDK records a tool error inside the generation result, so a stopped refund could otherwise look like an ordinary tool failure the model talks its way around.

So the split is simple. When your own code calls the action, like a webhook handler or a nightly job, use govern(). When the framework drives the calls, use its adapter. Plenty of apps end up with both.

The refund tool, AI SDK style

Here's the refund from part 1 as an AI SDK tool. ai.tool() takes the usual AI SDK tool options plus two of VeriProof's: the action name and the options you'd have passed to govern().

import { Veriproof } from '@veriproof/sdk-core';
import { createAISDKIntegration } from '@veriproof/sdk-ai-sdk';
import { tool } from 'ai';
import { z } from 'zod';
 
type RefundInput = { orderId: string; amount: number };
 
const ai = createAISDKIntegration(veriproof);
 
const issueRefund = ai.tool<RefundInput, RefundReceipt>({
  actionName: 'support.issue_refund',
  description: 'Refund an order for the customer on this ticket.',
  inputSchema: z.object({ orderId: z.string(), amount: z.number() }),
  governOptions: { review: { profile: 'support_refund_approval' } },
  execute: (input, _options, { invocationId }) =>
    billing.refund({ ...input, currency: 'USD', idempotencyKey: invocationId }),
});
 
const lookupOrder = tool({
  description: 'Look up an order by id.',
  inputSchema: z.object({ orderId: z.string() }),
  execute: ({ orderId }) => orders.get(orderId),
});

The type arguments name the tool's input and what billing.refund returns, so execute sees a typed input. Your execute also gets a third argument that plain AI SDK tools don't: the same context govern() hands your function, with invocationId for the idempotency key. The abortSignal in the second argument is VeriProof's stop signal, so pass it along to anything that can be cancelled.

lookupOrder is a plain AI SDK tool on purpose. It only reads, so it runs without a check, the same call we made in part 1. If you already have tools written with AI SDK's tool(), you don't have to rebuild them: pass a ToolSelection to createAISDKIntegration and it checks the ones you select in settings.tools.

Facts without a call site

In part 1 the facts sat right on the govern() call. Here the model picks the amount, so there's no call site to put them on. Register a factExtractor on the client instead, and it builds the facts from each call's input:

const veriproof = Veriproof.fromEnv(process.env, {
  factExtractor: async ({ actionName, input }) => {
    if (actionName !== 'support.issue_refund') return undefined;
    const { orderId, amount } = input as RefundInput;
    const order = await orders.get(orderId);
    return {
      refund_amount: amount,
      days_since_delivery: order.deliveredDaysAgo,
    };
  },
});

The extractor runs once per call, before the policy request. If it throws, the call ends failed with reason code fact_extraction_failed and the refund doesn't run. The facts are saved in the review checkpoint, so a resumed review uses those and doesn't call the extractor again. Part 3 goes into choosing them.

Run the agent and read what comes back

The model run is a check of its own, so you name it like any other action. The tools go in settings the way AI SDK expects:

try {
  const result = await ai.generateText(
    'support.reply',
    { ticketId, message: customerMessage },
    {
      settings: { model, tools: { issueRefund, lookupOrder } },
      buildPrompt: ({ input }) => ({ prompt: input.message }),
      selectResult: (generated) => ({ text: generated.text }),
    },
  );
  if ('value' in result) return reply(result.value.text);
  return reply("I can't help with that one, sorry.");
} catch (error) {
  if (!(error instanceof AISDKGovernanceInterruption)) throw error;
  const stopped = error.result;
  if (stopped.status === 'review_required') {
    await reviews.save(stopped.review as ReviewCheckpoint<RefundInput>);
    return reply("That's over my limit, so I've asked a manager.");
  }
  if (stopped.status === 'blocked') {
    return reply(`I can't issue that refund. ${stopped.message}`);
  }
  return reply("I couldn't check this refund right now, so I haven't issued it.");
}

buildPrompt gets the input after any rule has changed it, and selectResult picks the JSON value VeriProof checks before your app sees it. The result has the same statuses as part 1, and only the ones VeriProof released carry a value.

A refund that doesn't go ahead shows up differently. The adapter throws AISDKGovernanceInterruption from generateText once the run's record is closed, and error.result is the refund's own result, like review_required or blocked. The README is firm about one thing: don't turn that into a normal model reply. (The cast is there because the interruption types its result's input as plain JSON.)

Maya's $120 refund never reaches the catch. Daniel's $900 lands in the review_required branch, with the checkpoint in error.result.review.

Resuming Daniel's refund

That checkpoint belongs to support.issue_refund, the tool, so you resume the refund itself. It's the core review runtime from part 1, unchanged:

const resumed = await reviews.resume(checkpoint, ({ input, reviewResponse, invocationId }) =>
  billing.refund({
    ...input,
    amount: approvedAmount(reviewResponse, input.amount),
    currency: 'USD',
    idempotencyKey: invocationId,
  }),
);

The manager approves $600, VeriProof rechecks current facts and policy, and the refund runs with the approved amount. Then the agent tells Daniel what happened through whatever channel your app already uses.

The adapter also has ai.resume(). That's for the other case, when a rule sends the whole support.reply run to a person. It reruns generateText with the original input and hands the reviewer's response to your buildPrompt. Part 2 covers the parts of reviews this post skips, like expiries and duplicate callbacks.

Where the adapter stops

Every adapter documents exactly which framework operations it checks, and the AI SDK one is specific about its limits:

  • streamText, streamed tool results and UI streams aren't covered.
  • Provider-executed tools only get the run check. They have no local execute, so the adapter can't stop their side effect before it happens, which means a refund doesn't belong in one.
  • Running a ToolLoopAgent directly isn't covered.
  • A call you make straight through AI SDK next to the integration skips VeriProof entirely.

Other adapters draw the line somewhere else. The CrewAI adapter, for example, checks a complete crew kickoff and doesn't make separate decisions for the model and tool calls inside it. Read the row for your framework before you assume a tool is checked.

Anything past that line stays with your app, and the core SDK covers it. If you stream replies, keep the refund checked by putting a govern() call around billing.refund inside the tool's execute, the way part 1 did. The refund still gets its decision and its review. The streamed text isn't checked before it's shown, so decide which of the two actually needs a check.

The framework page shows which frameworks have an adapter, and the coverage guide lists what each one checks and what it leaves to you. Tools and SDKs in the portal has the install command for the packages enabled in your environment.

Earlier and later in the series

Part 3, facts your rules can use, covers the input and facts that factExtractor builds. Next, part 5 looks at agents you can't add code to at all: Copilot Studio, computer use, MCP and the VeriProof Agent Gateway.

Put a check in your framework

Pick your framework's adapter, build one tool through it, and watch the decision land in the portal. Developer is free for 60 days.