Skip to content

Route chat requests to the cheapest tier that fits, with one gateway for frontier models - #656

Draft
frankxai wants to merge 2 commits into
agent/antigravity/edge-agent-gatewayfrom
ccr-c6d34b54-d916c7
Draft

frankxai wants to merge 2 commits into
agent/antigravity/edge-agent-gatewayfrom
ccr-c6d34b54-d916c7

Conversation

@frankxai

@frankxai frankxai commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Scope

Stacked on #631 (base is its branch, head 6737029). Adds cost routing and a single upstream for frontier models to services/starlight-cloud-worker. Four files, all inside that service.

  • starlight-route: a new model name for POST /v1/chat/completions. The worker names the kind of work (the classes in starlight-evals routing-table.json, plus general), then tries routes from the cheapest tier up and stops at the first that answers. Callers can set starlight_task, starlight_stakes and starlight_min_tier, in the body or as headers.
  • GLM 5.3 and DeepSeek 4.1 Flash: routes edge-glm (@cf/zai-org/glm-5.3) and edge-deepseek (@cf/deepseek-ai/deepseek-v4.1-flash-1017) on the Workers AI binding. Both hit the per-minute limit in the Workers AI catalog eval lane: probe harness, scorecard, research pack #654 sweep, so each sits in a ladder with a fallback.
  • One key for Claude, GPT, Gemini and Grok: provider gateway calls one OpenAI-compatible endpoint with AI_GATEWAY_API_KEY. Vercel AI Gateway by default (the registry's decided model gateway); AI_GATEWAY_BASE_URL points it elsewhere. No OpenRouter.
  • Direct ids: any @cf/author/model or namespace/model id can be requested as the model.
  • GET /v1/models: the names accepted and whether each can answer with the keys configured.
  • Receipts: each routed response reports class, how it was chosen, the ladder, routes skipped for lack of a key, routes that failed, and estimated_cost_usd.

starlight-auto and every existing model name are unchanged; #631's 15 tests pass untouched.

Verification

At 57c5eb0, Node 22.23.5:

  • node --test src/llm/router.test.mjs src/llm/ai-gateway.test.mjs: 31 pass, 0 fail (16 new, 15 from fix(cloud-worker): honest multi-route Workers AI chat gateway #631).
  • tsc --noEmit --strict on router.ts and ai-gateway.ts: clean.
  • esbuild src/index.ts --bundle with Cloudflare modules external: bundles, so the ./router.ts import resolves the way wrangler resolves it.
  • node .claude/ci/estate-guard-scan.mjs --root .: 0 high.

Not verified:

Privacy and security

  • The gateway key is read from the environment only. There is no caller-supplied key header for it, and a test checks it never appears in a response.
  • A requested model id must match @cf/author/model or namespace/model. Paths, URLs and anything else are a 400 before any provider is called.
  • starlight_stakes: "irreversible" never routes below frontier-balanced. With no such route configured the answer is 503, and a test checks no edge model is asked.
  • Upstream error text is not returned; failed routes are listed with status only.

What this does not do

  • It is not reachable yet. resolveTenant in this worker returns null on purpose, so every protected route, this one included, answers 503 until a reviewed verifier lands. This PR does not change that and does not deploy.
  • The ladders are defaults, not eval-derived rules. They are ordered by tier, not by measured price or quality. The Workers AI catalog eval lane: probe harness, scorecard, research pack #654 run is one probe per model, and routing-table.json hardens a rule only after two concordant rounds.
  • It does not detect a bad answer. A route is skipped when it fails. A cheap model that answers wrongly is returned. starlight_min_tier is the caller's floor.
  • No prices ship. estimated_cost_usd is null unless STARLIGHT_PRICES_JSON is set.
  • No streaming and no tool calls, as in fix(cloud-worker): honest multi-route Workers AI chat gateway #631.

Evidence or preview

No UI. Response shape for a routed call:

{
  "model": "@cf/zai-org/glm-5.3",
  "starlight_model": "starlight-route",
  "starlight_route": "edge-glm",
  "starlight_routing": {
    "task": "constrained-output", "source": "explicit", "stakes": "low",
    "start_tier": "edge-large",
    "ladder": ["edge-deepseek", "edge-glm", "edge-reason", "frontier-fast", "google", "frontier-balanced", "frontier-heavy"],
    "not_configured": ["frontier-fast", "google", "frontier-balanced", "frontier-heavy"],
    "failed": [{ "route": "edge-deepseek", "status": "error" }]
  },
  "estimated_cost_usd": null
}

Substrate gate

Not applicable. Operational change inside services/; no SIP, registry, attestation or canon file is touched.

Implementation: Claude (Anthropic). #631 was written by Antigravity (Google), so review should come from a provider other than Anthropic.


Generated by Claude Code


Run Proof
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…er models

Adds model `starlight-route` to /v1/chat/completions. It names the kind of
work (the classes in starlight-evals routing-table.json), then tries routes
from the cheapest tier up and stops at the first that answers.

- New Workers AI routes: edge-glm (@cf/zai-org/glm-5.3) and edge-deepseek
  (@cf/deepseek-ai/deepseek-v4.1-flash-1017).
- New provider `gateway`: one OpenAI-compatible upstream and one key for
  Anthropic, OpenAI, Google and xAI. Vercel AI Gateway by default.
- Any Workers AI id or gateway id may be requested directly.
- irreversible stakes never route below frontier-balanced; with no such
  route configured the answer is 503.
- Each response reports the class, ladder, skipped and failed routes, and a
  cost estimate that is null unless prices are configured.
- GET /v1/models.

starlight-auto and every existing model name behave as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015JZDKoivSvKQuh571593Bv

frankxai commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

independent-review-policy is red at 57c5eb0: the change touches paths that need an independent review receipt bound to the exact head. This is the gate working, not a defect in the diff. The same check is red on the base branch (#631, 6737029) for the same reason. The other checks on this head pass (evaluate, scan, design-contract, editorial-contract).

The implementation here is Claude (Anthropic), so the receipt has to come from another provider. Nothing I push can clear it.

@codex review


Generated by Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 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-12-01T01:44:30.576007Z 8a8ede6 Manual request
ℹ️ 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: 57c5eb0a08

ℹ️ 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".

…tch what the router does

Three findings from the Codex review of 57c5eb0.

- Cache identity now includes the routing plan and the response format, so a
  cached answer cannot carry another request's task class.
- starlight-route is listed as available only when a route a ladder can
  select is configured. A lone Anthropic or OpenAI key no longer advertises
  a model that answers 503.
- response_format json_object/json_schema is forwarded whole to the routes
  that enforce it (gateway, openai). Other routes are left out for that
  request, and a model with no such route answers 400 instead of prose.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015JZDKoivSvKQuh571593Bv

frankxai commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

All three findings from the review of 57c5eb0 are answered with fixes in 8a8ede6. node --test src/llm/router.test.mjs src/llm/ai-gateway.test.mjs: 34 pass, 0 fail; the three new tests fail on 57c5eb0.

@codex review


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 8a8ede6dac

ℹ️ 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".

This branch has not been deployed

No deployments
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.

2 participants