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

With Key in Request

import { createAiGateway } from 'ai-gateway-provider';
import { createOpenAI } from 'ai-gateway-provider/providers/openai';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const openai = createOpenAI({ apiKey: '{OPENAI_API_KEY}' });
const { text } = await generateText({
model: aigateway(openai.chat("gpt-5.1")),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

With Stored Keys (BYOK) / Unified Billing

import { createAiGateway } from 'ai-gateway-provider';
import { createOpenAI } from 'ai-gateway-provider/providers/openai';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const openai = createOpenAI();
const { text } = await generateText({
model: aigateway(openai.chat("gpt-5.1")),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

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