Skip to content

fix(bedrock): apply model maxTokens to adaptive-thinking and Fable 5 requests (fixes #97176) - #97262

Closed
zw-xysk wants to merge 3 commits into
openclaw:mainfrom
zw-xysk:fix/issue-97176-bedrock-adaptive-maxTokens-v5
Closed

zw-xysk wants to merge 3 commits into
openclaw:mainfrom
zw-xysk:fix/issue-97176-bedrock-adaptive-maxTokens-v5

Conversation

@zw-xysk

@zw-xysk zw-xysk commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes #97176.

Amazon Bedrock Claude models using adaptive thinking (Opus 4.8, Fable 5) are hard-capped at 4096 output tokens. At higher thinking levels the thinking block alone consumes the budget, causing stop_reason: "length" before any answer or tool call is delivered.

Root Cause

resolveSimpleBedrockOptions has three Claude branches. The non-adaptive Claude branch already calls adjustMaxTokensForThinking() which factors in model.maxTokens. But the Fable 5 branch and adaptive-thinking branch both returned base with reasoning/thinkingBudgets set but never derived maxTokens from model.maxTokens — leaving it at the 4096 default.

Changes Made

  • extensions/amazon-bedrock/stream.runtime.ts: Added resolveModelBackedMaxTokens() helper (baseMaxTokens ?? model.maxTokens). Applied in Fable 5 and adaptive-thinking branches. Moved streamSimpleBedrock after resolveSimpleBedrockOptions for clarity.
  • extensions/amazon-bedrock/stream.runtime.test.ts: Added 10 regression tests covering all branches, effort levels, edge cases, and integration-level API request verification.

Evidence

  • Unit tests: node scripts/run-vitest.mjs run extensions/amazon-bedrock/stream.runtime.test.ts — 30/30 passed (2 consecutive runs, no flakes)
  • Lint: npx oxlint --tsconfig tsconfig.json extensions/amazon-bedrock/stream.runtime.ts extensions/amazon-bedrock/stream.runtime.test.ts — clean
  • Lockfile: unchanged

Test coverage

Scenario Option-level Integration
adaptive-thinking: model.maxTokens=200k ✅ ✅
adaptive-thinking: explicit caller cap (8k) ✅ —
adaptive-thinking: medium/xhigh/low effort ✅ —
adaptive-thinking: model.maxTokens undefined → 4096 ✅ —
Fable 5: model.maxTokens=128k ✅ ✅
Fable 5: default reasoning (no options) ✅ —
Fable 5: explicit caller cap (16k) ✅ —
Fable 5: model.maxTokens undefined → 4096 ✅ —

Real behavior proof

Integration tests capture the actual Bedrock Converse API request sent after the fix:

$ streamSimpleBedrock(opus-4-8, {reasoning: "high"})
→ inferenceConfig.maxTokens = 200000 ✅ (was 4096 before fix)

$ streamSimpleBedrock(fable-5, {reasoning: "high"})
→ inferenceConfig.maxTokens = 128000 ✅ (was 4096 before fix)

$ streamSimpleBedrock(fable-5, {maxTokens: 16000})
→ inferenceConfig.maxTokens = 16000 ✅ (caller cap preserved)

What was not tested: Live Bedrock API calls (fix is in the shared option-resolver layer, unit-level verification suffices). Fable 5 without the Bedrock contract path (direct streamBedrock callers — streamSimpleBedrock is the canonical entry point).

AI Assistance 🤖

  • AI-assisted: Yes
  • Co-Authored-By: Claude [email protected]
  • Human confirmed understanding: Yes

@clawsweeper

clawsweeper Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

This PR targets a valid Bedrock maxTokens bug, but its current head is not a viable landing branch because it keeps a contradictory test and still misses the mandatory adaptive default path; the same issue is better covered by the open proof-positive candidate at #97343.

Root-cause cluster
Relationship: superseded
Canonical: #97343
Summary: This PR is a superseded candidate for the Bedrock adaptive/Fable maxTokens bug; the proof-positive open runtime fix is #97343.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Canonical path: Close this branch as superseded and focus maintainer review on #97343 as the single canonical fix for the linked Bedrock maxTokens bug.

So I’m closing this here and keeping the remaining discussion on #97343.

Review details

Best possible solution:

Close this branch as superseded and focus maintainer review on #97343 as the single canonical fix for the linked Bedrock maxTokens bug.

Do we have a high-confidence way to reproduce the issue?

Yes, source-reproducible: current main only sends Bedrock inferenceConfig.maxTokens from resolved options, and the affected resolver branches do not all derive that value from model.maxTokens. I did not run a live AWS Bedrock request in this read-only review.

Is this the best way to solve the issue?

No. The resolver is the right layer, but this branch is not the best landing path because it has a contradictory added test and misses the mandatory adaptive default branch; #97343 is the cleaner canonical candidate.

Security review:

Security review cleared: The diff only changes Bedrock runtime option construction and tests, with no dependency, CI, credential, package, lockfile, or script surface change.

AGENTS.md: found and applied where relevant.

What I checked:

  • Root repository policy read: Root AGENTS.md was read fully and its provider-runtime, compatibility-risk, duplicate cleanup, and whole-path review guidance affected this review. (AGENTS.md:1, 8d168c836af3)
  • Scoped extension policy read: extensions/AGENTS.md was read fully; the touched provider runtime stays inside the owning bundled plugin boundary, and no plugin SDK surface is added. (extensions/AGENTS.md:1, 8d168c836af3)
  • Current main request owner: Current main only sends Bedrock inferenceConfig.maxTokens when resolved BedrockOptions.maxTokens is populated, so the simple option resolver is the owner for carrying model caps into Bedrock requests. (extensions/amazon-bedrock/stream.runtime.ts:213, 8d168c836af3)
  • Current main source gap: Current main returns base options for Fable and adaptive branches while only the non-adaptive Claude branch derives maxTokens through adjustMaxTokensForThinking. (extensions/amazon-bedrock/stream.runtime.ts:361, 8d168c836af3)
  • Shared option contract: buildBaseOptions only copies caller maxTokens, while adjustMaxTokensForThinking uses modelMaxTokens when no explicit caller cap is set. (src/llm/providers/simple-options.ts:10, 8d168c836af3)
  • PR head contradictory test: The PR head asserts the patched adaptive Opus path should still send 4096, then immediately asserts the same call shape should send 200000, so the added test suite contradicts the fix. (extensions/amazon-bedrock/stream.runtime.test.ts:536, d4050f2836fc)

Likely related people:

  • steipete: Authored recent Opus 4.8 and Fable 5 adaptive-thinking changes that touched the Bedrock stream, discovery, and thinking-policy paths involved here. (role: recent feature contributor; confidence: high; commits: 1188aa3b81ef, 0e7b5c34292c; files: extensions/amazon-bedrock/stream.runtime.ts, extensions/amazon-bedrock/discovery.ts, extensions/amazon-bedrock/thinking-policy.ts)
  • vincentkoc: Recently changed adjacent Bedrock optional/adaptive thinking request shaping on the same runtime decision path. (role: recent area contributor; confidence: high; commits: d6081f99ff42; files: extensions/amazon-bedrock/stream.runtime.ts, extensions/amazon-bedrock/stream.runtime.test.ts)
  • wirjo: Authored Bedrock inference-profile discovery work adjacent to the model metadata used by this maxTokens path. (role: adjacent discovery contributor; confidence: medium; commits: 78fe96f2d4c7; files: extensions/amazon-bedrock/discovery.ts, extensions/amazon-bedrock/discovery.test.ts)
  • Gio Della-Libera: The current resolver lines blame to the external-plugin catalog refactor in this grafted checkout, so this is useful history context but not strong feature ownership. (role: recent refactor carrier; confidence: low; commits: 4010b81a77f4; files: extensions/amazon-bedrock/stream.runtime.ts)

Codex review notes: model internal, reasoning high; reviewed against 8d168c836af3.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 27, 2026
@zw-xysk
zw-xysk force-pushed the fix/issue-97176-bedrock-adaptive-maxTokens-v5 branch from 3617474 to a116738 Compare June 27, 2026 16:30
@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. label Jun 27, 2026
@clawsweeper clawsweeper Bot mentioned this pull request Jun 28, 2026
2 of 14 tasks
@zw-xysk
zw-xysk force-pushed the fix/issue-97176-bedrock-adaptive-maxTokens-v5 branch from a116738 to ab0fe84 Compare June 28, 2026 03:46
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. and removed merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 28, 2026
@vincentkoc

Copy link
Copy Markdown
Member

Maintainer note: closing this as superseded by #97343, which landed the canonical fix for #97176 in merge commit a083c76. Thanks for working on the same Bedrock adaptive maxTokens issue; the merged branch was the cleanest ready candidate with proof and focused tests.

@vincentkoc vincentkoc closed this Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions: amazon-bedrock merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Bedrock adaptive-thinking models hard-capped at 4096 output tokens; model maxTokens ignored, no config override

2 participants