AI Gateway: one front door for AI requests with logs, cache, and limits
AI Gateway (observe and control AI API traffic)
What it is
AI Gateway is a single front door in front of AI model calls. Instead of your app talking straight to OpenAI, Anthropic, Workers AI, or another provider, it sends the request through a named gateway on your Cloudflare account. Cloudflare then forwards it to the real model, and on the way it can log the call, cache repeated answers, rate-limit busy clients, retry failures, and apply simple guardrails.
You create a gateway under AI, then AI Gateway in the dashboard (Create Gateway; names are limited to 64 characters). Each gateway has an ID you put in the request URL or in a Worker binding option. There is also a special ID called default: if you omit a gateway ID or use default, Cloudflare can auto-create a default gateway on the first authenticated request. From a Worker you often reuse the same [ai] binding as Workers AI and pass a gateway option such as { gateway: { id: "default" } } on env.AI.run(...). From outside a Worker you call gateway.ai.cloudflare.com or the unified /ai REST routes with your account ID and a Cloudflare API token.
AI Gateway is not itself a model. Workers AI (yesterday’s lesson) runs open models on Cloudflare. AI Gateway sits in front of Workers AI and of outside providers, so one place can show you volume, errors, latency, and token use across every app that points at it. Features such as caching, rate limiting, retries, and guardrails are configured per gateway in Settings; leave them off until you understand what each one does.
Why it matters
Several of your clinic and side apps already call models (for example triage.kosan.xyz through OpenRouter). Without a gateway, each app has its own keys, its own logs, and no shared view of cost or failures. A gateway gives you one log of what was asked, what came back, how long it took, and whether anything failed, without changing the model you use.
It is also the place to put safety rails later: cache identical FAQ answers so you do not pay twice, rate-limit a public demo so one visitor cannot burn your budget, and keep identifiable patient text out of prompts. Treat every log as something that might contain sensitive wording, and never send real patient details through a learning gateway.
The Workers AI lesson already hinted that a stronger commercial model can sit behind the same front door. Today’s lesson is that front door: observe first, control second, and keep provider keys out of the browser.
On your account
From your own setup: triage.kosan.xyz currently calls an outside model through OpenRouter with a stored key. The AI Gateway shape would be: create a gateway named something like clinic-apps, point the Worker’s model calls through that gateway ID (or through default while learning), and read request volume and errors on the gateway’s Analytics or Logs page instead of hunting through each app separately.
A Worker that already has [ai] binding = "AI" can route Workers AI through the gateway with a third argument, for example env.AI.run("@cf/...", { messages: [...] }, { gateway: { id: "default" } }). The tutor itself does not do this: cloudflare.kosan.xyz is still fixed text in worker.js with no AI binding.
Nothing in this lesson creates a gateway for you, stores a provider key, turns on caching or rate limits, or changes cloudflare-tutor, ainews, aiweekly, triage, or any other Worker or DNS record.
Do this yourself
These stay in the dashboard, on your side. This site does not change DNS, routes, or other Workers.
- In the Cloudflare dashboard, open AI, then AI Gateway (wording may differ slightly), and just look: note whether any gateways already exist (including one named default). If none exist, create a throwaway gateway named exactly tutor-aigateway-lesson, open it and write down where Logs or Analytics appear and whether Caching and Rate limiting are on or off, then delete that same tutor-aigateway-lesson gateway. Do not point any live Worker at it, do not add provider credentials, do not enable Unified Billing spend, and do not change cloudflare-tutor, ainews, aiweekly, or triage.