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/troubleshooting.md
1# AI Gateway Troubleshooting23## Common Errors45| Error | Cause | Fix |6|-------|-------|-----|7| 401 | Missing `cf-aig-authorization` header | Add header with CF API token |8| 403 | Invalid provider key / BYOK expired | Check provider key in dashboard |9| 429 | Rate limit exceeded | Increase limit or implement backoff |1011### 401 Fix1213```typescript14const client = new OpenAI({15baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`,16defaultHeaders: { 'cf-aig-authorization': `Bearer ${CF_API_TOKEN}` }17});18```1920### 429 Retry Pattern2122```typescript23async function requestWithRetry(fn, maxRetries = 3) {24for (let i = 0; i < maxRetries; i++) {25try { return await fn(); }26catch (e) {27if (e.status === 429 && i < maxRetries - 1) {28await new Promise(r => setTimeout(r, Math.pow(2, i) * 1000));29continue;30}31throw e;32}33}34}35```3637## Gotchas3839| Issue | Reality |40|-------|---------|41| Metadata limits | Max 5 entries, flat only (no nesting) |42| Cache key collision | Use unique keys per expected response |43| BYOK + Unified Billing | Mutually exclusive |44| Rate limit scope | Per-gateway, not per-user (use dynamic routing for per-user) |45| Log delay | 30-60 seconds normal |46| Streaming + caching | **Incompatible** |47| Model name (unified API) | Prefix required: `openai/gpt-4o`, not `gpt-4o` |4849## Cache Not Working5051**Causes:**52- Different request params (temperature, etc.)53- Streaming enabled54- Caching disabled in settings5556**Check:** `response.headers.get('cf-aig-cache-status')` → HIT or MISS5758## Logs Not Appearing59601. Check logging enabled: Dashboard → Gateway → Settings612. Remove `cf-aig-collect-log: false` header623. Wait 30-60 seconds634. Check log limit (10M default)6465## Debugging6667```bash68# Test connectivity69curl -v https://gateway.ai.cloudflare.com/v1/{account}/{gateway}/openai/models \70-H "Authorization: Bearer $OPENAI_KEY" \71-H "cf-aig-authorization: Bearer $CF_TOKEN"72```7374```typescript75// Check response headers76console.log('Cache:', response.headers.get('cf-aig-cache-status'));77console.log('Request ID:', response.headers.get('cf-ray'));78```7980## Analytics8182Dashboard → AI Gateway → Select gateway8384**Metrics:** Requests, tokens, latency (p50/p95/p99), cache hit rate, costs8586**Log filters:** `status: error`, `provider: openai`, `cost > 0.01`, `duration > 1000`8788**Export:** Logpush to S3/GCS/Datadog/Splunk89