Repository navigation
feat: switch a provider to a larger model of its own (#136) - #162
Merged
Merged
Conversation
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]>
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.
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'sinputTokenCount), the plan sends that provider's request to its highest-ranked catalog model that can hold the input plusmaxTokens, instead of overflowing or being skipped.The model it switches to must:
active(not a deprecatedcompatibilitymodel);rankModelsfor 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);LONG_CONTEXT, so a plain request isn't moved onto a research-only model such asgroq/compound;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 }(typeContextWindowSwitch), onLLMResponseandStreamCompletion. It isn't set when a caller's fallback rule sent another model.Why a separate option
The README promises that
contextWindowRoutingleaves 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 withoutcontextWindowRouting.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: falsedoesn'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
generateResponseWithTools.getRoutingInfoandresponse.modelshow the switched model.Effect today (checked per use case at 100k, 130k and 300k tokens)
RESEARCH, so only research requests switch (GLM-4.7 Flash to GLM-5.3).gpt-oss-120b(128k), orllama-3.1-8bfor cost-effective requests, moves tozai-glm-4.7(131k). That model costs about 15 times as much per input token, which the README notes.Review fixes (after the first adversarial review)
maxTokens.contextWindowSwitchfield reports switches.inputTokenCountdocs no longer claim the count can never change the model, and the per-provider claims are corrected.Review fixes (after the
/reviewpass)contextWindowRoutingandallowFallback: 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 inNoRouteError.contextWindowSwitchis missing or has stringfromandtovalues (validate at boundaries).allowFallback: falsecase, 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: validatescontextWindowSwitchon 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:ContextWindowSwitchand 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.[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