skip to content

As the lead for services calling two model providers and Workers AI through Cloudflare AI Gateway, how would you set gateways, caching, failover and spend controls?

level: principalimportance: should knowfreq 16%

answer

  1. settings live per gateway
  2. what may be cached, and who decides
  3. retries for blips, routes for failover
  4. rate limit is gateway-wide
  5. budgets split by user metadata

basics

~20 s

Give each application and environment its own gateway, cache only non-personal prompts, keep provider keys in BYOK, use retries for blips and Dynamic Routing fallbacks for provider outages, and cap cost with spend limits split by user metadata.

solid answer

~50 s

I would start from the fact that every setting lives on a **gateway**, so the layout is the policy: one gateway per application and environment (an account holds 10 on Free, 20 on paid), each with its own caching, logging, Guardrails and limits. Caching stays off by default and is turned on only for named, non-personal prompts, with custom keys reviewed. Provider keys go into BYOK with **Require provider credentials** on, unless we choose Unified Billing deliberately. Gateway retries absorb short upstream errors; failover between providers is a **Dynamic Route** whose Model node has a fallback, decided per flow because a different model changes answers and data handling. The built-in rate limit is one cap for the whole gateway, so per-user cost control comes from **spend limits** split by a user metadata key, accepting that they are eventually consistent. Logs keep metadata everywhere, and payloads only where privacy allows.

go deeper

for a junior

Recall that AI Gateway settings live per gateway and that caching, rate limits and retries are separate controls.

for a middle

Explain the difference between gateway retries, which retry one provider, and Dynamic Route fallbacks, which switch models.

for a senior

Show the operational traps: gateway-wide rate limits, eventually consistent spend limits, silent Unified Billing fallback and account-scoped tokens.

for a principal

Own the layout and the policy: gateways per application and environment, what may be cached, which flows may fail over, and how cost is attributed and capped per user.

## The decision in front of the lead Several services call two external model providers and Workers AI. AI Gateway can give them one place for logs, cost control, caching and failover, but almost every control is configured **per gateway**, and some choices trade cost against correctness or privacy. There is no single right setup; these are the decisions and what each one costs. ## How many gateways - **One gateway per application and environment** keeps settings, logs, dynamic routes and spend rules separate: a staging experiment cannot change production's cache or budget. Dynamic routes belong to the gateway they were created on. - **One shared gateway** is simpler to watch but forces one caching policy, one rate limit and one Guardrails table on every service. - Ceilings: 10 gateways per account on the Free plan, 20 on a paid plan. - **Isolation is limited by the token**: AI Gateway permissions are account-scoped, so a `Run` token reaches every gateway. Tenants or teams that must not spend each other's keys need separate accounts or Workers with the AI binding. ## What may be cached Caching is off by default and replays whole responses to whoever maps to the same key, so the lead decides **which traffic may be cached at all**: - personal or account-specific prompts: never cached (`cf-aig-skip-cache: true`); - generic prompts with stable answers: cached with an explicit TTL (60 seconds to one month); - any `cf-aig-cache-key`: reviewed for what it covers, including the prompt version and model. Write down who may change a gateway's cache setting; for API tokens, that is the `AI Gateway Edit` permission. ## Keys and billing | Option | What it costs | When it fits | | --- | --- | --- | | BYOK stored keys | Provider pricing under your own contract | You need your provider agreements, aliases per environment | | Unified Billing | Provider rates passed through, plus a 5% fee on credit purchases; 200 requests per 60 seconds per gateway on Cloudflare-managed credentials | One bill, no provider accounts, Zero Data Retention routing where a model supports it | If BYOK is the policy, turn on **Require provider credentials** so a missing key fails with `400` instead of drifting onto Unified Billing. ## Failover and retries 1. **Gateway-level retries** (Retry Requests, up to 5 attempts with constant, linear or exponential backoff, overridable per request) absorb short upstream errors without client changes. They retry the same provider. 2. **Provider failover** is a **Dynamic Route**: a versioned flow named `dynamic/<route>` with Model nodes whose `fallback` output points at another model, plus conditional, percentage, rate and budget nodes. The response headers `cf-aig-model` and `cf-aig-provider` show which model answered. Dynamic routes take the OpenAI chat-completions shape only. 3. The older Universal Endpoint fallback array is deprecated; new work uses Dynamic Routing. Failover is a product decision, not only a reliability one. A fallback model answers differently, may handle data under different terms and may cost more. Decide per flow: a ticket summariser can fail over to Workers AI; a contract-review flow might rather fail loudly. ## Spend and rate controls - The gateway's **rate limiting** applies one requests-per-period cap, fixed or sliding, uniformly to every request through that gateway. It protects the bill from a runaway loop but cannot tell users apart. - **Spend limits** track dollar cost and return `429` when a budget is exceeded. A rule can be **split by value** on a metadata key such as `user_id` (sent in `cf-aig-metadata`) or `cf.user_id` when the gateway is behind Cloudflare Access, giving each user a separate budget. Up to 20 rules per gateway. - Spend limits are **eventually consistent**: cost is recorded after a request completes, so a concurrent burst can overshoot before enforcement catches up. Set budgets with that margin. - A spend limit on a primary model inside a Dynamic Route can **fall back to a cheaper model** instead of blocking. ## Logs and privacy Keep logs on for metadata (model, tokens, cost, status) everywhere, and decide where prompt and response **payloads** may be stored; `cf-aig-collect-log-payload: false` keeps the metrics without the text. Tag every call with service, team and user metadata, because spend rules, log filters and User Insights depend on it. Accounts that created their first gateway on or after September 24, 2026 follow Workers Logs pricing and retention, which belongs in the cost estimate. ## A defensible default One gateway per application and environment, authentication on, caching off except reviewed generic prompts, BYOK with Require provider credentials, retries on, Dynamic Routes only for flows that approved a fallback model, and per-user spend limits keyed on a metadata field every service must send.

  • Why not rely on the AI Gateway rate limit to stop one user from running up the bill?
    The gateway's rate limit is a single cap applied uniformly to all requests through that gateway, so one heavy user exhausts it for everyone. Per-user control comes from a spend limit split by a user metadata key, or a rate node in a Dynamic Route keyed on metadata, both of which need every service to send that metadata.
  • When would you choose Unified Billing over BYOK for a team?
    When the team has no provider contracts of its own, wants one Cloudflare bill and can live with the 5% fee on credits and the per-gateway request ceiling on Cloudflare-managed credentials. It also makes sense where a model offers Zero Data Retention routing, which applies only to Unified Billing traffic. A team bound to existing provider agreements stays on BYOK.

  • The gateway rate limit gives each user an independent request quota.
  • Gateway-level retries fail over to a different provider when one is down.
  • Spend limits stop a burst exactly at the budget, never above it.
  • An AI Gateway token can be restricted to one team's gateway.
  • Fallback to another model is a pure reliability win with no product impact.

Assumes Cloudflare's platform as of 2026-10-10: spend limits arrived in June 2026, Require provider credentials in September 2026, and Dynamic Routing replaces the deprecated Universal Endpoint for fallbacks.