TokenPolice
Docs
Integrations

Vercel AI SDK

The Vercel AI SDK is metered out of the box in Node; community providers and bundled runtimes need one wrapper, and tool calls are always manual.

The Vercel AI SDK is a Node integration. On the base install, the ai package and the first-party @ai-sdk/* providers (OpenAI, Anthropic, Google, Groq, Mistral, xAI, the gateway) are discovered and metered automatically — including non-chat calls like embeddings, images, speech and transcription. Init as usual — see init parameters — and call generateText / streamText exactly as you do today.

Community providers

A community @ai-sdk-style provider isn't auto-discovered — it bundles its own nested copy of the provider packages. Hand it to init():

import { someCommunityProvider } from "ai-sdk-community-provider";

tp.init({
  /* … see init parameters … */
  instrumentModules: { aiSdkProviders: [someCommunityProvider] },
});

aiSdkProviders is an array and accepts provider factories, constructed model instances, or package namespaces — pass whichever you have.

Bundled or edge runtimes

Where module patching can't reach — a bundled build, an edge runtime — wrap the model with the middleware instead:

import { wrapLanguageModel } from "ai";
import { openai } from "@ai-sdk/openai";
import * as tp from "token-police";

const model = wrapLanguageModel({
  model: openai("gpt-4o-mini"),
  middleware: tp.tokenPoliceAiSdkMiddleware(),
});

Tools are always manual

Tool execute functions are never captured automatically here — unlike LangChain or the OpenAI Agents SDK. The model calls around them are metered and enforced as normal; the tool steps just don't appear on their own in the trace.

Under enforce, a blocked streamText surfaces through the stream's error path (onError, or a rejected promise) rather than throwing at call time; a blocked generateText throws directly. Either way it is a TokenPoliceBlockedError — see Errors.

Reroute does not switch the model on the Vercel AI SDK

A Reroute rule does not switch the model on the Vercel AI SDK — those calls still run on the original model. Metering and blocking work normally. See Actions.

Next