Cloudflare AI Gateway
Cloudflare AI Gateway is a proxy layer that sits in front of other AI providers. It adds caching, rate limiting, cost tracking, and observability — without changing the underlying model or provider.Cloudflare AI Gateway does not host models itself. It proxies requests to other providers (OpenAI, Anthropic, etc.). You still need credentials for the underlying provider.
Prerequisites
Create a Cloudflare AI Gateway
In the Cloudflare dashboard, go to AI → AI Gateway and create a new gateway. Note your account ID and gateway name.
Configuration
ThebaseUrl must include your Cloudflare account ID and gateway name:
apiKey is the name of the environment variable, not the key value itself. Keep secrets in .env, not in committed config files.Selecting a model
The model reference uses thecloudflare-ai-gateway/ prefix:
<upstream-provider>/<model-id>.
What Cloudflare AI Gateway adds
| Feature | Behaviour |
|---|---|
| Caching | Identical requests return cached responses, reducing cost and latency |
| Rate limiting | Configurable per-gateway request limits |
| Observability | Request logs, latency metrics, and cost estimates in the Cloudflare dashboard |
| Fallbacks | Route to a secondary provider if the primary fails |
Troubleshooting
404 — gateway not found
404 — gateway not found
The
baseUrl account ID or gateway name is incorrect. Double-check both values in the Cloudflare dashboard.401 Unauthorized
401 Unauthorized
Upstream provider error
Upstream provider error
Cloudflare AI Gateway forwards errors from the underlying provider. Check that your upstream provider credentials are valid and that the model ID is correct for that provider.