Skip to content

feat: Workers AI reasoning control and pinned-model integrity (#135, #133, #128) - #139

Merged
stackbilt-admin merged 5 commits into
mainfrom
feat/136-pin-reasoning
Nov 6, 2026
Merged

stackbilt-admin merged 5 commits into
mainfrom
feat/136-pin-reasoning

Conversation

@stackbilt-admin

Copy link
Copy Markdown
Member

First slice of #136. Closes #135, closes #133, closes #128. Additive, so it targets a minor release (1.24.0).

Reasoning control on Workers AI (#135, #136 need 1)

Each Workers AI reasoning model documents its own reasoning_effort levels, and some document aliases that turn a "lower" value into the model's maximum (GLM-5.3 Flash maps none and medium to max). So reasoning.effort is mapped per model, from each model's Cloudflare docs page (checked 2026-11-06, cited in src/providers/cloudflare-reasoning.ts):

Model Sent as requested Mapped Not sent
gpt-oss 20B / 120B low, medium, high none
GLM-5.3, GLM-5.3 Flash low, high medium → high none
GLM-5.2 none, high low, medium → high
DeepSeek V4 Flash / Pro none, low, high medium → high
Kimi K2.6 none, high low, medium → high
Qwen 3.8 27B low, medium high → xhigh none
Nemotron 3 120B none, low (chat_template_kwargs) medium, high → default

Models whose pages document no control get nothing sent.

What callers get:

  • LLMResponse.reasoningEffort reports { requested, applied, exact }.
  • reasoning.strict: true throws InvalidRequestError before the call when the level can't be sent exactly.
  • ModelCapabilities.reasoningEffort ({ exact, source, verifiedAt }) appears in the catalog and in the provider capabilities. Both read the same table.
  • TokenUsage.reasoningTokens, from Workers AI completion_tokens_details / output_tokens_details.
  • LLMResponse.lengthCause ('reasoning' | 'answer') when finishReason is 'length'. It is set by the Cloudflare provider and by the factory for every provider, and an unclosed <think> block counts as reasoning.

Evidence gap: for gpt-oss, the field name comes from Cloudflare's workers-ai-provider 3.1.12 (PR cloudflare/ai#504), which forwards top-level reasoning_effort for chat input. The gpt-oss model page lists the levels but not the chat request field. None of this was tested against live Workers AI.

Pinned-model integrity (#128, #133)

  • fallbackRules: [] was never a full opt-out. Server errors, network errors, timeouts and schema drift fall back after rules are evaluated. A test now shows this.
  • LLMRequest.allowFallback: false limits the plan to the first provider (the pinned model's provider) and rethrows its error, overriding both rules and built-in fallback. It throws NoRouteError if no configured provider can serve the pin. This covers generateResponse, the tool loop, generateManagedResponseStream and getRoutingInfo. allowFallback is part of the semantic request, so the response cache already keys on it.
  • LLMResponse.fallback / StreamCompletion.fallback carry { requestedProvider, requestedModel, failedAttempts }. A planned provider that was skipped (quota-exhausted, circuit-open, ledger-limited, or unable to stream) counts as a failed attempt. normalizeLLMResponse() copies the attempts into routing.fallbackChain.
  • StreamCompletion.provider / model report what served the stream.
  • onRequestError.willRetry is false when allowFallback is false.

Behavior change

Requests that already set reasoning.effort and are served by a mapped Workers AI model now actually send it; until now it was silently dropped. Output length, latency and cost can change. This is noted in the CHANGELOG.

Not in this PR

Validation (Windows, Node 24)

  • npm test: 813 passed across 31 files, including 24 new in src/__tests__/pin-and-reasoning.test.ts.
  • npm run typecheck and npm run test:worker-import: passed.
  • No live provider calls.

🤖 Generated with Claude Code

kovermier and others added 5 commits November 6, 2026 08:19
…133, #128)

First slice of #136.

- Map reasoning.effort per Workers AI model from each model's Cloudflare
  docs page (reasoning_effort, or chat_template_kwargs for Nemotron);
  never forward undocumented values or aliases that mean max.
- Report what was sent in LLMResponse.reasoningEffort; reasoning.strict
  rejects inexact levels before the call; expose the mapping as
  ModelCapabilities.reasoningEffort in the catalog and provider.
- Add TokenUsage.reasoningTokens and LLMResponse.lengthCause.
- Add LLMRequest.allowFallback: false, which stops all failover including
  built-in fallback, and throws NoRouteError for an unservable pin.
- Mark substitutions with LLMResponse.fallback / StreamCompletion.fallback,
  including skipped providers; add StreamCompletion.provider and model.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- reasoning.strict: new ReasoningEffortUnsupportedError (extends
  InvalidRequestError) never triggers fallback, and strict requests with an
  effort never fail over to a provider other than Cloudflare, including via
  the default any-error rule or a rule's fallbackProvider.
- allowFallback: false with a skipped pinned provider throws NoRouteError
  naming the skip; ALL_PROVIDERS_FAILED messages list skipped providers.
- Response cache: validate fallback, reasoningEffort and lengthCause on
  cached envelopes; document that hits replay the stored fallback report.
- lengthCauseFor: handle think tags anywhere, template-opened think blocks
  (closing tag only) and untagged output from thinking models (QwQ,
  R1-Distill).
- fallback.requestedModel is captured at plan time.
- Nemotron medium/high are no longer reported as 'default'; they send
  nothing and report applied: null.
- Docs: strict semantics, GLM-5.3 aliases, stream completion fields.
- Tests for each, and a deterministic Math.random in a flaky stream test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- reasoning.strict with an effort is served only by Cloudflare, first hop
  included. A pin on another provider, or no Cloudflare provider, throws
  ReasoningEffortUnsupportedError before any call; an unpinned strict
  request goes to Cloudflare even when another provider is preferred.
- onRequestError.willRetry is false when a strict chain has no next hop.
- lengthCauseFor only reads think structure at the start of the message,
  so answer text that mentions <think> is no longer called reasoning.
- Nemotron medium/high send normal reasoning (enable_thinking: true)
  explicitly, per the model page's three documented modes; inexact.
- The factory no longer caches envelopes its read path would reject.
- No-route message says "First planned provider" when nothing was pinned.
- Tests: non-Cloudflare pin, no Cloudflare provider, preferred provider,
  tool loop, isolated fallback guard, willRetry, cache write guard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- A strict request pinned to a model no configured Cloudflare provider can
  serve (unconfigured provider, unknown model, images the pin can't take)
  throws ReasoningEffortUnsupportedError instead of being served by a
  default Cloudflare model.
- lengthCauseFor applies the template-opened rule only to thinking models
  and only when the message doesn't start with <think>, so answers ending
  in a literal </think> and reasoning that quotes <think> classify right.
- A skipped response-cache write logs a warning and emits an onCache
  bypass event.
- Docs: the error now also covers strict routing failures; getRoutingInfo
  and allowFallback paragraphs note it; unpinned strict uses the default
  Workers AI model, so pin a model with strict.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- README: with allowFallback: false, a pin no configured provider can
  serve throws NoRouteError (that check runs first); only a pin on another
  configured provider throws ReasoningEffortUnsupportedError. Same note on
  getRoutingInfo.
- Tests: that precedence, and willRetry: false on the stream path for a
  strict request with no next hop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stackbilt-admin
stackbilt-admin merged commit 0a2dae1 into main Nov 6, 2026
2 checks passed
@stackbilt-admin
stackbilt-admin deleted the feat/136-pin-reasoning branch November 6, 2026 19:45
stackbilt-admin added a commit that referenced this pull request Nov 6, 2026
Workers AI reasoning control and pinned-model integrity (#139).
Bumps package.json, package-lock.json and src/version.ts to 1.24.0,
dates the CHANGELOG section, and refreshes the issue-sweep checkpoint.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants