With Cloudflare AI Gateway, how do an authenticated gateway and stored provider keys (BYOK) keep model-provider API keys out of application code?
answer
- two different credentials
- cf-aig-authorization on gateway URLs
- provider keys in Secrets Store
- request key, stored key, then Unified Billing
- Require provider credentials
basics
~20 sAn authenticated gateway accepts only requests carrying a Cloudflare token (cf-aig-authorization on gateway.ai.cloudflare.com), and BYOK stores the provider key in the gateway, so services hold a revocable Cloudflare token instead of the provider key itself.
solid answer
~40 sTwo credentials are involved. **Authenticated Gateway** is a gateway setting: with it on, a request must carry a Cloudflare API token with AI Gateway `Run` permission, sent as `cf-aig-authorization: Bearer <token>` on `gateway.ai.cloudflare.com` URLs (the REST API on `api.cloudflare.com` uses `Authorization` instead). **BYOK** stores the provider's API key in the gateway's **Provider Keys**, backed by Secrets Store, and the gateway adds it upstream. The request must then carry **no** provider `Authorization` header: one that is present is forwarded as-is. Credentials resolve as request key, then the stored key under the `default` alias, then Unified Billing with Cloudflare-managed credentials. **Require provider credentials** (`byok_only`) turns that last fallback into a `400`. Rotating a stored key needs no code change, and the Cloudflare token stays on servers because AI Gateway permissions are account-scoped.
code
bash · 5 linescurl https://gateway.ai.cloudflare.com/v1/$ACCOUNT_ID/support-assistant/openai/chat/completions \
--header "cf-aig-authorization: Bearer $CF_AIG_TOKEN" \
--header "cf-aig-byok-alias: production" \
--header "Content-Type: application/json" \
--data '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Draft a shipping-delay apology."}]}'go deeper
Recall the two credentials: a Cloudflare token for the gateway, sent as cf-aig-authorization on gateway URLs, and the provider key, which BYOK stores in the gateway.
Explain credential precedence, request key, then the default stored alias, then Unified Billing, and why the app must drop its provider Authorization header.
Show the production consequences: account-scoped tokens that reach every gateway, silent Unified Billing fallback, and rotation and deletion of stored keys.
Decide where tokens may live, whether tenants need separate accounts, and whether Require provider credentials is a team default.
## Two credentials, two jobs A call through AI Gateway can involve two different secrets, and confusing them is the root of most setup mistakes: - **The gateway credential** proves the caller may use your gateway. It is a **Cloudflare API token**. - **The provider credential** proves to the model provider that someone pays for the call. It is the provider's **API key**, or Cloudflare's own credentials under Unified Billing. Keeping provider keys out of application code means the app holds only the first kind, and the gateway supplies the second. ## Authenticated Gateway **Authenticated Gateway** is a per-gateway setting. In the gateway's settings you select **Create authentication token**, which issues a token with the `Run` permission, then turn the setting on. From then on: | Setting | Token sent | Result | | --- | --- | --- | | On | Yes | Request succeeds | | On | No | Request fails for missing authorization | | Off | Either | Request succeeds | On provider-native URLs (`gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/...`) the token goes in **`cf-aig-authorization: Bearer <token>`**, because `Authorization` there belongs to the provider. On the REST API (`api.cloudflare.com/.../ai/...`) the Cloudflare token goes in the standard `Authorization` header, and it needs the **Workers AI Read** permission; a token holding only an AI Gateway permission gets `401` with error code `10000`. A Worker calling through the AI binding is pre-authenticated within its account and sends no header. The auto-created `default` gateway starts with authentication on. An unauthenticated gateway accepts requests from anyone who learns the account ID and gateway ID, and such traffic inflates log volume. BYOK and Unified Billing both list an authenticated gateway as a prerequisite, and requests through them spend stored keys or prepaid credits. ## BYOK: stored provider keys **Bring your own keys (BYOK)** stores a provider API key in the gateway, under **Provider Keys** in the dashboard. The key lives in **Secrets Store**; when added from the dashboard the secret is created and named for you, while through the API you create it first under the name `{gateway_id}_{provider_slug}_{alias}`. - Each key has an **alias**; without one it is `default`. On provider-native requests the `cf-aig-byok-alias` header picks another alias, such as `production` or `testing`. - On the REST API and the AI binding only the `default` alias is consulted. - **Rotation** is an edit of the stored entry; applications pick up the new key with no code change. Deleting a key immediately breaks every request that depended on it. The application must **remove** its provider `Authorization` header. If one is present, AI Gateway forwards that value to the provider instead of substituting the stored key, and a placeholder value produces a provider authentication error. ## Which credential wins 1. A **provider key on the request** is forwarded unchanged; BYOK and Unified Billing are not consulted. 2. Otherwise, a **stored key** for that provider under the `default` alias is used. 3. Otherwise, the request runs on **Unified Billing**: Cloudflare-managed credentials, paid from the account's prepaid credits. That third step can surprise a team that meant every call to go through its own provider contract. Turning on **Require provider credentials** (`byok_only: true` on the gateway) makes a third-party request without applicable credentials return `400` instead. For a single request, `cf-aig-no-wholesale: true` does the same; it can tighten but never relax the gateway setting. Workers AI requests are unaffected, since they use no provider credentials. ## Where the Cloudflare token may live - AI Gateway permissions are **account-scoped**: a token with `AI Gateway Run` can send requests through **every** gateway in the account, including ones holding stored keys. A token cannot be narrowed to one gateway. - So the token belongs on servers and in Workers, never in a browser or a mobile app. Browser traffic goes to your own backend, which calls the gateway after its own authentication and per-user checks; keep the token in the backend's secret store and rotate it like any other credential. - For isolation between tenants or teams, use separate Cloudflare accounts or a Worker with the AI binding, rather than relying on token scope. - A gateway behind Cloudflare Access on a custom domain accepts the Access JWT as the credential instead, which ties each call to a signed-in user; the Access setup itself belongs with the Access material.
- A team turned on BYOK but left a placeholder provider Authorization header in its SDK config; what happens?AI Gateway substitutes the stored key only when the provider authorization header is absent. A present header is forwarded to the provider as-is, so the placeholder fails provider authentication. Remove the provider header and send only the Cloudflare token in `cf-aig-authorization`.
- Why might a team turn on Require provider credentials even though Unified Billing works?Without it, a request with no provider key and no `default` stored key silently runs on Cloudflare-managed credentials and draws on prepaid credits. A team bound to its own provider agreements, or tracking spend per provider account, wants that case to fail with `400` so a missing or misnamed key is noticed at once.
- With BYOK on, the gateway overrides any provider Authorization header the request sends.
- An AI Gateway token can be scoped to a single gateway like an R2 bucket token.
- Putting the Cloudflare gateway token in browser code is safe because it is not the provider key.
- Deleting a stored provider key only affects the next deployment of the app.
- A request with no key anywhere simply fails, so nothing can be billed unexpectedly.
Assumes Cloudflare's platform as of 2026-10-10: the REST API with standard Authorization arrived in May 2026, and Require provider credentials, which blocks fallback to Unified Billing, arrived in September 2026.