Skip to content
AnswerWell geo Documentation

Research · 9 min read · Answerwell

AI Gateway Provider Endpoints, URL Routing, and Chat Completions

How to point SDKs and HTTP clients at AI Gateway: provider paths, Cloudflare REST API, compat routing, auth headers, and custom upstream URLs.

This page summarises Cloudflare AI Gateway documentation on how to send inference traffic through a gateway. The same idea appears dozens of times across provider pages: you either call a provider-native path under gateway.ai.cloudflare.com, use the account REST API with a provider/model string, or use the gateway /compat/chat/completions surface when you want one OpenAI-shaped URL. Choosing the wrong base URL or duplicating path segments is the most common cause of upstream 404 responses.

AI Gateway endpoint types: which URL to use

Section titled “AI Gateway endpoint types: which URL to use”

Cloudflare exposes more than one entry point, and they are not interchangeable for every workload.

Approach Typical base URL Request shape Best when
Account REST API https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/... OpenAI chat, Anthropic messages, or per-model input on /ai/run New apps, Unified Billing, no provider SDK key in the client
Provider-specific gateway URL https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider} Each vendor’s native API (paths appended after the provider slug) You already use a provider SDK and only want to swap baseURL
Gateway Unified API (/compat) .../{gateway_id}/compat/chat/completions OpenAI chat/completions JSON; model selects the backend Legacy integrations, dynamic routes, some multi-provider OpenAI SDK setups

For direct integration with a named vendor, the getting started guide describes dedicated endpoints that keep the original provider schema:

For direct integration with specific AI providers, use dedicated endpoints that maintain the original provider’s API schema while adding AI Gateway features.

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider}

— Cloudflare, Getting started

The account-level shortcut is documented on the same page:

Call any model — whether hosted on Cloudflare or by a third-party provider — through the same Cloudflare API. No provider SDKs or API keys needed — authentication and billing are handled through your Cloudflare account. Three endpoints are available: /ai/run for all modalities, /ai/v1/chat/completions for OpenAI SDK compatibility, and /ai/v1/responses for agentic workflows.

— Cloudflare, Getting started

Common conclusion: use api.cloudflare.com/.../ai/v1/chat/completions with Authorization: Bearer and a provider/model name when you want one client and Cloudflare-managed auth; use gateway.ai.cloudflare.com/.../{provider} when you must preserve a vendor’s exact paths and headers (Azure api-version, Cohere Token, Fal Key, and so on).

Provider-specific endpoint URL: how path routing works

Section titled “Provider-specific endpoint URL: how path routing works”

Provider-specific routing follows one pattern: swap the vendor’s public host for the gateway prefix, then keep the rest of the path.

Examples from the cluster:

  • OpenAI: replace https://api.openai.com/v1 with https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai, e.g. .../openai/chat/completions.
  • Groq: replace https://api.groq.com/openai/v1 with .../groq, then call .../groq/chat/completions.
  • Hugging Face Inference API: replace https://api-inference.huggingface.co/models/ with .../huggingface/{model} (model id immediately after the slug).
  • Amazon Bedrock: replace https://bedrock-runtime.us-east-1.amazonaws.com/ with .../aws-bedrock/bedrock-runtime/us-east-1/ and append the Bedrock model path.
  • Azure OpenAI: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/azure-openai/{resource_name}/{deployment_name} then append routes such as chat/completions?api-version=2023-05-15.
  • Google Vertex AI: build .../google-vertex-ai/v1/projects/{project_name}/locations/{region}/... and prefer a regional host (for example us-central1) rather than global when the docs warn about limited global support.

Prerequisites repeat across provider pages: AI Gateway account ID, gateway name, the provider credential (API key, token, or cloud signature), and the model or deployment id you intend to call.

Anthropic native path vs OpenAI-shaped account API

Section titled “Anthropic native path vs OpenAI-shaped account API”

On the gateway Anthropic base URL, provider credentials belong in x-api-key, not Authorization: Bearer for the upstream key, and AI Gateway itself is authenticated separately:

When supplying a provider key to /anthropic/v1/messages, send it in x-api-key. AI Gateway does not recognize Authorization: Bearer as provider credentials on this endpoint. Authenticate to AI Gateway separately with cf-aig-authorization.

— Cloudflare, Anthropic

For Anthropic via the account API, use REST API /ai/v1/messages with your Cloudflare token and model values such as anthropic/claude-sonnet-5.

REST API chat/completions: OpenAI SDK base URL

Section titled “REST API chat/completions: OpenAI SDK base URL”

The REST API documents /ai/v1/chat/completions as the standard OpenAI-compatible surface at the account level:

Uses the standard OpenAI chat completions format. The model field uses the same author/model naming. This endpoint is compatible with the OpenAI SDK and other OpenAI-compatible clients.

— Cloudflare, REST API

Point the OpenAI SDK at:

https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/ai/v1

with apiKey set to your Cloudflare API token, as shown in Authenticated Gateway and REST API. Set model to strings like openai/gpt-4.1-mini, groq/llama3-8b-8192, or deepseek/deepseek-chat—provider pages label this the “OpenAI-Compatible Endpoint” and give the same https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions URL.

For modalities that do not map cleanly to chat JSON, /ai/run accepts arbitrary per-model input objects (REST API). For agent workflows, /ai/v1/responses follows the OpenAI Responses API (REST API).

Workers AI models use the @cf/ prefix and may require gateway selection via header:

Workers AI models use the @cf/ prefix in the model name and require the cf-aig-gateway-id header to specify which gateway to route through.

— Cloudflare, Workers AI

Example model: @cf/moonshotai/kimi-k2.6 on .../ai/v1/chat/completions.

Gateway /compat/chat/completions: Unified API limits

Section titled “Gateway /compat/chat/completions: Unified API limits”

The gateway-hosted Unified API lives at:

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions

Cloudflare marks it deprecated for ordinary single-model chat in favour of the account REST API, but it remains important for dynamic routes and existing clients:

For standard single-model chat completions, 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.

— Cloudflare, Unified API (OpenAI compat)

Dynamic routes (dynamic/{route}) can be invoked through this /compat/chat/completions endpoint, and existing integrations continue to work.

— Cloudflare, Unified API (OpenAI compat)

Invoke a dynamic route by setting model to dynamic/<your-dynamic-route-name> (Using a dynamic route). On /compat, many backends are selected with prefixed model ids, for example aws-bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0 (Amazon Bedrock) or parallel/speed (Parallel).

Amazon Bedrock also notes regional behaviour on Unified API: API-key auth tends to target us-east-1, while SigV4 uses the region in stored AWS credentials.

Authentication: cf-aig-authorization vs provider keys

Section titled “Authentication: cf-aig-authorization vs provider keys”

Patterns from the cluster:

Client target Gateway auth Provider / upstream auth
api.cloudflare.com/.../ai/v1/chat/completions Authorization: Bearer $CLOUDFLARE_API_TOKEN Usually none in the client when using Unified Billing (Unified Billing)
gateway.ai.cloudflare.com/.../{provider}/... cf-aig-authorization: Bearer {CF_AIG_TOKEN} when the gateway requires it Vendor header (Authorization, api-key, x-api-key, etc.) per provider page
/compat/chat/completions with BYOK cf-aig-authorization Provider key in Authorization or stored keys (Custom Providers)

Authenticated Gateway shows account REST calls with only the Cloudflare API token. Gateway provider examples often add cf-aig-authorization in defaultHeaders when using the OpenAI or Azure SDK against gateway.ai.cloudflare.com.

Custom domain on AI Gateway: shorter public URL

Section titled “Custom domain on AI Gateway: shorter public URL”

With custom domains, the account and gateway ids drop out of the path:

For example, if your custom domain is ai.example.com, an OpenAI provider request uses: https://ai.example.com/openai/v1/chat/completions Instead of: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/v1/chat/completions

— Cloudflare, Custom domains

The same mapping applies to https://ai.example.com/compat/chat/completions. If the hostname sits behind Cloudflare Access, requests must include a valid Access token (Custom domains).

Custom provider URL mapping: custom-{slug} and base_url

Section titled “Custom provider URL mapping: custom-{slug} and base_url”

Custom Providers explain upstream construction:

Gateway URL: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/custom-{slug}/{provider-path}
Upstream URL: {base_url}/{provider-path}

Everything after custom-{slug}/ in your request URL is appended directly to the base_url to form the final upstream URL.

— Cloudflare, Custom Providers

Keep base_url at the root domain only; put API segments in the gateway path, not in base_url:

The base_url field should contain only the root domain (or domain with a fixed prefix) of the provider’s API. Any API-specific path segments (like /v1/chat/completions) go in the request URL, not in base_url.

— Cloudflare, Custom Providers

Need Use
OpenAI-compatible upstream at /v1/chat/completions Either provider path .../custom-{slug}/v1/chat/completions or Unified API with model: custom-{slug}/{model-name}
Non-standard upstream path Provider-specific endpoint only; Unified API cannot route arbitrary paths

For providers with non-standard paths, you must use the provider-specific endpoint. The Unified API only supports the /chat/completions path and cannot route to custom API paths.

— Cloudflare, Custom Providers

If the upstream returns 404, Custom Providers lists checks: base_url must not already include /v1/chat/completions; the gateway URL must include the full path after custom-{slug}/; avoid doubling segments such as /v1/v1/chat/completions.

OpenAI SDK baseURL: gateway vs account API

Section titled “OpenAI SDK baseURL: gateway vs account API”

Account API (recommended for new chat apps):

import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.CLOUDFLARE_API_TOKEN,
baseURL: `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/ai/v1`,
});
await openai.chat.completions.create({
model: "openai/gpt-4.1-mini",
messages: [{ role: "user", content: "What is Cloudflare?" }],
});

Pattern from REST API and Authenticated Gateway.

Provider-native via gateway (swap vendor base URL only):

import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DEEPSEEK_TOKEN,
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/deepseek",
});
await openai.chat.completions.create({
model: "deepseek-chat",
messages: [{ role: "user", content: "What is Cloudflare?" }],
});

From DeepSeek. The same “replace the vendor host, keep SDK paths” approach appears on Groq, OpenRouter, and xAI Grok.

Custom provider with OpenAI SDK: set base_url to the gateway path through the API version prefix the upstream expects—often .../custom-{slug}/v1—so the SDK’s automatic /chat/completions suffix lands on the correct upstream (Custom Providers).

Workers can resolve URLs with the binding helper (Workers Bindings):

const openaiUrl = await gateway.getUrl("openai"); → https://gateway.ai.cloudflare.com/v1/my-account-id/my-gateway/openai

— Cloudflare, Workers Bindings

Several integrations are not chat-shaped; you append the vendor’s normal path after the gateway slug:

  • Fal AI: replace https://fal.run with .../fal; synchronous calls hit fal.run/<path> by default.
  • Replicate: replace https://api.replicate.com/v1 with .../replicate (see also Pruna tutorial).
  • Parallel: append arbitrary Parallel paths, e.g. .../parallel/v1/tasks/runs.
  • Ideogram, Cartesia, Deepgram, ElevenLabs: same “swap base, keep vendor path” rule on their respective pages.

Baseten illustrates two shapes: OpenAI-compatible .../baseten/v1/chat/completions and model-specific .../baseten/model/{model_id} with a prompt body.

Common mistakes when routing through AI Gateway

Section titled “Common mistakes when routing through AI Gateway”
  • Using /compat/chat/completions for new single-model apps instead of REST API /ai/v1/chat/completions, contrary to the deprecation notice.
  • Sending Anthropic provider keys as Authorization: Bearer on .../anthropic/v1/messages instead of x-api-key (Anthropic).
  • Putting /v1/chat/completions inside custom provider base_url and again in the request path, producing doubled segments (Custom Providers).
  • Forgetting cf-aig-gateway-id when calling @cf/... models via the account API (Workers AI).
  • Using Vertex global endpoints when the model needs a regional location (Google Vertex AI).
  • Relying on the deprecated Universal Endpoint multi-provider payload instead of REST or dynamic routes.
  1. Obtain your account id (wrangler whoami) and an API token (wrangler auth token), then send a minimal POST to REST API chat/completions with model set to a known value such as openai/gpt-4.1-mini and confirm HTTP 200 plus a model reply.

  2. If you use a provider SDK, repeat the call with baseURL pointed at https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider} per that vendor’s page (for example OpenAI or Mistral AI) and verify the upstream still accepts your native headers.

  3. For custom upstreams, list configuration with the custom domains API if applicable, then hit a custom-{slug} path and confirm the upstream URL matches {base_url}/{provider-path} from Custom Providers.

  4. When migrating off legacy URLs, confirm you are not still calling the Universal Endpoint or sole reliance on Unified API /compat unless you need dynamic routes.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match your gateway behaviour, email yimasynthetic@gmail.com with the page URL, the heading or quote you are questioning, and a redacted sample request (host, path, and headers only). That makes it easier to confirm whether the docs changed or the URL mapping is wrong.