Skip to content

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

Closed
LiuwqGit wants to merge 3 commits into
openclaw:mainfrom
LiuwqGit:feat/issue-97176-bedrock-adaptive-maxtokens-cap
Closed

LiuwqGit wants to merge 3 commits into
openclaw:mainfrom
LiuwqGit:feat/issue-97176-bedrock-adaptive-maxtokens-cap

Conversation

@LiuwqGit

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.

Why This Change Was Made

Root cause: resolveSimpleBedrockOptions in extensions/amazon-bedrock/stream.runtime.ts only calls adjustMaxTokensForThinking() for the non-adaptive Claude branch. The Fable 5 branch (line 361) and adaptive-thinking branch (line 391) both return base with reasoning/thinkingBudgets set but never derive maxTokens from model.maxTokens — leaving it at the 4096 default.

Fix: Apply adjustMaxTokensForThinking() in all three Claude branches so model.maxTokens is consulted uniformly. When no explicit caller cap is set (base.maxTokens is undefined), the helper uses model.maxTokens directly; when a cap exists, it fits the thinking budget inside it.

Branch Before After
Fable 5 base.maxTokens (4096) adjusted.maxTokens (model cap)
Adaptive thinking base.maxTokens (4096) adjusted.maxTokens (model cap)
Non-adaptive Claude adjusted.maxTokens ✓ unchanged

Evidence

Source-level reproduction (current main)

// extensions/amazon-bedrock/stream.runtime.ts:361-367
if (usesClaudeFable5BedrockContract(model)) {
  return { ...base, reasoning: ..., thinkingBudgets: ... };
  // ^ base.maxTokens is 4096 (options?.maxTokens ?? default)
  //   model.maxTokens is 200000+ but never consulted
}

// extensions/amazon-bedrock/stream.runtime.ts:391-407
if (supportsAdaptiveThinking(model)) {
  return { ...base, reasoning: ..., thinkingBudgets: ... };
  // ^ same: base.maxTokens stays at 4096
}

Non-adaptive Claude branch (lines 411-427) correctly calls adjustMaxTokensForThinking() — this fix extends that same pattern.

adjustMaxTokensForThinking contract (simple-options.ts:42-71):

  • baseMaxTokens === undefined → returns modelMaxTokens
  • baseMaxTokens set → returns Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens)
  • Always reserves ≥1024 tokens for output after thinking

After fix

Opus 4.8 (maxTokens 200000, thinking high): maxTokens=200000, thinkingBudget=16384
Fable 5 (maxTokens 128000, thinking high): maxTokens=128000, thinkingBudget=16384

Tests

node scripts/run-vitest.mjs run extensions/amazon-bedrock/stream.runtime.test.ts extensions/amazon-bedrock/index.test.ts
# 2 files, 73 tests passed
npx oxlint --deny=warn extensions/amazon-bedrock/stream.runtime.ts
# ok

Risk checklist

  • Only Bedrock Converse provider affected — 1 file changed
  • Reuses existing adjustMaxTokensForThinking helper (same as non-adaptive branch)
  • Explicit options.maxTokens still acts as a cap (helper preserves Math.min)
  • No config, schema, or SDK changes
  • All 73 Bedrock tests pass

Proof limitations

Live AWS Bedrock proof is not practical in this contributor PR. The fix is source-reproducible:

  • ClawSweeper confirmed the source gap on main in the canonical issue review
  • The non-adaptive Claude branch already proves the same adjustMaxTokensForThinking pattern works
  • AWS SDK contract supports inferenceConfig.maxTokens (verified in ClawSweeper review)

Current review state

Ready for ClawSweeper review. Triple-stack labeling (queueable-fix, fix-shape-clear, source-repro) on the canonical issue.

@clawsweeper

clawsweeper Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 27, 2026, 8:09 AM ET / 12:09 UTC.

Summary
Updates Amazon Bedrock Converse option resolution and tests so Fable 5 and adaptive Claude requests derive maxTokens from model metadata via adjustMaxTokensForThinking.

PR surface: Source +21, Tests +135. Total +156 across 2 files.

Reproducibility: yes. at source level: current main only sends Bedrock inferenceConfig.maxTokens from resolved options, and the Fable/adaptive branches do not derive that value from model.maxTokens while the non-adaptive Claude branch does. I did not run a live AWS Bedrock request in this read-only review.

Review metrics: 1 noteworthy metric.

  • Provider request cap paths: 2 branches changed. Fable 5 and adaptive Claude option resolution now send computed Bedrock maxTokens, so maintainers should notice the runtime quota and cost behavior change before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #97176
Summary: This PR is the linked candidate fix for the open Bedrock adaptive-thinking maxTokens bug; no separate merged canonical fix was found.

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: 🦞 diamond lobster
Result: blocked until real behavior proof from a real setup is added.

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

Rank-up moves:

  • [P1] Add a redacted after-fix Bedrock payload, terminal output, runtime log, recording, or linked artifact showing adaptive thinking plus the raised inferenceConfig.maxTokens.
  • Redact private details such as credentials, account IDs, phone numbers, IP addresses, and non-public endpoints before posting proof.
  • After adding proof, update the PR body so ClawSweeper re-reviews automatically; if it does not, ask a maintainer to comment @clawsweeper re-review.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body and new tests show source-level and mocked SDK payload evidence, but no redacted live Bedrock payload, runtime log, terminal output, recording, or linked artifact shows the after-fix provider request. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] No redacted live Bedrock payload, terminal output, runtime log, or linked artifact shows AWS accepting an after-fix adaptive/Fable request with the raised inferenceConfig.maxTokens.
  • [P1] Merging raises maxTokens sent for existing Bedrock adaptive/Fable simple-stream requests, which can affect provider quota reservation or cost behavior even though it fixes truncation.

Maintainer options:

  1. Add Bedrock payload proof before merge (recommended)
    Attach a redacted after-fix Bedrock payload, terminal output, runtime log, or linked artifact showing adaptive thinking fields and the raised inferenceConfig.maxTokens on a real request.
  2. Accept source-level provider proof
    A maintainer may choose to merge based on the source path, focused tests, AWS API contract, and the linked issue's direct Bedrock evidence if live credentials are unavailable.
  3. Pause for Bedrock owner review
    Hold the PR if quota reservation or cost behavior needs owner acceptance before changing the default request cap for existing Bedrock users.

Next step before merge

  • [P1] Human review is needed for the real-behavior-proof gate and Bedrock quota/cap acceptance; there is no narrow automated code repair left to request.

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

Review details

Best possible solution:

Land the focused provider-runtime fix after redacted real Bedrock proof is added, or after a maintainer explicitly accepts source-level proof and the quota/cost behavior change.

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

Yes, at source level: current main only sends Bedrock inferenceConfig.maxTokens from resolved options, and the Fable/adaptive branches do not derive that value from model.maxTokens while the non-adaptive Claude branch does. I did not run a live AWS Bedrock request in this read-only review.

Is this the best way to solve the issue?

Yes, this appears to be the best fix shape because it reuses the existing adjustMaxTokensForThinking helper in the Bedrock simple option resolver instead of adding a new config surface. The remaining blocker is real provider proof or maintainer acceptance of source-level evidence.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This is a normal-priority provider runtime bugfix with a clear source path and limited blast radius to Bedrock adaptive-thinking Claude requests.
  • merge-risk: 🚨 compatibility: The PR changes maxTokens sent for existing Bedrock adaptive/Fable simple-stream requests, which can affect provider quota or cost behavior even when the bugfix is correct.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦞 diamond lobster.
  • 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 and new tests show source-level and mocked SDK payload evidence, but no redacted live Bedrock payload, runtime log, terminal output, recording, or linked artifact shows the after-fix provider request. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +21, Tests +135. Total +156 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 24 3 +21
Tests 1 136 1 +135
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 160 4 +156

What I checked:

Likely related people:

  • steipete: Authored Opus 4.8 support and Fable 5 adaptive-thinking changes touching the Bedrock stream, discovery, and thinking-policy surfaces 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, 6b0f718b9a4b; files: extensions/amazon-bedrock/stream.runtime.ts)
  • wirjo: Authored Bedrock inference-profile discovery and known context-window work adjacent to the model metadata used by this maxTokens 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: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. 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
@LiuwqGit

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 added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jun 27, 2026
@LiuwqGit

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.

@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
@LiuwqGit
LiuwqGit deleted the feat/issue-97176-bedrock-adaptive-maxtokens-cap branch September 22, 2026 14:47
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: S 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