Skip to content
Cloudflare Docs

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?',
});

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.',
});