feat: add Cloudflare Workers AI and AI Gateway as model providers#3389
Merged
Conversation
Add two OpenAI-compatible aliases whose base URLs are account/gateway scoped
and therefore templated with CLOUDFLARE_* env vars, resolved at provider-build
time via the existing ${env.X} expansion path (issue #2261):
- cloudflare-workers-ai: .../accounts/${CLOUDFLARE_ACCOUNT_ID}/ai/v1
- cloudflare-ai-gateway: .../v1/${CLOUDFLARE_ACCOUNT_ID}/${CLOUDFLARE_GATEWAY_ID}/compat
Both authenticate with CLOUDFLARE_API_TOKEN and coalesce consecutive system
messages, like the other open-model hosts. The config preflight now also
requires the env vars referenced by a templated alias base URL, and the runtime
model picker only advertises such an alias once all of them are set (not on the
token alone), so it never surfaces catalog models that cannot be selected.
Cloudflare is deliberately not wired into auto-selection: its conjunctive env
requirement (token plus account, plus gateway) cannot be expressed by the
disjunctive "any key set" detection model, so it stays an explicitly configured
provider.
Verified against the Cloudflare docs. The AI Gateway alias sends the token in
the Authorization header, so it works with an unauthenticated gateway routing to
Workers AI models. An authenticated gateway (unified billing) expects the
cf-aig-authorization header and is not supported out of the box; this is
documented with a custom-provider fallback.
Wires the change into the JSON schema, docs (two provider pages, nav, overview,
concepts, configuration), example configs, and tests: templated URL resolution,
missing-env errors, the shared alias end-to-end harness, the preflight env
gathering, and picker availability.
Closes #3354
Closes #3355
dgageot
approved these changes
Jul 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds two OpenAI-compatible provider aliases for Cloudflare, closing #3355 (Workers AI) and #3354 (AI Gateway). They share the
CLOUDFLARE_*environment variables and are handled together.Both endpoints are account/gateway scoped, so a fully static alias is not possible. The chosen approach is a templated base URL: the alias
BaseURLcarries${CLOUDFLARE_ACCOUNT_ID}(and${CLOUDFLARE_GATEWAY_ID}for the gateway), resolved from the environment at provider-build time via the existing${env.X}expansion path (issue #2261). No new resolution machinery is introduced.cloudflare-workers-aihttps://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/ai/v1CLOUDFLARE_API_TOKEN,CLOUDFLARE_ACCOUNT_IDcloudflare-ai-gatewayhttps://gateway.ai.cloudflare.com/v1/${CLOUDFLARE_ACCOUNT_ID}/${CLOUDFLARE_GATEWAY_ID}/compatCLOUDFLARE_API_TOKEN,CLOUDFLARE_ACCOUNT_ID,CLOUDFLARE_GATEWAY_IDDefinition of Done
pkg/model/provider/aliases.goexamples/cloudflare-workers-ai.yaml,examples/cloudflare-ai-gateway.yamldocs/providers/cloudflare-*, overview, concepts, configuration, navagent-schema.jsonDesign decisions
${env.X}expansion already applied tomodel/base_url, so a bareprovider: cloudflare-workers-airesolves the endpoint from the environment. A missing variable produces a clear error instead of dialing a malformed URL.pkg/config/gather.go) now also requires the env vars referenced by a templated alias base URL. The runtime model picker (pkg/runtime/model_switcher.go) only advertises such an alias once all of them are set, not on the token alone, so it never surfaces catalog models that cannot be selected (the embedded models.dev snapshot does list Cloudflare models).pkg/config/auto.gocannot express. Cloudflare stays an explicitly configured provider. Auto-selection is not part of either issue's Definition of Done.openModelHostProviders: Workers AI serves open-weight models directly, and the gateway can front them.AI Gateway authentication (residual design point)
Verified against the Cloudflare docs. The alias sends
CLOUDFLARE_API_TOKENin the standardAuthorization: Bearerheader, so it works with a gateway that has authentication disabled (the default), routing to Workers AI models. A gateway with authentication enabled (unified billing) instead expects thecf-aig-authorizationheader; the alias does not send it, andprovider_opts.http_headersvalues are not environment-expanded, so that case is not supported out of the box. This is documented on the provider page with a custom-provider fallback. Adding a first-class, env-expanded gateway-auth header can be a follow-up if unified billing is wanted; the current change does not preclude it.Testing
TestCloudflareAliases(pkg/model/provider/aliases_test.go)pkg/model/provider/cloudflare_alias_test.gopkg/model/provider/openai_alias_providers_test.go(hermetic, plus opt-in live viaCLOUDFLARE_*)pkg/config/gather_alias_env_test.goTestGetAvailableProviders_TemplatedAlias(pkg/runtime/model_switcher_test.go)go build ./...,go vet, andgolangci-lint(0 issues) pass; tests for the affected packages (pkg/config,pkg/model/provider,pkg/runtime) pass.Not covered
No live request was executed against the Cloudflare API in this change. The opt-in
TestOpenAIAliasProvider_LiveAPIruns a real request whenCLOUDFLARE_API_TOKEN(and the account/gateway ids) are present, and stays skipped otherwise.