Skip to content

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

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

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

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.
  • extensions/amazon-bedrock/stream.runtime.test.ts: Added 9 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 — 29/29 passed

Test coverage

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

Real behavior proof

Integration test captures 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)

Resolver proof (option-level):
✅ model.maxTokens=200000 → maxTokens=200000
✅ explicit caller=8000 → maxTokens=8000 (preserved)
✅ medium/xhigh effort → maxTokens=200000 (both)
✅ undefined model.maxTokens → maxTokens=4096 (default)
✅ Fable 5 default reasoning → maxTokens=128000

All 9 tests PASSED ✓
  • AI-assisted

@zw-xysk

zw-xysk commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper

clawsweeper Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 27, 2026, 10:54 AM ET / 14:54 UTC.

Summary
The PR updates Amazon Bedrock simple-stream option resolution and tests so adaptive-thinking and Fable 5 Claude requests can derive maxTokens from model metadata when callers omit an explicit cap.

PR surface: Source +61, Tests +171. Total +232 across 2 files.

Reproducibility: yes. source-reproducible: current main sends Bedrock inferenceConfig.maxTokens only 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.

Review metrics: 1 noteworthy metric.

  • Provider request cap branches: 2 changed, 1 sibling unchanged. Fable and explicit adaptive resolution now use model-backed maxTokens, but mandatory adaptive default resolution still controls a user-visible provider request cap.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #97176
Summary: This PR is a candidate fix for the canonical Bedrock adaptive-thinking maxTokens issue; two other open candidate PRs exist and none is merged.

Members:

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

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🦐 gold shrimp
Result: blocked until real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Apply model-backed maxTokens to the mandatory adaptive default branch and test it.
  • [P1] Add redacted live Bedrock terminal/log proof showing accepted after-fix requests with maxTokens above 4096.
  • Coordinate with maintainers on the single landing branch for the canonical issue.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body shows tests and mocked SDK payload inspection, but no redacted live Bedrock payload, terminal output, runtime log, recording, or linked artifact shows AWS accepting the after-fix request; after adding proof, update the PR body to trigger re-review or ask a maintainer to comment @clawsweeper re-review.

Risk before merge

  • [P1] The no-explicit-reasoning mandatory adaptive branch still keeps the old base-option behavior, so mandatory adaptive Bedrock models can continue omitting model-backed maxTokens.
  • [P1] The PR body and follow-up provide unit tests plus mocked SDK payload inspection, but no redacted live Bedrock payload, terminal output, runtime log, recording, or linked artifact shows AWS accepting the after-fix request.
  • [P1] Merging changes maxTokens sent for existing Bedrock adaptive/Fable simple-stream requests when callers omit maxTokens, which can affect provider quota reservation or cost behavior.
  • [P1] Three open candidate PRs target the same canonical issue, so maintainers should choose one landing branch rather than merge duplicate fixes.

Maintainer options:

  1. Complete one canonical Bedrock fix (recommended)
    Cover the mandatory adaptive default path, add the missing regression test, and provide real Bedrock proof or explicit maintainer acceptance before landing one branch.
  2. Accept the quota behavior deliberately
    A maintainer may accept the raised default request cap for omitted maxTokens after acknowledging the provider quota and cost behavior change.
  3. Pause and pick the landing branch
    Hold this PR until maintainers choose whether this branch, fix: honor Bedrock adaptive max token caps #97248, or fix(bedrock): apply model maxTokens to adaptive-thinking and Fable 5 requests #97182 should be the single candidate to finish.

Next step before merge

  • [P1] Human handling is needed because the branch has a concrete code gap, missing contributor real-provider proof, and competing open candidate PRs for the same canonical issue.

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

Review findings

  • [P2] Apply maxTokens in the default adaptive branch — extensions/amazon-bedrock/stream.runtime.ts:390-393
Review details

Best possible solution:

Land one Bedrock provider-runtime fix that covers Fable, explicit adaptive, and mandatory adaptive default paths, with redacted live Bedrock proof or explicit maintainer acceptance of source-level proof and quota/cost behavior.

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

Yes, source-reproducible: current main sends Bedrock inferenceConfig.maxTokens only 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, not yet: the resolver is the right layer, but this PR should also cover the mandatory adaptive-thinking default branch and still needs real Bedrock proof or explicit maintainer acceptance before merge.

Full review comments:

  • [P2] Apply maxTokens in the default adaptive branch — extensions/amazon-bedrock/stream.runtime.ts:390-393
    This branch still forces mandatory adaptive reasoning: "high" when callers omit reasoning and returns only base, so those Bedrock requests can continue omitting model-backed inferenceConfig.maxTokens. Mirror the new helper in this branch and add a focused regression test for the default adaptive path.
    Confidence: 0.9

Overall correctness: patch is incorrect
Overall confidence: 0.9

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 4010b81a77f4.

Label changes

Label justifications:

  • P2: This is a normal-priority provider runtime bugfix with blast radius limited to Bedrock adaptive-thinking Claude requests.
  • merge-risk: 🚨 compatibility: The patch changes maxTokens sent for existing Bedrock adaptive/Fable requests when callers omit maxTokens, which can affect provider quota or cost behavior on upgrade.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body shows tests and mocked SDK payload inspection, but no redacted live Bedrock payload, terminal output, runtime log, recording, or linked artifact shows AWS accepting the after-fix request; after adding proof, update the PR body to trigger re-review or ask a maintainer to comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +61, Tests +171. Total +232 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 81 20 +61
Tests 1 175 4 +171
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 256 24 +232

What I checked:

  • Repository policy applied: Root and scoped extension policy require whole-path provider-runtime review, dependency contract proof, and compatibility-risk treatment for provider request/default changes. (AGENTS.md:13, 4010b81a77f4)
  • Scoped extension boundary: The extensions policy keeps provider-specific runtime behavior inside the owning plugin boundary; this PR stays in the Amazon Bedrock plugin and does not add SDK surface. (extensions/AGENTS.md:38, 4010b81a77f4)
  • Current request path: streamBedrock only sends inferenceConfig.maxTokens when resolved Bedrock options contain maxTokens, so resolveSimpleBedrockOptions controls whether model metadata reaches the SDK request. (extensions/amazon-bedrock/stream.runtime.ts:213, 4010b81a77f4)
  • Current main gap: Current main returns base for Fable, no-explicit-reasoning mandatory adaptive, and explicit adaptive branches, while only the non-adaptive Claude branch uses adjustMaxTokensForThinking. (extensions/amazon-bedrock/stream.runtime.ts:361, 4010b81a77f4)
  • Submitted branch gap: PR head applies resolveModelBackedMaxTokens to Fable and explicit adaptive branches, but the no-explicit-reasoning mandatory adaptive branch still returns { ...base, reasoning } without maxTokens. (extensions/amazon-bedrock/stream.runtime.ts:390, f65d28fffae0)
  • PR test coverage gap: The new tests cover explicit adaptive/Fable paths, but the existing mandatory adaptive default test still checks only reasoning payload shape and not model-backed maxTokens. (extensions/amazon-bedrock/stream.runtime.test.ts:258, f65d28fffae0)

Likely related people:

  • steipete: Authored the Opus 4.8 and Fable 5 adaptive-thinking work that shaped 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 and mandatory adaptive request shaping plus discovery paths feeding model metadata. (role: recent area contributor; confidence: high; commits: d6081f99ff42, 6b0f718b9a4b; files: extensions/amazon-bedrock/stream.runtime.ts, extensions/amazon-bedrock/discovery.ts, extensions/amazon-bedrock/thinking-policy.ts)
  • wirjo: Authored Bedrock inference-profile and known context-window discovery changes adjacent to the model metadata consumed by this fix path. (role: adjacent discovery contributor; confidence: medium; commits: 78fe96f2d4c7, 2a15a3bb53f9; files: extensions/amazon-bedrock/discovery.ts, extensions/amazon-bedrock/discovery.test.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@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 commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

Added comprehensive test coverage: 9 tests covering adaptive, Fable 5, all effort levels, API integration. @clawsweeper re-review

openclaw#97176)

- Add low reasoning effort test for adaptive-thinking models
- Add Fable 5 boundary test when model.maxTokens is undefined
- Reorder resolveModelBackedMaxTokens before resolveSimpleBedrockOptions
  for clarity (caller-after-callee)
@zw-xysk

zw-xysk commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

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

1 participant