# One Account, Twenty Four AI Providers: How a Small Business Actually Uses Cloudflare AI Gateway

AI Gateway is free on every Cloudflare plan and creates itself on your first request. Here are the request shapes that work, the header that causes most 401s, and what Code Mode really saves.

Author: J.A. Watte
Published: July 25, 2026
Source: https://jwatte.com/blog/cloudflare-ai-gateway-small-business/

---

Most small businesses using AI end up in the same mess. There is an OpenAI key in a Zapier step, an Anthropic key in a script somebody wrote in March, a Gemini key in a spreadsheet add-on, and nobody can answer two basic questions: what did we spend last month, and which of those calls did we pay for twice because the same prompt ran again.

Cloudflare AI Gateway fixes that specific mess. It is a proxy you point your existing AI calls at. Your requests still go to OpenAI or Anthropic or Google, you still use your own keys if you want to, but now every call is logged, cacheable, rate limited, and countable in one place. Cloudflare states it plainly: "AI Gateway is available to use on all plans" and "AI Gateway's core features available today are offered for free, and all it takes is a Cloudflare account and one line of code to get started" ([AI Gateway pricing](https://developers.cloudflare.com/ai-gateway/reference/pricing/)).

That is the whole pitch. One line of code, and the thing you could not see becomes a dashboard.

This post is the practical version. What the request actually looks like, which of the four request styles to use, the header mistake that will cost you an afternoon, and then the part that matters for anyone running agents: Code Mode, and what it genuinely saves.

## The gateway creates itself, which is the part people miss

You do not have to go set anything up first. From the docs:

> AI Gateway can automatically create a gateway for you. If you omit the gateway ID from your request entirely, AI Gateway defaults to using `default` as the gateway ID. When no gateway named `default` exists in your account, AI Gateway creates it on the first authenticated request.

That is from [Manage gateways](https://developers.cloudflare.com/ai-gateway/configuration/manage-gateway/). Two conditions attach to it, and both bite people.

First, auto creation only works for the literal name `default`. The docs are explicit: "Auto-creation only applies to the gateway ID `default`. Using any other gateway ID requires creating the gateway first." So a typo in your gateway name does not quietly create a new gateway. It fails.

Second, the request has to be authenticated, and what counts as authenticated depends on which endpoint you called:

> The request that triggers auto-creation must be authenticated. When using the REST API, the standard `Authorization` header is sufficient. When using provider-native endpoints at `gateway.ai.cloudflare.com`, include a valid `cf-aig-authorization` header. For Workers AI bindings, the account identity from the binding is used instead of a header.

There is a third thing nobody tells you, and it is the one that costs money. The gateway that gets created for you does not have the cost controls turned on. The documented defaults are authentication on, log collection on, caching **off** with a TTL of zero, and rate limiting **off** ([Manage gateways](https://developers.cloudflare.com/ai-gateway/configuration/manage-gateway/)). So your first request buys you visibility and nothing else. Caching is a switch you have to go flip, or a header you have to send. More on that below.

## Which request should you actually make

There are four ways to call through the gateway, and two of them are deprecated. This is the single most confusing thing about AI Gateway right now, so here it is straight.

**Use the REST API on `api.cloudflare.com` for anything new.** The old unified endpoint carries this banner:

> This endpoint is deprecated. Use the REST API instead, which provides OpenAI-compatible endpoints at `api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/v1/chat/completions`. The `/compat/chat/completions` endpoint will continue to work for existing integrations.

That is from [Unified API (OpenAI compat)](https://developers.cloudflare.com/ai-gateway/usage/chat-completion/). The older Universal Endpoint, the one that took an array of provider objects, is [deprecated too](https://developers.cloudflare.com/ai-gateway/usage/universal/).

Be aware the docs currently contradict themselves here. The Universal Endpoint page says to move to the OpenAI compatible endpoint, and the OpenAI compatible endpoint page says to move to the REST API. The Universal page was last updated May 8, 2026 and the compat page June 12, 2026, so the REST API is the newer instruction. Both deprecated paths still work, so nothing breaks today, but do not start there.

Here is the honest decision:

| What you want | Use this | Why |
|---|---|---|
| A new integration, several providers, Cloudflare handles keys and billing | REST API at `api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/v1` | Documented current recommendation. Works with the OpenAI and Anthropic SDKs unchanged. |
| Your own provider keys, or a provider feature that is not chat completions | Provider-native at `gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider}` | "For direct integration with specific AI providers, use dedicated endpoints that maintain the original provider's API schema while adding AI Gateway features." ([Get started](https://developers.cloudflare.com/ai-gateway/get-started/)) |
| You are already inside a Cloudflare Worker | The `env.AI` binding with a `gateway` option | No URLs to hardcode. Account identity handles auth. |
| You have an existing integration on `/compat` or the Universal Endpoint | Leave it | Both are explicitly documented to keep working. Migrate when you touch that code anyway. |

Two hard limits decide it for you in practice. The unified endpoint "only supports the `/chat/completions` path and cannot route to custom API paths" ([Custom providers](https://developers.cloudflare.com/ai-gateway/configuration/custom-providers/)), so anything else means provider-native. And bring your own keys "is not supported for third-party models called through the AI binding" ([Worker binding methods](https://developers.cloudflare.com/ai-gateway/usage/worker-binding-methods/)), so if you must use your own OpenAI key from inside a Worker, you use the provider-native URL, not the binding.

## The REST API, four endpoints

The REST API publishes its own chooser. Quoting the [REST API page](https://developers.cloudflare.com/ai-gateway/usage/rest-api/):

| Endpoint | Body shape | Good for |
|---|---|---|
| `POST /ai/run` | Envelope with `model` and `input` | All models and modalities, including image, text to speech, and speech recognition |
| `POST /ai/v1/chat/completions` | OpenAI chat completions | Text models, OpenAI SDK compatible |
| `POST /ai/v1/responses` | OpenAI Responses API | Agentic workflows, OpenAI SDK compatible |
| `POST /ai/v1/messages` | Anthropic Messages API | Text models, Anthropic SDK compatible |

One trap in that table. The Anthropic schema endpoint cannot serve Cloudflare's own models: "The `/ai/v1/messages` endpoint strictly uses Anthropic's API schema and supports routing to Anthropic and other third-party models. Workers AI models (`@cf/`) do not support this schema."

Third-party models use an `author/model` string. The documented examples are `openai/gpt-4.1`, `anthropic/claude-sonnet-4`, `google/gemini-3-flash`, and `xai/grok-3`.

A plain curl call, straight from the docs:

```bash
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "model": "openai/gpt-4.1",
    "messages": [{"role": "user", "content": "What is Cloudflare?"}]
  }'
```

The same thing with the OpenAI SDK, which is the "one line of code" the pricing page means. You change `baseURL` and nothing else:

```javascript
import OpenAI from "openai";

const openai = new OpenAI({
	apiKey: CLOUDFLARE_API_TOKEN,
	baseURL: `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/ai/v1`,
});

const response = await openai.chat.completions.create({
	model: "openai/gpt-4.1",
	messages: [{ role: "user", content: "What is Cloudflare?" }],
});
```

And with the Anthropic SDK. Note the model still needs the `anthropic/` prefix here, which catches people who copy their old code across:

```javascript
import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
	apiKey: CLOUDFLARE_API_TOKEN,
	baseURL: `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/ai/v1`,
});

const message = await anthropic.messages.create({
	model: "anthropic/claude-sonnet-4-5",
	max_tokens: 512,
	messages: [{ role: "user", content: "What is Cloudflare?" }],
});
```

## Calling Workers AI, where the gateway ID is not optional

This is the case the dashboard onboarding copy points at, and it has an extra requirement that trips everyone. From the [REST API page](https://developers.cloudflare.com/ai-gateway/usage/rest-api/):

> By default, third-party model requests route through your account's default AI Gateway. To use a specific gateway, include the `cf-aig-gateway-id` header. Workers AI requests always require this header.

So the `default` gateway gets used automatically for OpenAI or Anthropic, but for a `@cf/` model you must name it explicitly, even if the name you are naming is `default`:

```bash
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "cf-aig-gateway-id: default" \
  --header "Content-Type: application/json" \
  --data '{
    "model": "@cf/moonshotai/kimi-k2.6",
    "messages": [{"role": "user", "content": "What is Cloudflare?"}]
  }'
```

That example is verbatim from [the Workers AI provider page](https://developers.cloudflare.com/ai-gateway/usage/providers/workersai/), including the model. Leave off `cf-aig-gateway-id` and you get an error rather than a helpful default.

## Provider-native, when you want your own keys

The pattern is `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider}`. You keep the provider's exact request and response shape, which means provider-specific parameters keep working. Your provider key goes where that provider normally expects it, and the Cloudflare token goes in `cf-aig-authorization`.

OpenAI:

```javascript
import OpenAI from "openai";

const client = new OpenAI({
	apiKey: "YOUR_OPENAI_API_KEY",
	defaultHeaders: {
		"cf-aig-authorization": `Bearer {cf_api_token}`,
	},
	baseURL:
		"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai",
});
```

Anthropic over curl, where the key lives in `x-api-key` exactly as it does when you call Anthropic directly:

```bash
curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic/v1/messages \
 --header 'x-api-key: {anthropic_api_key}' \
 --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
 --header 'anthropic-version: 2023-06-01' \
 --header 'Content-Type: application/json' \
 --data '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "What is Cloudflare?"}]
  }'
```

Google AI Studio, which keeps its odd colon-joined path and its own key header:

```bash
curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/google-ai-studio/v1/models/gemini-2.5-flash:generateContent" \
 --header 'content-type: application/json' \
 --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
 --header 'x-goog-api-key: {google_studio_api_key}' \
 --data '{"contents": [{"role":"user","parts":[{"text":"What is Cloudflare?"}]}]}'
```

Notice the pattern across all three. The provider's own authentication convention survives untouched. That is the point of the provider-native path.

## The header that causes most 401s

Read this twice, because the convention inverts between the two endpoint styles and the error you get is a 401 that tells you nothing useful.

On `gateway.ai.cloudflare.com`, the `Authorization` header belongs to the **provider**. Your Cloudflare token goes in `cf-aig-authorization`. The troubleshooting page says it outright: "Make sure your Cloudflare token is in `cf-aig-authorization`, not `Authorization`. The `Authorization` header is reserved for provider credentials" ([Troubleshooting](https://developers.cloudflare.com/ai-gateway/reference/troubleshooting/)).

On `api.cloudflare.com`, it flips. "Authenticate with a Cloudflare API token that has `AI Gateway` permission. Pass it in the `Authorization` header" ([REST API](https://developers.cloudflare.com/ai-gateway/usage/rest-api/)).

Same product, opposite rule, depending on the host you called. If you are getting 401s and your keys are definitely right, this is why.

While you are in the security settings, know one limitation. AI Gateway tokens cannot be scoped to a single gateway: "The `AI Gateway Read`, `Run`, and `Edit` permissions cannot be restricted to a single gateway, unlike R2, which supports per-bucket scoping. Any token with `AI Gateway Run` can send requests through every gateway in the account, including any configured with stored provider keys through Bring Your Own Keys (BYOK), consuming those credentials" ([Authentication](https://developers.cloudflare.com/ai-gateway/configuration/authentication/)). Treat a Run token as an account-wide spending key, because that is what it is.

## The headers that actually save you money

Caching is the reason most small businesses see a real bill drop, and it is off by default on an auto created gateway. Three request headers control it, per the [caching docs](https://developers.cloudflare.com/ai-gateway/features/caching/):

- `cf-aig-cache-ttl`: seconds to cache. "The minimum TTL is 60 seconds and the maximum TTL is one month."
- `cf-aig-skip-cache`: bypass the cache for this one request.
- `cf-aig-cache-key`: override the default key.

The response comes back with `cf-aig-cache-status` set to `HIT` or `MISS`, so you can prove it is working rather than hoping.

Four things about that cache will surprise you, and all four are documented:

1. It is exact match only. "Currently caching is supported only for text and image responses, and it applies only to identical requests." One different token in the prompt is a different entry.
2. The default key hashes the provider, endpoint, model, **the provider authentication header**, and the **full request body**. Rotate an API key and you have thrown away your entire cache.
3. Streaming responses are not cached by default ([Troubleshooting](https://developers.cloudflare.com/ai-gateway/reference/troubleshooting/)). If everything you do is streamed, caching buys you nothing.
4. The cache is volatile, with no protection against two identical requests arriving at once: "If two identical requests are sent simultaneously, the first request may not cache in time for the second request to use it."

So caching pays off for repetitive, non-streamed, identical calls. Classification, tagging, canned FAQ answers, product description generation over a catalogue where the same item gets processed twice. It does very little for a chat interface where every prompt is unique.

The other header worth wiring in on day one is `cf-aig-metadata`, which tags a request so you can slice spending later:

```bash
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --header 'cf-aig-metadata: {"team": "AI", "user": 12345, "test":true}' \
  --data '{"model": "openai/gpt-4.1", "messages": [{"role": "user", "content": "What should I eat for lunch?"}]}'
```

The cap is five entries and the failure is silent: "AI Gateway allows you to pass up to five custom metadata entries per request. If more than five entries are provided, only the first five will be saved; additional entries will be ignored." Values may be strings, numbers, or booleans. Objects are not supported ([Custom metadata](https://developers.cloudflare.com/ai-gateway/observability/custom-metadata/)).

Five entries is enough for customer, feature, and environment. Spend them deliberately.

Retries and timeouts round it out, via `cf-aig-max-attempts`, `cf-aig-retry-delay`, and `cf-aig-backoff`. Maximum five attempts, maximum five seconds of delay, backoff constant, linear, or exponential. Two behaviours are worth knowing before you rely on them: the timeout measures time to the first byte, so "as long as the first part of the response returns within the specified timeframe, such as when streaming a response, your gateway will wait for the response", and "on the final retry attempt, your gateway will wait until the request completes, regardless of how long it takes" ([Request handling](https://developers.cloudflare.com/ai-gateway/configuration/request-handling/)).

## Inside a Worker

If your code already runs on Cloudflare, skip the URLs. Add the binding to your Wrangler config:

```jsonc
{
	"ai": {
		"binding": "AI",
	},
}
```

Then the gateway is the third argument:

```javascript
export default {
	async fetch(request, env) {
		const response = await env.AI.run(
			"@cf/meta/llama-3.1-8b-instruct",
			{ prompt: "Why should you use Cloudflare for your AI inference?" },
			{
				gateway: {
					id: "{gateway_id}",
					skipCache: false,
					cacheTtl: 3360,
				},
			},
		);
		return new Response(JSON.stringify(response));
	},
};
```

Third-party models work through the same binding, using Cloudflare managed credentials rather than your own key:

```javascript
const resp = await env.AI.run(
	"openai/gpt-4.1-mini",
	{ messages: [{ role: "user", content: "tell me a joke" }] },
	{ gateway: { id: "default" } },
);
```

And if you want provider-native behaviour without hardcoding your account ID in source, the binding will build the URL for you:

```typescript
import OpenAI from "openai";

const openai = new OpenAI({
	apiKey: "my api key",
	baseURL: await env.AI.gateway("my-gateway").getUrl("openai"),
});
```

All four of those are verbatim from Cloudflare's [Worker binding methods](https://developers.cloudflare.com/ai-gateway/usage/worker-binding-methods/) and [Workers AI provider](https://developers.cloudflare.com/ai-gateway/usage/providers/workersai/) pages.

## What it costs, and where the numbers are soft

The gateway itself is free on every plan. What changes with a paid plan is how much history you keep. Workers Free gives you "100,000 logs total across all gateways". Workers Paid gives you "10,000,000 logs per gateway" ([pricing](https://developers.cloudflare.com/ai-gateway/reference/pricing/)). Gateway count also differs: 10 per account on free, 20 on paid ([limits](https://developers.cloudflare.com/ai-gateway/reference/limits/)). Logpush, for shipping logs somewhere else, is Workers Paid only.

Two costs hide in features that look free:

**Guardrails.** Turning on prompt and response screening is not free inference. "Guardrails evaluates prompts and responses using `@cf/meta/llama-guard-3-8b` on Workers AI. Usage is billed as Workers AI token-based inference, cost scales with the length of the prompts and responses being evaluated." It also cannot stream: "Streaming is not supported when using Guardrails" ([usage considerations](https://developers.cloudflare.com/ai-gateway/features/guardrails/usage-considerations/)).

**Unified Billing.** If you let Cloudflare hold the provider keys and bill you, there is a surcharge and a throughput ceiling. "A 5% fee is applied to all credits purchased through Unified Billing. For example, a $100 credit purchase will result in a $105 charge. Inference pricing from providers is passed through with no markup" ([Unified billing](https://developers.cloudflare.com/ai-gateway/features/unified-billing/)). The ceiling is 200 requests per 60 seconds per gateway, returning a 429 past that, and "this limit does not apply to requests that use your own provider keys through bring your own keys (BYOK)" ([limits](https://developers.cloudflare.com/ai-gateway/reference/limits/)).

That 5% buys you one real thing: a single invoice instead of five. For a small shop juggling four provider accounts, that can be worth more than the fee. If you are throughput sensitive, bring your own keys instead.

Now the important caveat, because this is where people get burned. **The cost number in the dashboard is an estimate, not your bill.** Cloudflare says so: "The cost metric is an estimation based on the number of tokens sent and received in requests. While this metric can help you monitor and predict cost trends, refer to your provider's dashboard for the most accurate cost details." It also only works "for endpoints where the models return token data and the model name in their responses", and the underlying price table can go stale when a provider changes pricing ([Costs](https://developers.cloudflare.com/ai-gateway/observability/costs/)).

Use the gateway number for trend and for attribution. Reconcile against the provider invoice monthly. Do not put the gateway estimate in a client report.

## The best provider combination, by job

Cloudflare currently lists 24 providers on [the providers index](https://developers.cloudflare.com/ai-gateway/usage/providers/): Workers AI, Amazon Bedrock, Anthropic, Azure OpenAI, Baseten, Cartesia, Cerebras, Cohere, Deepgram, DeepSeek, ElevenLabs, Fal AI, Google AI Studio, Google Vertex AI, Groq, HuggingFace, Ideogram, Mistral AI, OpenAI, OpenRouter, Parallel, Perplexity, Replicate, and xAI.

That list is the real constraint on any "best stack" answer, so here is how I would combine them for a small business. I am naming providers rather than quoting per token prices, because model prices change monthly and a stale number in a blog post is worse than no number. Every provider below links to its own current pricing.

| Job | Primary | Cheaper fallback | Why the primary |
|---|---|---|---|
| Writing, support replies, analysis | [Anthropic](https://www.anthropic.com/pricing) | [Workers AI](https://developers.cloudflare.com/workers-ai/platform/pricing/) | Best instruction following on long, messy business documents |
| Coding and agents | [Anthropic](https://www.anthropic.com/pricing) or [OpenAI](https://openai.com/api/pricing/) | [DeepSeek](https://api-docs.deepseek.com/quick_start/pricing) | Both ship first-party terminal agents that close their own loop |
| High volume classification and tagging | [Groq](https://groq.com/pricing) or [Cerebras](https://www.cerebras.ai/pricing) | [Workers AI](https://developers.cloudflare.com/workers-ai/platform/pricing/) | Speed per dollar on short, repetitive calls, and these cache well |
| Images | [Ideogram](https://developers.cloudflare.com/ai-gateway/usage/providers/ideogram/) | [Workers AI](https://developers.cloudflare.com/workers-ai/models/) | Ideogram is a first-class gateway provider, so image spend lands in the same dashboard |
| Video | [Replicate](https://developers.cloudflare.com/ai-gateway/usage/providers/replicate/) or [Fal AI](https://developers.cloudflare.com/ai-gateway/usage/providers/fal/) | none worth naming | Video models are mostly reached through these two aggregators, not direct |
| Voice out | [ElevenLabs](https://developers.cloudflare.com/ai-gateway/usage/providers/elevenlabs/) or [Cartesia](https://developers.cloudflare.com/ai-gateway/usage/providers/cartesia/) | [Workers AI](https://developers.cloudflare.com/workers-ai/models/) | Both are listed providers, so voice usage is visible alongside text |
| Voice in | [Deepgram](https://developers.cloudflare.com/ai-gateway/usage/providers/deepgram/) | [Workers AI](https://developers.cloudflare.com/workers-ai/models/) | Transcription is the single highest volume audio job in most shops |
| One key, many models | [OpenRouter](https://developers.cloudflare.com/ai-gateway/usage/providers/openrouter/) | n/a | A gateway in front of a router is redundant for routing but still useful for caching and logs |

Two honest notes on that table.

The gateway helps most where you make many similar requests. Caching, per customer attribution, and rate limits all pay off on repetitive work. For video generation, where every job is unique, expensive, and slow, the gateway gives you logging and a single bill and essentially nothing else. That is still worth having. Just do not expect the cache to save you anything.

And if a provider you want is not on that list of 24, you are not stuck. Cloudflare supports [custom providers](https://developers.cloudflare.com/ai-gateway/configuration/custom-providers/) where you define the base URL yourself and call it at `.../custom-{slug}/...`.

## Code Mode, and what it really saves

Here is the part worth reading if you are running agents rather than just making API calls.

The Model Context Protocol lets an agent use external tools. The problem is how the tools get introduced. Every connected tool's definition is loaded into the model's context before your question is even read, and every single tool call is a round trip back to the model. Connect a handful of servers with thirty tools apiece and the model is reading hundreds of schemas on every turn whether it uses them or not.

Code Mode changes the shape of that. Cloudflare's original description, from Kenton Varda and Sunil Pai on September 26, 2025, is one sentence: "Convert the MCP tools into a TypeScript API, and then ask an LLM to write code that calls that API" ([Code Mode](https://blog.cloudflare.com/code-mode/)). The tools become a typed API with doc comments, the model writes a short script against it, and that script runs in a sandboxed Worker that can call back to the MCP servers but cannot reach the open internet. Chaining five calls becomes one script, not five round trips.

The numbers are real and they come from three independent places.

**Cloudflare's own.** On February 20, 2026, Matt Carey published results for putting the entire Cloudflare API behind Code Mode. The API has over 2,500 endpoints. A native MCP server exposing all of them would consume about 1.17 million tokens of context. The Code Mode version uses roughly 1,000 tokens, exposing just two tools, `search()` and `execute()`. That is a 99.9% reduction ([Code Mode: give agents an entire API in 1,000 tokens](https://blog.cloudflare.com/code-mode-mcp/)).

**Anthropic's.** On November 4, 2025, Anthropic published the same finding independently, with a worked example of pulling a transcript from Google Drive into a Salesforce record. Doing it through direct tool calls passes the whole document through the context window twice. Doing it in an execution environment "reduces the token usage from 150,000 tokens to 2,000 tokens, a time and cost saving of 98.7%" ([Code execution with MCP](https://www.anthropic.com/engineering/code-execution-with-mcp)).

**An independent benchmark.** Bifrost, built by Maxim AI, ran three rounds scaling tool counts and published the raw numbers on July 3, 2026. At 508 tools across 16 servers, input tokens fell from 75.1M to 5.4M, a 92.8% drop, and estimated cost fell from $377.00 to $29.00, a 92.2% drop. Pass rate stayed at 100% in every round, which is the number that matters most, because a saving bought with a drop in accuracy is not a saving ([Cutting MCP token costs](https://www.getmaxim.ai/articles/cutting-mcp-token-costs-by-92-at-500-tools/)).

That third result is worth dwelling on because it is the closest published match to the roughly 93% figure Cloudflare quotes for its own internal usage in its AI security campaign email. I could not find that specific 93% number on any public Cloudflare page, so treat it as a company statement about its own workload rather than a benchmark you can audit. The 92.8% from Bifrost, with published methodology and a 100% pass rate, is the number I would actually cite.

Notice the spread: 92.8%, 98.7%, 99.9%. The savings scale with how many tools you have connected. A shop with one MCP server and six tools will see very little. A shop running an agent against a dozen connected services will see a lot. That is the whole rule.

Anthropic also names the cost of the approach, and it is fair: "code execution introduces its own complexity. Running agent-generated code requires a secure execution environment with appropriate sandboxing, resource limits, and monitoring. These infrastructure requirements add operational overhead and security considerations that direct tool calls avoid."

For a small business, the practical version is that you do not build the sandbox. You point your agent at Cloudflare's hosted server, which is one config block:

```json
{
  "mcpServers": {
    "cloudflare-api": {
      "url": "https://mcp.cloudflare.com/mcp"
    }
  }
}
```

That server is live and uses OAuth 2.1, with an API token as bearer auth for CI use ([Code Mode MCP](https://blog.cloudflare.com/code-mode-mcp/)). One caveat carried over from the original announcement: the Dynamic Worker Loader API that powers the sandbox was described as being in closed beta for production use, with pricing not yet determined. So using Cloudflare's hosted Code Mode server is available today. Building your own on the same primitive is the part that was still gated.

## What Cloudflare actually changed

Three things, and only one of them is about AI.

It removed the tax on leaving. R2 charges nothing to get your data back out: "Egressing directly from R2, including via the Workers API, S3 API, and r2.dev domains does not incur data transfer (egress) charges and is free", with a free tier of 10 GB-month of storage, 1 million Class A operations, and 10 million Class B operations per month ([R2 pricing](https://developers.cloudflare.com/r2/pricing/)). Egress fees were the quiet thing that made cloud storage sticky. Pricing them at zero forced a conversation the whole industry had been avoiding.

It put the observability layer for AI spending in the free tier. Not a trial, not a seat-based tool, not something you get after a sales call. A Cloudflare account and a changed base URL, and a small business can see its AI spending broken out by customer. Companies sell that as a product.

And with Code Mode it published a structural fix for a protocol problem, then open sourced the approach rather than keeping it as a differentiator. Anthropic reached the same conclusion independently six weeks later. When two companies with opposing commercial interests land on the same answer, that is usually the answer.

The honest counterweight: this is one company, and routing more of your stack through it concentrates your risk. A gateway that sits in front of every AI provider you use is a single point of failure by design. That is a real trade, and worth making deliberately rather than by drift. If a single vendor going dark would stop your business, read [the vendor lockout continuity plan](/blog/ai-vendor-lockout-continuity-plan/) before you consolidate further.

## Where to start on Monday

1. Make a Cloudflare account if you do not have one. Get your account ID and an API token with AI Gateway permission.
2. Change the `baseURL` on your loudest AI integration to `https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/v1`. The gateway creates itself.
3. Go turn caching on, because your auto created gateway has it off. Start with a TTL of an hour on anything repetitive.
4. Add `cf-aig-metadata` with a customer or feature tag before you have a lot of history, because you cannot backfill it.
5. Let it run a month, then compare the gateway estimate against your real provider invoice. That gap is the number you now know and did not before.
6. If you run agents against more than a few MCP servers, try Code Mode. If you run one server with six tools, skip it.

None of that requires a developer on staff, a contract, or a sales call. That is the wider argument my book *The $20 Dollar Agency* makes at length, if you want it: most of what small businesses get quoted four figures for is now a config change and an afternoon of attention.

## Fact-check notes and sources

- AI Gateway is free on all plans, log storage limits, Logpush plan gating, Guardrails billing: [AI Gateway pricing](https://developers.cloudflare.com/ai-gateway/reference/pricing/).
- Gateway count limits and the Unified Billing rate limit of 200 requests per 60 seconds: [AI Gateway limits](https://developers.cloudflare.com/ai-gateway/reference/limits/).
- Default gateway auto creation, the `default`-only restriction, what counts as an authenticated request, and the default settings table: [Manage gateways](https://developers.cloudflare.com/ai-gateway/configuration/manage-gateway/).
- REST API endpoints, the four-endpoint chooser, `author/model` naming, the `cf-aig-gateway-id` requirement for Workers AI, and the `/ai/v1/messages` restriction: [REST API](https://developers.cloudflare.com/ai-gateway/usage/rest-api/).
- Deprecation of the unified `/compat` endpoint: [Unified API (OpenAI compat)](https://developers.cloudflare.com/ai-gateway/usage/chat-completion/). Deprecation of the Universal Endpoint: [Universal endpoint](https://developers.cloudflare.com/ai-gateway/usage/universal/).
- Provider-native base URL pattern and the schema-preservation rationale: [Get started](https://developers.cloudflare.com/ai-gateway/get-started/). Per-provider examples: [OpenAI](https://developers.cloudflare.com/ai-gateway/usage/providers/openai/), [Anthropic](https://developers.cloudflare.com/ai-gateway/usage/providers/anthropic/), [Google AI Studio](https://developers.cloudflare.com/ai-gateway/usage/providers/google-ai-studio/), [Workers AI](https://developers.cloudflare.com/ai-gateway/usage/providers/workersai/).
- The `cf-aig-authorization` versus `Authorization` rule: [Troubleshooting](https://developers.cloudflare.com/ai-gateway/reference/troubleshooting/). Token scoping limitation: [Authentication](https://developers.cloudflare.com/ai-gateway/configuration/authentication/).
- Caching headers, TTL bounds, exact-match behaviour, cache key composition, and cache volatility: [Caching](https://developers.cloudflare.com/ai-gateway/features/caching/). Streaming responses not cached by default: [Troubleshooting](https://developers.cloudflare.com/ai-gateway/reference/troubleshooting/).
- Custom metadata five-entry cap and supported types: [Custom metadata](https://developers.cloudflare.com/ai-gateway/observability/custom-metadata/).
- Retry and timeout semantics including first-byte timing and the untimed final attempt: [Request handling](https://developers.cloudflare.com/ai-gateway/configuration/request-handling/).
- Cost figures are estimates, coverage conditions, and staleness warning: [Costs](https://developers.cloudflare.com/ai-gateway/observability/costs/).
- Unified Billing 5% credit fee and pass-through pricing: [Unified billing](https://developers.cloudflare.com/ai-gateway/features/unified-billing/).
- BYOK not supported for third-party models through the AI binding, and binding code examples: [Worker binding methods](https://developers.cloudflare.com/ai-gateway/usage/worker-binding-methods/).
- The 24 supported providers: [Providers index](https://developers.cloudflare.com/ai-gateway/usage/providers/). Custom provider support: [Custom providers](https://developers.cloudflare.com/ai-gateway/configuration/custom-providers/).
- Code Mode concept, TypeScript API conversion, and sandboxed Worker execution, published September 26, 2025 by Kenton Varda and Sunil Pai: [Code Mode](https://blog.cloudflare.com/code-mode/).
- The 1.17 million to roughly 1,000 token figure across 2,500-plus endpoints, the `search()` and `execute()` design, and the `mcp.cloudflare.com/mcp` endpoint, published February 20, 2026 by Matt Carey: [Code Mode: give agents an entire API in 1,000 tokens](https://blog.cloudflare.com/code-mode-mcp/).
- The 150,000 to 2,000 token example and the 98.7% figure, plus the sandboxing caveat, published November 4, 2025: [Anthropic, Code execution with MCP](https://www.anthropic.com/engineering/code-execution-with-mcp).
- The 508-tool benchmark, 75.1M to 5.4M input tokens (92.8%), $377.00 to $29.00 (92.2%), 100% pass rate, published July 3, 2026 by Bifrost / Maxim AI: [Cutting MCP token costs by 92% at 500+ tools](https://www.getmaxim.ai/articles/cutting-mcp-token-costs-by-92-at-500-tools/).
- The roughly 93% internal figure appears in Cloudflare's [AI security](https://www.cloudflare.com/solutions/ai-security/) marketing campaign email and not, as far as I could find, on any public Cloudflare page. It is reported here as a company claim about its own workload.
- R2 zero egress and free tier allowances: [R2 pricing](https://developers.cloudflare.com/r2/pricing/).
- All Cloudflare documentation pages were checked on July 25, 2026. Documentation for this product is moving quickly, including the two deprecations noted above, so verify endpoint paths against the live docs before you ship.

## Related reading

- [The Cloudflare Developer Platform, Explained Like You're Going to Ship on It](/blog/cloudflare-developer-platform/): the wider tour of Workers, R2, D1, and where AI Gateway sits among them.
- [How a Small Business Runs AI Agents Without a $47,000 Surprise Bill](/blog/blog-ai-agent-cost-controls-smb/): the four safeguards that matter before you let an agent loose, which is the other half of this post.
- [The Best MCP Servers By Industry](/blog/blog-mcp-servers-by-industry-guide/): what to actually connect once Code Mode makes connecting a lot of them cheap.
- [The $50-a-Month AI Stack](/blog/blog-50-month-ai-stack-smb/): the budget this post assumes you are working inside.
- [Picking the Right AI Model in 2026](/blog/blog-ai-model-routing-2026/): the model-by-model comparison behind the provider table above.
- Run the numbers yourself: the [LLM Retrieval Cost Estimator](/tools/llm-retrieval-cost-estimator/) and the [MCP Server Audit](/tools/mcp-server-audit/), both free and browser-side, no signup.

*This post is informational, not consulting or financial advice. Prices, plan limits, and endpoint paths were verified against vendor documentation on July 25, 2026 and change frequently. Mentions of Cloudflare, Anthropic, Maxim AI, and other third parties are nominative fair use; no affiliation or endorsement is implied.*


---

Canonical HTML: https://jwatte.com/blog/cloudflare-ai-gateway-small-business/
RSS: https://jwatte.com/feed.xml
JSON Feed: https://jwatte.com/feed.json
Hero image: https://jwatte.com/images/cloudflare-ai-gateway-small-business.webp
