Vercel AI SDK
Use the AnonRouter provider for Vercel AI SDK 7 for private ticketed chat, streaming, tool calls, and embeddings.
Use @anonrouter/ai-sdk-provider from a server-side JavaScript or TypeScript
application. It uses the private ticket flow by default. The
inference key stays on the control origin, and each inference request carries
a single-use ticket.
Install and configure
These examples require Vercel AI SDK 7 and Node.js 22 or newer.
npm install ai@7 @anonrouter/ai-sdk-providerCreate an inference key in your dashboard and set ANONROUTER_API_KEY in
your server environment. Keep the key on your server. Choose a model from the
model catalog. For the tool example, choose a
route that supports tool calling.
Create provider.ts in your server code.
import { anonrouter } from '@anonrouter/ai-sdk-provider';
export const model = anonrouter('deepseek/deepseek-v3.2');Set maxRetries: 0 on AI SDK calls. A disconnect can happen after a request
has been charged, so repeating it can create another charge.
Stream an answer
Run this in your server application, using provider.ts above:
import { streamText } from 'ai';
import { model } from './provider';
const result = streamText({
model,
prompt: 'Describe a quiet morning in two sentences.',
maxOutputTokens: 256,
maxRetries: 0,
onError: () => {
process.stderr.write('The model stream failed.\n');
process.exitCode = 1;
},
});
for await (const text of result.textStream) {
process.stdout.write(text);
}This prints the answer for a terminal demonstration. In a web app, send it to the user instead of recording it in server logs. Handle stream errors separately; partial text is not proof that a request completed successfully. See the streaming reference.
Run a bounded tool loop
Your application executes tools. AnonRouter forwards the model's tool calls and
the results you send back. Install zod@4 for this unit conversion example.
import { generateText, isStepCount, tool } from 'ai';
import { z } from 'zod';
import { model } from './provider';
const result = await generateText({
model,
prompt: 'Use the conversion tool to convert 21 Celsius to Fahrenheit.',
maxOutputTokens: 256,
maxRetries: 0,
stopWhen: isStepCount(2),
tools: {
celsiusToFahrenheit: tool({
description: 'Convert a temperature from Celsius to Fahrenheit.',
inputSchema: z.object({ celsius: z.number().min(-273.15).max(10000) }),
execute: async ({ celsius }) => ({ fahrenheit: celsius * 9 / 5 + 32 }),
}),
},
});
if (!result.text) throw new Error('No final answer within the two-step limit.');
process.stdout.write(result.text);The loop allows at most two model steps, including the follow-up after a tool result. Each model request uses your account's available funding. Keep tools narrowly scoped and validate inputs before accessing files or services. See the tool-calling reference.
Create an embedding
import { anonrouter } from '@anonrouter/ai-sdk-provider';
import { embed } from 'ai';
const { embedding } = await embed({
model: anonrouter.embedding('qwen/qwen3-embedding-0.6b'),
value: 'The Pacific Ocean is the largest ocean on Earth.',
maxRetries: 0,
});Choose how a request is routed
Set a policy when you select a model, or for a single call under
providerOptions.anonrouter. This model selection requires a TEE route and
allows one provider attempt.
import { anonrouter } from '@anonrouter/ai-sdk-provider';
const model = anonrouter('deepseek/deepseek-v3.2', {
provider: { minimum_privacy: 'tee', allow_fallbacks: false },
});Read result.providerMetadata?.anonrouter for the selected model, provider,
privacy tier, and cost when the API returns them. See provider routing
for policy fields and reasoning controls for model-specific effort settings.
For managed web search, choose an eligible model and set webSearch: { maxUses: 1 }
when you select it. The adapter exposes the search summary in
providerMetadata.anonrouter.webSearch. See web search for
eligible routes, limits, and pricing.
Compatibility mode
For an existing bearer-key integration or cross-model fallback, create the
provider with mode: 'compatibility' and a compatibility-enabled inference key.
import { createAnonRouter } from '@anonrouter/ai-sdk-provider';
const anonrouter = createAnonRouter({ mode: 'compatibility' });In this mode the broker receives the API-key identity and request content
together in memory. Pass models as an ordered list of up to four fallback
model IDs if you need cross-model fallback. See compatibility mode
for supported endpoints and key settings.
For independent attestation or end-to-end encryption, use the confidential SDK.
Troubleshooting
- 401 or 403. Confirm that the server has an inference key. If you chose compatibility mode, confirm that OpenAI-compatible access is enabled for it.
- Unknown model. Copy a current model ID from the catalog.
- Tool calls fail. Check the selected route's tool support and your tool schema.
See the npm package for factory settings and additional examples.