Skip to content
Artificial Intelligence 8 min read 🎯 Intermediate Cloudflare AI Gateway, Workers AI, Wrangler 3.x, Node.js 22+ Verified

Cloudflare AI Gateway: Stop Flying Blind on LLM Costs

Uncontrolled AI API spending is the new shadow IT. Cloudflare AI Gateway gives you a single control plane for every LLM call your app makes — with caching, analytics, and guardrails built in.


Key takeaways
  1. AI Gateway requires only one URL change to activate — no SDK rewrites or custom middleware.
  2. Response caching can reduce redundant API calls to zero for repeated prompts, cutting both latency and cost.
  3. The Analytics dashboard exposes per-provider token counts, costs, and error rates in real time.
  4. Dynamic routing lets you define fallback models when a primary provider is unavailable.
  5. Guardrails scan both prompts and responses for harmful content without additional third-party services.

Bottom Line Up Front: Most teams build AI features by calling LLM provider APIs directly — and they pay for it twice: once in tokens, and again in debugging time when a provider goes down. Cloudflare AI Gateway is a single-URL proxy that adds caching, analytics, rate limiting, and automatic fallbacks to every LLM call your app makes. You change one line. You gain a control plane.

The “Vibe Coding” Cost Problem

When your team starts shipping AI features fast, a pattern emerges that Cloudflare’s own engineering team calls “the horror stories” — uncontrolled token spend with no visibility into which application, team, or feature is driving it. According to the 2025 Stack Overflow Developer Survey, 82% of developers now use AI coding tools regularly, but fewer than 30% work at organizations that track AI API costs at a feature level.

The result is what looks like shadow IT but denominated in tokens. One team routes every log summary through GPT-4o. Another uses Claude Sonnet for input validation that could run on a $0.0001-per-million-token model. Nobody knows until the invoice arrives.

Cloudflare AI Gateway solves this with a proxy architecture: all your AI traffic routes through a gateway endpoint you control, and Cloudflare captures every request — token counts, latency, error codes, and cost estimates — before it reaches any provider.

What AI Gateway Actually Does

AI Gateway sits between your application and any LLM provider. The architecture is simple:

main
src/ index.plaintext
--:--
Your App → Cloudflare AI Gateway → OpenAI / Anthropic / Workers AI / etc.

The gateway URL format is:

main
src/ index.plaintext
--:--
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/{provider}/v1

Every request that flows through it gets five capabilities automatically:

| Feature | What it does | | ------------------- | ------------------------------------------------------------------------------------------ | | Analytics | Token counts, cost estimates, latency, error rates — per provider, per model | | Caching | Serves identical prompts from Cloudflare’s edge. Up to 90% latency reduction on cache hits | | Rate limiting | Fixed or sliding window limits to prevent abuse and budget overruns | | Dynamic routing | Automatic retry against a fallback model on provider 5xx errors | | Guardrails | Real-time content moderation on both prompts and responses |

As Matthew Prince, Cloudflare’s CEO, put it: “The goal for our Workers developer platform has always been to abstract away the infrastructure complexities so that developers can focus on the thing they care about most — building and delivering amazing products.” AI Gateway applies that same principle to the AI API layer.

Setup: One URL Change

You do not need to refactor your application. The gateway exposes an OpenAI-compatible interface, so the integration is a single configuration change in how you initialize your SDK client.

Step 1: Create a Gateway

main
src/ Cloudflare Dashboard
--:--

Navigate to: dash.cloudflare.com → AI → AI Gateway → Create Gateway # Give

it a name (e.g. “production”), then copy the generated URL

Step 2: Update Your SDK Client

Here’s the before and after for the three most common SDKs:

Vercel AI SDK (ai package):

main
src/ index.typescript
--:--
// Before — direct provider call
import { createOpenAI } from "@ai-sdk/openai";
const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });

// After — routed through AI Gateway
import { createOpenAI } from "@ai-sdk/openai";
const openai = createOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: `https://gateway.ai.cloudflare.com/v1/${process.env.CF_ACCOUNT_ID}/${process.env.CF_GATEWAY_NAME}/openai/v1`,
});

OpenAI Node SDK:

main
src/ index.typescript
--:--
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: `https://gateway.ai.cloudflare.com/v1/${CF_ACCOUNT_ID}/${CF_GATEWAY_NAME}/openai/v1`,
});

Anthropic SDK:

main
src/ index.typescript
--:--
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
  baseURL: `https://gateway.ai.cloudflare.com/v1/${CF_ACCOUNT_ID}/${CF_GATEWAY_NAME}/anthropic`,
});

That’s the entire integration for basic routing and analytics. Every subsequent request to client.chat.completions.create(...) now flows through your gateway.

Step 3: Enable Caching

In the gateway’s Settings panel, toggle Response Caching on and set a TTL. The cache key is derived from the full request body, so identical prompts — same model, same messages array, same parameters — are served from Cloudflare’s edge without touching the provider.

For applications with repeated or templated prompts (report summaries, code explanations, FAQ lookups), this eliminates the majority of API calls. The Cloudflare team reports up to 90% latency reduction on cached responses.

Step 4: Add a Fallback

Under Advanced settings → Fallbacks, define a secondary model to retry against when the primary provider returns a 5xx error. A typical production configuration:

main
src/ index.plaintext
--:--
Primary:  openai/gpt-4o-mini
Fallback: workers-ai/@cf/meta/llama-3.1-8b-instruct

This makes your application resilient to provider outages without any error-handling code on your side.

Workers AI Integration (Bindings)

If you’re running AI inference inside Cloudflare Workers, the gateway integrates through the AI binding in wrangler.toml:

main
src/ index.toml
--:--
# wrangler.toml
[ai]
binding = "AI"

Then in your worker, pass the gateway ID to the run call:

main
src/ index.typescript
--:--
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const response = await env.AI.run("@cf/meta/llama-3.1-8b-instruct", {
      prompt: "Explain Cloudflare AI Gateway in one sentence.",
      gateway: {
        id: "production", // your gateway name
        skipCache: false,
        cacheTtl: 3600,
      },
    });
    return Response.json(response);
  },
};
main
src/ Deploy Worker with AI binding
--:--

wrangler deploy

All inference calls from the worker now appear in the gateway’s Analytics dashboard alongside any external provider calls — giving you a single view across your entire AI stack.

Reading the Analytics Dashboard

The Analytics tab gives you three panels worth watching:

  • Request volume by provider — which provider is handling your traffic and at what rate
  • Token distribution — input vs. output tokens per model (output tokens cost more; high output ratios indicate long responses worth caching)
  • Cache hit rate — if this is below 40% for a conversational app, review whether you’re sending unnecessary variation in system prompts that’s busting the cache key

One non-obvious cost lever: the analytics view often reveals that a small set of prompts (documentation lookups, boilerplate explanations) accounts for a large share of token spend. Moving those to a cached, smaller model (like llama-3.1-8b-instruct via Workers AI) while routing complex reasoning to frontier models is the highest-ROI optimization available.

Guardrails: Content Moderation Without a Third-Party Service

Cloudflare AI Gateway includes built-in prompt and response moderation under Settings → Guardrails. It runs on Cloudflare’s edge before and after the provider call and can:

  • Block prompts that violate content policies (harm, violence, PII)
  • Strip sensitive data from responses (Data Loss Prevention mode)
  • Log flagged requests without blocking them (audit-only mode)

This matters for teams building customer-facing AI features that need to demonstrate compliance without integrating a separate moderation service.

Summary & Next Steps

Cloudflare AI Gateway converts your AI API layer from a black box into a monitored, cached, rate-limited infrastructure component. The integration cost is one URL change. The operational upside — cost visibility, caching, fallbacks, moderation — is immediate.

  • Start here: Cloudflare AI Gateway docs — takes 10 minutes to create a gateway and route your first request
  • Pair with Workers AI: if you’re on Cloudflare Workers, you get free inference on Llama, Mistral, and Flux models with the AI binding — no external provider needed for many use cases
  • Counter-perspective: AI Gateway caching works best for applications with deterministic or templated prompts. For purely conversational applications where every message is unique, cache hit rates will be low and the main value is analytics + fallbacks rather than cost reduction
Henrique Bonfim

Author

Henrique Bonfim

Senior Software Engineer

Senior Software Engineer with extensive experience building production web systems. Creator of ZettaBytes. Contributor to open-source Astro tooling. Specializes in edge-first architectures, AI integration, and web performance.

Related articles