AI Gateway
See, cache, slow down and protect every AI request — without changing your model code.
What is AI Gateway?
AI Gateway is a smart middleman that sits between your app and whatever AI provider you use (OpenAI, Anthropic, Workers AI and more). Every request passes through it, so you instantly get logs, analytics, caching and rate limiting.
You don't rewrite your app. You just point your existing AI calls at the gateway's URL instead of the provider's URL — that's the whole setup.
Think of it like a smart power strip
Plug all your appliances (AI providers) into one smart strip. Now you can see how much each uses, switch one off if it draws too much, and remember the last setting so you don't waste power. AI Gateway does that for AI requests.
Why use it?
Once your app starts calling AI, you quickly hit four worries: How much is this costing? Is it slow? What if the provider goes down? Are people abusing it? AI Gateway answers all four.
See everything
Dashboards show request counts, token usage and cost per app, plus real-time logs of every call.
Cache repeats
If the same question comes in again, serve the saved answer from Cloudflare instead of paying the provider again.
Rate limit
Cap how many requests can come through, so one user (or a bug) can't run up a huge bill.
Retry & fallback
If a provider fails, automatically retry or fall back to another provider so your app keeps working.
When should you use it?
Watching costs
When you need to know exactly how much your AI features cost and where the spend goes.
Going to production
Before real users arrive, add rate limits and fallbacks so a spike or outage can't break things.
Using many providers
When you mix OpenAI, Anthropic and others and want one place to manage them all.
Debugging prompts
Real-time logs let you replay exactly what was sent and what came back.
How do you start?
Create a gateway
In the Cloudflare dashboard, open AI Gateway and create one. You'll get an account ID and a gateway name to use in the URL.
Swap the base URL
Replace your provider's URL with the gateway URL. The pattern is below — your real provider call goes after the gateway part.
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider}Send a request
Here we route a request to OpenAI through the gateway. Notice only the host changed — the rest is the normal OpenAI call.
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \ -H "Authorization: Bearer {openai_token}" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini", "messages": [{ "role": "user", "content": "What is Cloudflare?" }] }'
Bonus: automatic fallback
The Universal Endpoint accepts a list of providers. If the first one fails, the gateway automatically tries the next — great for resilience.
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id} \
-H "Content-Type: application/json" \
-d '[
{
"provider": "workers-ai",
"endpoint": "@cf/meta/llama-3.1-8b-instruct",
"headers": { "Authorization": "Bearer {cf_token}" },
"query": { "messages": [{ "role": "user", "content": "Hi" }] }
},
{
"provider": "openai",
"endpoint": "chat/completions",
"headers": { "Authorization": "Bearer {openai_token}" },
"query": { "model": "gpt-4o-mini", "messages": [{ "role": "user", "content": "Hi" }] }
}
]'Key concepts
Universal Endpoint
One URL that can reach any supported provider — and try several in order as a fallback chain.
Provider
An AI service such as OpenAI, Anthropic or Workers AI. AI Gateway supports 23+ of them.
Caching
Storing a previous answer so identical future requests are free and instant.
Token & cost tracking
Tokens (詞元) are the chunks of text models bill by. The gateway tallies them so you can see spend.
Tips & billing
Available on every plan
AI Gateway is included on all Cloudflare plans, including the free one. Adding it is usually just a one-line URL change.
- Turn on caching first — it often gives the biggest instant savings.
- Set rate limits early so a runaway loop can't drain your budget.
- Use the real-time logs to debug bad prompts before users notice.
- Pair it with Workers AI or Agents for a fully managed AI stack.
Related products
menu_bookOfficial docsopen_in_new