Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/ai-gateway/sdk-integration.md
1# AI Gateway SDK Integration23## Vercel AI SDK (Recommended)45```typescript6import { createAiGateway } from 'ai-gateway-provider';7import { createOpenAI } from '@ai-sdk/openai';8import { generateText } from 'ai';910const gateway = createAiGateway({11accountId: process.env.CF_ACCOUNT_ID,12gateway: process.env.CF_GATEWAY_ID,13apiKey: process.env.CF_API_TOKEN // Optional for auth gateways14});1516const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });1718// Single model19const { text } = await generateText({20model: gateway(openai('gpt-4o')),21prompt: 'Hello'22});2324// Automatic fallback array25const { text } = await generateText({26model: gateway([27openai('gpt-4o'),28anthropic('claude-sonnet-4-5'),29openai('gpt-4o-mini')30]),31prompt: 'Complex task'32});33```3435### Options3637```typescript38model: gateway(openai('gpt-4o'), {39cacheKey: 'my-key',40cacheTtl: 3600,41metadata: { userId: 'u123', team: 'eng' }, // Max 5 entries42retries: { maxAttempts: 3, backoff: 'exponential' }43})44```4546## OpenAI SDK4748```typescript49const client = new OpenAI({50apiKey: process.env.OPENAI_API_KEY,51baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`,52defaultHeaders: { 'cf-aig-authorization': `Bearer ${cfToken}` }53});5455// Unified API - switch providers via model name56model: 'openai/gpt-4o' // or 'anthropic/claude-sonnet-4-5'57```5859## Anthropic SDK6061```typescript62const client = new Anthropic({63apiKey: process.env.ANTHROPIC_API_KEY,64baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/anthropic`,65defaultHeaders: { 'cf-aig-authorization': `Bearer ${cfToken}` }66});67```6869## Workers AI Binding7071```toml72# wrangler.toml73[ai]74binding = "AI"75[[ai.gateway]]76id = "my-gateway"77```7879```typescript80await env.AI.run('@cf/meta/llama-3-8b-instruct',81{ messages: [...] },82{ gateway: { id: 'my-gateway', metadata: { userId: '123' } } }83);84```8586## LangChain / LlamaIndex8788```typescript89// Use OpenAI SDK pattern with custom baseURL90new ChatOpenAI({91configuration: {92baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`93}94});95```9697## HTTP / cURL9899```bash100curl https://gateway.ai.cloudflare.com/v1/{account}/{gateway}/openai/chat/completions \101-H "Authorization: Bearer $OPENAI_KEY" \102-H "cf-aig-authorization: Bearer $CF_TOKEN" \103-H "cf-aig-metadata: {\"userId\":\"123\"}" \104-d '{"model":"gpt-4o","messages":[...]}'105```106107## Headers Reference108109| Header | Purpose |110|--------|---------|111| `cf-aig-authorization` | Gateway auth token |112| `cf-aig-metadata` | JSON object (max 5 keys) |113| `cf-aig-cache-ttl` | Cache TTL in seconds |114| `cf-aig-skip-cache` | `true` to bypass cache |115