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/README.md
1# Cloudflare AI Gateway23Expert guidance for implementing Cloudflare AI Gateway - a universal gateway for AI model providers with analytics, caching, rate limiting, and routing capabilities.45## When to Use This Reference67- Setting up AI Gateway for any AI provider (OpenAI, Anthropic, Workers AI, etc.)8- Implementing caching, rate limiting, or request retry/fallback9- Configuring dynamic routing with A/B testing or model fallbacks10- Managing provider API keys securely with BYOK11- Adding security features (guardrails, DLP)12- Setting up observability with logging and custom metadata13- Debugging AI Gateway requests or optimizing configurations1415## Quick Start1617**What's your setup?**1819- **Using Vercel AI SDK** → Pattern 1 (recommended) - see [sdk-integration.md](./sdk-integration.md)20- **Using OpenAI SDK** → Pattern 2 - see [sdk-integration.md](./sdk-integration.md)21- **Cloudflare Worker + Workers AI** → Pattern 3 - see [sdk-integration.md](./sdk-integration.md)22- **Direct HTTP (any language)** → Pattern 4 - see [configuration.md](./configuration.md)23- **Framework (LangChain, etc.)** → See [sdk-integration.md](./sdk-integration.md)2425## Pattern 1: Vercel AI SDK (Recommended)2627Most modern pattern using official `ai-gateway-provider` package with automatic fallbacks.2829```typescript30import { createAiGateway } from 'ai-gateway-provider';31import { createOpenAI } from '@ai-sdk/openai';32import { generateText } from 'ai';3334const gateway = createAiGateway({35accountId: process.env.CF_ACCOUNT_ID,36gateway: process.env.CF_GATEWAY_ID,37});3839const openai = createOpenAI({40apiKey: process.env.OPENAI_API_KEY41});4243// Single model44const { text } = await generateText({45model: gateway(openai('gpt-4o')),46prompt: 'Hello'47});4849// Automatic fallback array50const { text } = await generateText({51model: gateway([52openai('gpt-4o'), // Try first53anthropic('claude-sonnet-4-5'), // Fallback54]),55prompt: 'Hello'56});57```5859**Install:** `npm install ai-gateway-provider ai @ai-sdk/openai @ai-sdk/anthropic`6061## Pattern 2: OpenAI SDK6263Drop-in replacement for OpenAI API with multi-provider support.6465```typescript66import OpenAI from 'openai';6768const client = new OpenAI({69apiKey: process.env.OPENAI_API_KEY,70baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/compat`,71defaultHeaders: {72'cf-aig-authorization': `Bearer ${cfToken}` // For authenticated gateways73}74});7576// Switch providers by changing model format: {provider}/{model}77const response = await client.chat.completions.create({78model: 'openai/gpt-4o', // or 'anthropic/claude-sonnet-4-5'79messages: [{ role: 'user', content: 'Hello!' }]80});81```8283## Pattern 3: Workers AI Binding8485For Cloudflare Workers using Workers AI.8687```typescript88export default {89async fetch(request, env, ctx) {90const response = await env.AI.run(91'@cf/meta/llama-3-8b-instruct',92{ messages: [{ role: 'user', content: 'Hello!' }] },93{94gateway: {95id: 'my-gateway',96metadata: { userId: '123', team: 'engineering' }97}98}99);100101return Response.json(response);102}103};104```105106## Headers Quick Reference107108| Header | Purpose | Example | Notes |109|--------|---------|---------|-------|110| `cf-aig-authorization` | Gateway auth | `Bearer {token}` | Required for authenticated gateways |111| `cf-aig-metadata` | Tracking | `{"userId":"x"}` | Max 5 entries, flat structure |112| `cf-aig-cache-ttl` | Cache duration | `3600` | Seconds, min 60, max 2592000 (30 days) |113| `cf-aig-skip-cache` | Bypass cache | `true` | - |114| `cf-aig-cache-key` | Custom cache key | `my-key` | Must be unique per response |115| `cf-aig-collect-log` | Skip logging | `false` | Default: true |116| `cf-aig-cache-status` | Cache hit/miss | Response only | `HIT` or `MISS` |117118## In This Reference119120| File | Purpose |121|------|---------|122| [sdk-integration.md](./sdk-integration.md) | Vercel AI SDK, OpenAI SDK, Workers binding patterns |123| [configuration.md](./configuration.md) | Dashboard setup, wrangler, API tokens |124| [features.md](./features.md) | Caching, rate limits, guardrails, DLP, BYOK, unified billing |125| [dynamic-routing.md](./dynamic-routing.md) | Fallbacks, A/B testing, conditional routing |126| [troubleshooting.md](./troubleshooting.md) | Debugging, errors, observability, gotchas |127128## Reading Order129130| Task | Files |131|------|-------|132| First-time setup | README + [configuration.md](./configuration.md) |133| SDK integration | README + [sdk-integration.md](./sdk-integration.md) |134| Enable caching | README + [features.md](./features.md) |135| Setup fallbacks | README + [dynamic-routing.md](./dynamic-routing.md) |136| Debug errors | README + [troubleshooting.md](./troubleshooting.md) |137138## Architecture139140AI Gateway acts as a proxy between your application and AI providers:141142```143Your App → AI Gateway → AI Provider (OpenAI, Anthropic, etc.)144↓145Analytics, Caching, Rate Limiting, Logging146```147148**Key URL patterns:**149- Unified API (OpenAI-compatible): `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions`150- Provider-specific: `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider}/{endpoint}`151- Dynamic routes: Use route name instead of model: `dynamic/{route-name}`152153## Gateway Types1541551. **Unauthenticated Gateway**: Open access (not recommended for production)1562. **Authenticated Gateway**: Requires `cf-aig-authorization` header with Cloudflare API token (recommended)157158## Provider Authentication Options1591601. **Unified Billing**: Use AI Gateway billing to pay for inference (keyless mode - no provider API key needed)1612. **BYOK (Store Keys)**: Store provider API keys in Cloudflare dashboard1623. **Request Headers**: Include provider API key in each request163164## Related Skills165166- [Workers AI](../workers-ai/README.md) - For `env.AI.run()` details167- [Agents SDK](../agents-sdk/README.md) - For stateful AI patterns168- [Vectorize](../vectorize/README.md) - For RAG patterns with embeddings169170## Resources171172- [Official Docs](https://developers.cloudflare.com/ai-gateway/)173- [API Reference](https://developers.cloudflare.com/api/resources/ai_gateway/)174- [Provider Guides](https://developers.cloudflare.com/ai-gateway/usage/providers/)175- [Discord Community](https://discord.cloudflare.com)176