Skip to content

Vercel AI SDK

The Vercel AI SDK is a TypeScript library for building AI applications. The SDK supports many different AI providers, tools for streaming completions, and more. To use Cloudflare AI Gateway with Vercel AI SDK, you will need to use the ai-gateway-provider package.

Installation

Terminal window
npm install ai-gateway-provider

Examples

Make a request to
OpenAI
Unified
API with
Stored Key (BYOK)
import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('openai/gpt-5.2')),
prompt: 'What is Cloudflare?',
});

AI binding with third-party models

If you are already using the workers-ai-provider package, you can route requests through AI Gateway to call third-party models without needing separate provider SDKs. Pass a gateway option with your gateway ID to createWorkersAI:

JavaScript
import { createWorkersAI } from "workers-ai-provider";
import { streamText } from "ai";
export default {
async fetch(request, env) {
const workersai = createWorkersAI({
binding: env.AI,
gateway: { id: "my-gateway" },
});
const result = streamText({
model: workersai("openai/gpt-4o"),
messages: [{ role: "user", content: "Write a short story" }],
});
return result.toTextStreamResponse();
},
};

This works with any supported provider and model available through AI Gateway.

Fallback Providers

To specify model or provider fallbacks to handle request failures and ensure reliability, you can pass an array of models to the model option.

const { text } = await generateText({
model: aigateway([openai.chat("gpt-5.1"), anthropic("claude-sonnet-4-5")]),
prompt: "Write a vegetarian lasagna recipe for 4 people.",
});