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.
npm install ai-gateway-providerWith 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.',});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}',});
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.',});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.',});Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-