Skip to content

feat: switch a provider to a larger model of its own (#136) - #162

Merged
stackbilt-admin merged 3 commits into
mainfrom
feat/136-larger-own-model
Nov 10, 2026
Merged

stackbilt-admin merged 3 commits into
mainfrom
feat/136-larger-own-model

Conversation

@stackbilt-admin

@stackbilt-admin stackbilt-admin commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Part of #136 (long-context slice: switching a provider to a larger model of its own).

What

New opt-in factory option, contextWindowModelSwitch (off by default). When a provider's planned model can't hold the request's estimated input (or the caller's inputTokenCount), the plan sends that provider's request to its highest-ranked catalog model that can hold the input plus maxTokens, instead of overflowing or being skipped.

The model it switches to must:

  • be active (not a deprecated compatibility model);
  • come from rankModels for the use case the planned model was chosen for, so it passed the request's compatibility checks (vision, tools, structured output; no retired, thinking-only or explicit-only models);
  • suit that use case or be tagged LONG_CONTEXT, so a plain request isn't moved onto a research-only model such as groq/compound;
  • for reasoning.strict, be a Cloudflare model that accepts the effort exactly.

Nothing changes when the planned model already fits, the model is pinned, the planned model isn't in the catalog, or none of the provider's models fits.

A switch is reported as a new optional field, contextWindowSwitch: { from, to } (type ContextWindowSwitch), on LLMResponse and StreamCompletion. It isn't set when a caller's fallback rule sent another model.

Why a separate option

The README promises that contextWindowRouting leaves planned models unchanged. Changing that option's behaviour would break the promise for current users, so the switch is its own opt-in. It works with or without contextWindowRouting.

With both on, the switch runs first, so a provider that switched isn't skipped. The exception: no switch is made when another provider that can be relied on right now, and that allowFallback: false doesn't remove from the chain, already has a fitting planned model (as planned before any switch) with at least twice the switch target's window. That provider is skipped as before. For example, with Workers AI configured, a 100k-token request goes to its 1M-token model rather than to NVIDIA's 128k one.

Where it applies

  • The response and streaming paths, and each turn of generateResponseWithTools.
  • The client getRoutingInfo and response.model show the switched model.
  • The option is in the response cache key when on, so default cache keys are unchanged.

Effect today (checked per use case at 100k, 130k and 300k tokens)

  • Workers AI: it already plans a 1M-token model for every use case except RESEARCH, so only research requests switch (GLM-4.7 Flash to GLM-5.3).
  • NVIDIA: a plain request moves from DeepSeek V4 Flash (64k) to a 128k or 131k model, or to Llama 4 Maverick (1M) for larger inputs.
  • Cerebras: gpt-oss-120b (128k), or llama-3.1-8b for cost-effective requests, moves to zai-glm-4.7 (131k). That model costs about 15 times as much per input token, which the README notes.
  • Groq: no change.

Review fixes (after the first adversarial review)

  • Only active models are switch targets. Before, a plain Cerebras request moved to a Qwen model deprecated on 2026-05-27.
  • The target must hold the input plus maxTokens.
  • Candidates are ranked for the planned model's use case.
  • The 2× rule above, so the switch no longer overrides a much better skip.
  • The new contextWindowSwitch field reports switches.
  • The inputTokenCount docs no longer claim the count can never change the model, and the per-provider claims are corrected.

Review fixes (after the /review pass)

  • The 2× rule now counts only providers that stay in the chain. Before, with contextWindowRouting and allowFallback: false, a 100k-token NVIDIA request counted Workers AI, which the trim then removed. So NVIDIA wasn't switched, was skipped, and the request ended in NoRouteError.
  • The 2× rule compares each provider with its model as planned before any switch, so the result doesn't depend on chain order. This can't happen with today's catalog; it guards against future catalog changes.
  • A cached response is rejected unless contextWindowSwitch is missing or has string from and to values (validate at boundaries).
  • New tests: the 100k allowFallback: false case, chain order, a cache hit returning the stored switch, and a malformed cached switch. Undoing each fix makes its test fail.

Files

  • src/utils/context-window.ts (new, internal): largerOwnModel, requestedOutputTokens.
  • src/utils/response-cache.ts: validates contextWindowSwitch on cached responses.
  • src/factory.ts: the option, a planner block that runs before the context-window skip check, switch reporting, and a cache-key entry.
  • src/types.ts, src/index.ts: ContextWindowSwitch and the two new optional fields.
  • src/__tests__/context-window-model-switch.test.ts (new, 36 tests). Removing any one of the active-only, maxTokens, 2×, remaining-providers, use-case, fallback-rule or cache-validation rules makes a test fail.
  • README (new subsection under "Context windows"), CHANGELOG [Unreleased], and the sweep checkpoint.

Validation

npm run typecheck, npm test (1,074 passing across 36 files), npm run build, bash scripts/package-smoke.sh, npm run test:worker-import, git diff --check: all passed. No live or billable requests were made.

🤖 Generated with Claude Code

kovermier and others added 3 commits November 10, 2026 05:28
Add the opt-in contextWindowModelSwitch factory option. When a provider's
planned model can't hold the request's estimated input (or inputTokenCount),
the plan uses the provider's highest-ranked compatible catalog model that
can, suited to the use case or tagged LONG_CONTEXT, and for strict reasoning
one that accepts the effort exactly. Pinned models are never switched. It
runs before contextWindowRouting's skip check, so a switched provider isn't
skipped. Off by default; part of the response cache key when on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Switch only to active models that hold the input plus maxTokens.
- Rank candidates for the use case the planned model was chosen for.
- With contextWindowRouting, don't switch when a reliable provider already
  has a fitting planned model with at least twice the target's window.
- Report a switch as contextWindowSwitch { from, to } on LLMResponse and
  StreamCompletion (new ContextWindowSwitch type), unless a caller's
  fallback rule sent another model.
- Qualify the inputTokenCount "doesn't change model selection" wording, and
  correct which providers and models the switch changes.
- 10 more tests: lifecycle, maxTokens, window boundary, the 2x rule,
  use-case consistency, fallback-rule override, allowFallback: false,
  Workers AI research with strict reasoning, vision, stream reporting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With contextWindowRouting and allowFallback: false, the 2x rule counted
providers the trim removes, so a 100k request on NVIDIA ended in
NoRouteError instead of switching. The rule now counts only providers
that stay in the chain, each with its model as planned before any
switch, so the result doesn't depend on chain order.

Cached responses now validate contextWindowSwitch. Adds tests for the
allowFallback case, chain order, a cache hit and malformed cached
switches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stackbilt-admin
stackbilt-admin merged commit f88c929 into main Nov 10, 2026
4 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.

2 participants