TL;DR:
- Cloudflare AI Gateway proxies all your LLM API calls through a single endpoint, adding logging, caching, rate limiting, and fallback routing with minimal code changes.
- Semantic caching can reduce costs by 30–60% on agent workflows that repeat similar queries across users or sessions.
- It supports 30+ providers natively (OpenAI, Anthropic, Google, Mistral, Cohere, etc.) and works alongside existing SDK code.
Running an AI agent in production means dealing with problems that don’t exist in development: runaway costs when a loop misbehaves, no visibility into what your agent actually called and what it got back, no way to enforce rate limits per user or team, and no graceful fallback when your primary model is down. Cloudflare AI Gateway addresses all four without requiring you to change your model SDK code.
What Cloudflare AI Gateway Is
AI Gateway is a managed reverse proxy for LLM API calls. Instead of your agent calling https://api.openai.com/v1/chat/completions directly, it calls your AI Gateway endpoint: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/openai/chat/completions. From OpenAI’s perspective, it’s a normal API call. From your perspective, every call now passes through Cloudflare’s infrastructure.
The gateway speaks the native API formats for each provider — you swap the base URL and nothing else changes in your SDK code. For agents built with LangChain, LlamaIndex, Mastra, or direct SDK calls, this is typically a one-line change.
Logging and Observability
Every request and response through AI Gateway is logged, including:
- Full request and response bodies (or truncated summaries for very long contexts)
- Model, temperature, and parameter settings per call
- Token counts and latency
- Whether the response was served from cache
- Provider errors and status codes
This solves the problem of understanding what your agent is actually doing. In development you can add print statements; in production you often can’t, and the agent’s internal reasoning is opaque unless you’ve wired up your own logging at every SDK call site. AI Gateway gives you a complete audit trail by default.
The logs are queryable in the Cloudflare dashboard and exportable to your own storage or observability stack via Logpush (which supports R2, S3, Datadog, Splunk, and others). For regulated environments or teams that need to audit AI interactions, this is often the fastest path to compliance-ready logging.
Caching: The Biggest Cost Lever
AI Gateway supports two caching modes:
Exact match caching — if the same prompt appears again, the cached response is returned without hitting the model. Useful for deterministic lookups, static content generation, or FAQ-style queries where the question text is identical across users.
Semantic caching — Cloudflare embeds incoming queries and uses vector similarity to find cached responses to semantically equivalent questions. A question phrased five different ways by five different users might all get served from one cached model response. For customer service agents, internal knowledge bots, and document Q&A systems, semantic cache hit rates of 30–60% are common in production.
You control the cache TTL and can set bypass rules for user-specific or session-sensitive queries. Cache keys can include or exclude specific headers, so you can namespace caching per customer tier if needed.
At scale, semantic caching changes the economics of AI agents significantly. A query that costs $0.01 in tokens returns from cache for a fraction of that.
Rate Limiting
AI Gateway lets you set rate limits on:
- Total requests per time window (global, per gateway)
- Requests per user or customer (using a custom identifier you pass in a header)
- Token consumption (for providers that return token counts)
For multi-tenant agent deployments — where multiple customers share the same backend — per-user rate limiting is the difference between one abusive user affecting everyone and each user being independently throttled. You can configure rate limit responses to return a specific error message or to queue and retry after a delay.
Fallback Routing
AI Gateway supports model fallback chains: if the primary provider returns an error or times out, the gateway automatically retries against a configured backup. A common setup is Claude as primary with GPT-4o as fallback, or a high-capacity model with a faster, cheaper model for latency-sensitive paths.
Fallback routing is configured in the gateway settings, not in your application code. When OpenAI has an outage, your agents don’t need a code deployment to switch providers — the gateway handles it.
When to Add AI Gateway
AI Gateway adds latency (Cloudflare estimates ~10-20ms in the worst case, often less). For latency-sensitive real-time voice agents, that may matter. For async task agents, document processing, or workflows where the LLM call already takes 1–30 seconds, 20ms is noise.
The cases where AI Gateway is worth adding:
Multi-tenant production deployments — rate limiting, per-customer cost tracking, and per-user audit logs are hard to build from scratch and AI Gateway provides them out of the box.
Cost-sensitive high-volume agents — if your agent makes thousands of similar queries per day, semantic caching will pay for the gateway many times over.
Multi-provider agent architectures — if you’re routing to different models based on task type, AI Gateway gives you a unified observability view and fallback handling across all of them.
Compliance environments — if you need a complete log of all AI interactions, AI Gateway’s logging is the fastest path to that capability.
Setup
Getting started is a Cloudflare account and changing the base URL in your SDK client. For the Anthropic SDK:
import anthropic
client = anthropic.Anthropic(
base_url="https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/anthropic",
api_key="your-anthropic-key",
)
Cloudflare provides the equivalent one-liner for OpenAI, Mistral, Cohere, Google, and every other supported provider.
The gateway itself is configured through the Cloudflare dashboard or via Wrangler for infrastructure-as-code teams. Cache settings, rate limits, and fallback chains are all configured there.
Positioning in the Stack
AI Gateway is not a replacement for LiteLLM or Portkey if you need provider abstraction at the SDK level — it operates at the HTTP layer, not the SDK layer. For teams that need to switch providers dynamically based on task logic inside the agent, SDK-level routing is still necessary. But AI Gateway and SDK-level routing are complementary: SDK routing decides which provider to call; AI Gateway handles logging, caching, and rate limiting regardless of which provider was chosen.
For teams running AI agents at production scale who haven’t yet solved observability, cost control, or rate limiting, Cloudflare AI Gateway is the lowest-friction way to add all three.