Skip to content

Other providers' models go through Cloudflare AI Gateway, not Vercel's - #17

Merged
vijayakrishnans merged 1 commit into
mainfrom
claude/cloudflare-ai-gateway
Nov 28, 2026
Merged

vijayakrishnans merged 1 commit into
mainfrom
claude/cloudflare-ai-gateway

Conversation

@vijayakrishnans

Copy link
Copy Markdown
Contributor

The optional "gateway" models were the last thing in this baseline that called Vercel. They now run through Cloudflare AI Gateway on the Worker's own AI binding.

How it works now

  • workers-ai-provider (already a dependency) sends a catalogue id such as openai/gpt-5.4-mini as env.AI.run(id, inputs, { gateway }). The gateway bills the Cloudflare account's AI Gateway credits (Unified Billing), so no provider key reaches the Worker. No new dependency.
  • AI_GATEWAY_ID (the gateway's name; default is the one every account has) replaces AI_GATEWAY_API_KEY. With it unset, only Workers AI's models are offered, as before.
  • The list is three models from Cloudflare's catalogue: GPT-5.4 mini, Grok 4.3, DeepSeek V4 Pro. Their capabilities are declared in lib/ai/models.ts.
  • A request the gateway cannot bill (no credits, or the gateway's authentication is off) opens a dialog linking to the gateway's dashboard page, in place of Vercel's add-a-credit-card dialog.

Removed: the per-model health lookup and the demo catalogue, which only Vercel's gateway served; the gateway.order and OpenAI reasoningEffort provider options that went with upstream's list.

Owner setup (README, "Models"): load AI Gateway credits, turn on the gateway's authentication, then wrangler secret put AI_GATEWAY_ID.

Checked locally: unit tests (53 pass, including a new one that a gateway model reaches the binding with the named gateway), typecheck, lint, the component-docs check, and the testing/ contract gates (orun v2.96.0).

Not checked: a real request through a live gateway. That needs a Cloudflare account with AI Gateway credits. workers-ai-provider labels its third-party routing experimental, and the wording the billing dialog matches on comes from Cloudflare's troubleshooting page rather than an observed response.

🤖 Generated with Claude Code

The optional gateway models called Vercel's AI Gateway with an
AI_GATEWAY_API_KEY. They now run on the Worker's own AI binding through
Cloudflare AI Gateway: workers-ai-provider sends a catalogue id as
env.AI.run(id, inputs, { gateway }), and the gateway bills the account's
AI Gateway credits, so no provider key reaches the Worker.

- AI_GATEWAY_ID (the gateway's name; default is the account's own) replaces
  AI_GATEWAY_API_KEY as the setting that offers these models.
- The list is three models from Cloudflare's catalogue: GPT-5.4 mini,
  Grok 4.3 and DeepSeek V4 Pro, with their capabilities declared.
- The per-model health lookup and the demo catalogue, which only Vercel's
  gateway served, are removed.
- A request the gateway cannot bill opens a dialog linking to the gateway's
  page, in place of Vercel's add-a-credit-card dialog.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T08:18:27.532730Z 36c9b20 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 36c9b2093a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/web/lib/errors.ts
export function isGatewayBillingError(error: unknown): boolean {
return (
error instanceof Error &&
/unified billing|insufficient balance/i.test(error.message ?? "")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match only gateway setup and balance errors

When Cloudflare returns a unified-billing rate-limit error (2018) or temporary-unavailability error (7005), its message also contains “Unified billing,” so this predicate misclassifies it as a billing setup failure. The chat then tells users to add credits and enable authentication instead of reducing request rate or retrying; distinguish those transient errors from setup and balance failures.

Useful? React with 👍 / 👎.

Comment thread apps/web/lib/ai/models.ts
// copies the Worker's secrets and vars into process.env per request, so this
// is read at call time, never at module load.
export function getGatewayId(): string | undefined {
return process.env.AI_GATEWAY_ID?.trim() || undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update the operator guide for the renamed gateway secret

The operator guide at ai/context/operations.md still tells owners to set AI_GATEWAY_API_KEY. Since this change only reads AI_GATEWAY_ID, following the documented setup leaves the Cloudflare gateway models unavailable. Update the guide to use the new setting.

Useful? React with 👍 / 👎.

@vijayakrishnans
vijayakrishnans merged commit 91b18ed into main Nov 28, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant