Network Architecture for AI Products Going Global: Model Calls, AI Gateway, and Edge Caching
For global AI products, latency lives in cross-border round trips and model inference, not page rendering. Split your architecture into edge access, an AI Gateway, and the model layer — and know that gateway caching only works on byte-identical requests.
Short answer: Ship the network layer for a global AI product as three separate concerns: put user access at the edge near your users, funnel every model call through an AI Gateway as the single egress, and apply caching and fallback per request type. Gateway caching is off by default and only matches byte-identical requests — the cache key is a SHA-256 of provider, endpoint, model, auth header and full request body — so it cuts latency and token spend on template-like traffic but does nothing for personalized requests. Semantic deduplication has to live in your application. Configure model fallbacks with Dynamic Routing instead of hardcoding them, and note that the old Universal Endpoint is deprecated for new integrations.
Where the time actually goes in a global AI product
The latency users feel comes from cross-border round trips plus model inference — both happen after the HTML renders. A conventional CDN cannot fix either, because AI responses are not cacheable by default.
| Segment | Typical magnitude | Can a traditional CDN help? |
|---|---|---|
| Static assets (pages, JS, model listings) | Tens of milliseconds on a CDN hit | Yes — this is CDN’s core job |
| User to inference entry point | Hundreds of milliseconds of jitter on cross-border paths | Partially: edge access removes international-path variance |
| Model inference (streaming LLM generation) | Hundreds of milliseconds to tens of seconds, scaling with output tokens | Not cacheable; needs closer compute or smaller models |
| Repeated requests re-inferred | Same as first call | Yes — a cache hit skips inference entirely |
A common misdiagnosis: blaming origin performance and scaling the origin up. If users are global but model calls concentrate on a single-region third-party provider, the bottleneck is the path and the inference, not origin CPU. Scaling adds cost without moving time-to-first-token.
Cloudflare’s network spans 330+ cities across 100+ countries. The point of the access layer is to shorten the “user → gateway” leg and stop the unpredictable international transit at the gateway.
How the three layers cooperate: edge access, AI Gateway, model layer
Split the stack into three layers, each with one job:
| Layer | What lives here | What it solves | What does not belong here |
|---|---|---|---|
| Edge access | CDN, human verification (Turnstile), basic rate limiting | Stop scanners, abusive crawlers and obvious misuse; serve static content nearby | Not for precise per-token cost quotas |
| AI Gateway | Caching, rate limiting, spend limits, model fallback, logs and analytics | Single egress, observability, attributable billing | Don’t store business semantics (tier, plan) only in the gateway |
| Model layer | Workers AI / self-hosted inference / third-party models | Actual inference compute | Don’t let the app connect to multiple providers with its own retry logic per provider |
AI Gateway is available on all plans, so validating the architecture costs almost nothing: point your inference base URL at the gateway and request logs plus cost attribution appear immediately.
The only rule that matters for the split: push repeatable requests forward (cache), pull non-repeatable requests into a single controlled egress (gateway and fallbacks). Many teams do the reverse — caching in application memory, which means instances never share hits and multi-region deployments dilute the hit rate further.
What edge caching saves — and what it cannot
Three verified facts define what AI Gateway caching is good for:
- It is disabled by default and must be enabled in the gateway settings.
- Only text and image responses are supported, and only for identical requests. The cache key is the SHA-256 of provider, API endpoint, model name, authentication header and the full request body. One different field, timestamp or nonce in the body means a different cache entry.
- Hit or miss is visible in the response header
cf-aig-cache-status(HIT/MISS). The first thing to do after rollout is check the real hit rate from this header, not guesswork.
The resulting applicability map:
| Request type | Caching effect | Alternative |
|---|---|---|
| Guided conversations with fixed options (support openers, menu Q&A) | Good — limited option set, naturally repeated | Enable caching directly |
| Document RAG with the same retrieved context asked repeatedly | Partial | Exact-match cache plus application-level semantic cache |
| Personalized summaries, code generation, long-context writing | Almost none — cache keys are naturally distinct | Application-level semantic dedup, or shared-prefix reuse |
| Streaming responses (SSE) | Evaluate carefully | Confirm your call pattern is within supported scope first |
Semantic deduplication is not a gateway capability. The documentation is explicit that matching is currently exact, with semantic-search caching on the roadmap. Merging similar questions has to happen in your application: normalize and embed inputs, return an existing result when similarity clears a threshold. Do not assume flipping the cache switch is the win — that is the most common overestimate.
Spend limits and fallbacks: the gateway’s real job
Two control-plane capabilities matter even more than caching:
Spend limits: set dollar-denominated budgets scoped by model, provider or custom metadata. When cumulative spend crosses the limit within a time window, further requests get a 429 until the window resets. Each gateway supports up to 20 rules. Two caveats: costs are computed from official model pricing, so the unit is dollars, not requests; and accounting is eventually consistent — a concurrent burst can briefly overshoot before enforcement catches up. Keep the last-resort circuit breaker in your application ledger; do not treat the gateway budget as a hard real-time wall.
Dynamic routing: compose conditions, quotas and model fallbacks through a visual interface or JSON config — routing by user tier, percentage-based rollouts, and automatic switches to a fallback model when budget or rate limits trip. The older chain-of-providers approach via the Universal Endpoint is deprecated; new integrations should use the OpenAI-compatible endpoint plus Dynamic Routing. This matters especially for teams serving global users: third-party provider availability is outside your control, and the fallback strategy must be reconfigurable in seconds, not tied to a release cycle.
If you are weighing self-hosted compute against managed inference: Workers AI bills at $0.011 per 1,000 Neurons with a free allocation of 10,000 Neurons per day (resetting at 00:00 UTC daily); for llama-3.3-70b-instruct-fp8-fast that works out to $0.293 per million input tokens and $2.253 per million output tokens. Spiky, lightweight workloads on Workers AI avoid the fixed cost of idle GPUs; large, predictable volumes are usually cheaper self-hosted, fronted by the same gateway for unified egress.
Rollout order
- Gateway first, optimization later: converge all model-call egress on the AI Gateway. Within a week you will have real request volume, token distribution and cost attribution — every later decision depends on this data.
- Attach spend limits per endpoint class: start coarse — one global budget, then a separate rule per expensive endpoint (long context, image, video).
- Mine the logs for naturally cacheable traffic: look at request-body distributions; enable caching for conversation endpoints with limited option sets first, and watch the actual
cf-aig-cache-statushit rate. - Configure model fallbacks: primary model down or over budget → switch to backup via Dynamic Routing, not application code.
- Add semantic dedup at the application layer only after the logs confirm real repeat traffic — inverting this order is wasted work.
Common mistakes
- Assuming the cache switch saves money: exact-match caching yields near-zero hit rate on personalized traffic; savings come from identifying naturally repeated flows first.
- Treating the gateway as a firewall: AI Gateway is about call governance, not bot defense; malicious traffic stops at the edge access layer. Keep the two concerns separate.
- Multi-region deployments with per-instance caches: caching belongs at the gateway side, or scaling dilutes your hit rate.
- Hardcoding fallback logic in application code: price changes, rate limits and outages at providers are operational events; fallback must not ride the release train.
Boundaries and non-applicable cases
- Heavily personalized generation (every request unique): gateway caching contributes little. This architecture’s value is observability, fallbacks and budget control — don’t bank on cache numbers.
- Strict data-residency requirements: evaluate where requests transit and where logs are stored against your compliance obligations; this is not satisfied by default.
- Single-region users with self-hosted GPU clusters: if users and compute share a region, inserting a cross-region gateway adds a hop — direct connection is simpler.
- Hard real-time scenarios (realtime voice, interactive game inference): proxying adds a hop; evaluate compute placement instead of applying this architecture.
- Long-running async jobs (video generation, batch inference): fallback and caching semantics differ from synchronous requests; design queues and idempotency separately.
FAQ
Where should the origin live so model calls don’t take a detour? Put the origin and the model provider in the same major region (e.g. both US West) and hand user access to the edge network. The worst combination is origin in one region, provider in another, users in a third — every request runs three international legs.
Does AI Gateway cost extra or require a plan upgrade? AI Gateway is available on all plans, so validating the architecture starts at zero cost. What you pay for is the model usage behind it: Workers AI beyond the daily 10,000-Neuron free allocation bills at $0.011 per 1,000 Neurons, and third-party models bill with their respective providers.
The cache key includes the auth header — does BYOK mean zero hit rate? Yes. The key is derived from provider, endpoint, model, the Authorization header and the request body; a different auth header means a different key. To raise hit rates, authenticate uniformly at the gateway instead of letting each caller supply its own provider key.
Will users notice when a model fallback kicks in? They will. Output style and latency differ across models, so the switch is perceptible. Define fallback rules in advance — which endpoints may degrade, and to which model — rather than deciding mid-incident.
Related reading
Sources
Official Cloudflare documentation. Where this article mentions availability, quotas or limits, these pages are the source of truth. Cloudflare features change over time — confirm what is visible in your dashboard before committing to a design.
- developers.cloudflare.com/ai-gateway/
- developers.cloudflare.com/ai-gateway/features/caching/
- developers.cloudflare.com/ai-gateway/features/spend-limits/
- developers.cloudflare.com/ai-gateway/features/dynamic-routing/
- developers.cloudflare.com/ai-gateway/usage/universal/
- developers.cloudflare.com/workers-ai/platform/pricing/
- www.cloudflare.com/network-page/