sellAI
alt_route

AI Gateway

See, cache, slow down and protect every AI request — without changing your model code.

23+Supported providers
1URL change to enable
AllPlans included
lightbulb

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.

router

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.

help

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.

monitoring

See everything

Dashboards show request counts, token usage and cost per app, plus real-time logs of every call.

cached

Cache repeats

If the same question comes in again, serve the saved answer from Cloudflare instead of paying the provider again.

speed

Rate limit

Cap how many requests can come through, so one user (or a bug) can't run up a huge bill.

restart_alt

Retry & fallback

If a provider fails, automatically retry or fall back to another provider so your app keeps working.

target

When should you use it?

attach_money

Watching costs

When you need to know exactly how much your AI features cost and where the spend goes.

shield

Going to production

Before real users arrive, add rate limits and fallbacks so a spike or outage can't break things.

swap_horiz

Using many providers

When you mix OpenAI, Anthropic and others and want one place to manage them all.

bug_report

Debugging prompts

Real-time logs let you replay exactly what was sent and what came back.

rocket_launch

How do you start?

  1. 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.

  2. 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.

    text
    https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider}
  3. 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.

    bash
    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.

bashFallback request
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" }] }
    }
  ]'
school

Key concepts

hub

Universal Endpoint

One URL that can reach any supported provider — and try several in order as a fallback chain.

lan

Provider

An AI service such as OpenAI, Anthropic or Workers AI. AI Gateway supports 23+ of them.

cached

Caching

Storing a previous answer so identical future requests are free and instant.

token

Token & cost tracking

Tokens (詞元) are the chunks of text models bill by. The gateway tallies them so you can see spend.

tips_and_updates

Tips & billing

check_circle

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.