Repository navigation
fix(model): derive anthropic thinking budget from per-call max_tokens - #3292
Merged
qbc2016 merged 7 commits intoNov 4, 2026
Conversation
_call_api built the thinking config against the parameters-derived max_tokens, so a per-call max_tokens passed via generate_kwargs could be sent with thinking.budget_tokens >= max_tokens, which the Anthropic API rejects. Build the config from the effective kwargs["max_tokens"] so the guard derives and expands against the value actually sent.
Anthropic requires thinking.budget_tokens >= 1024 and strictly less than max_tokens, but the auto budget (half of max_tokens) could emit values below that floor once a per-call max_tokens is honored, and the regression test asserted a provider-invalid budget of 500. Clamp the budget up to the 1024 floor, keep auto-expanding max_tokens for explicit thinking_budget requests, and raise a clear ValueError when the effective max_tokens leaves no valid budget (max_tokens <= 1024) rather than sending a request the API would reject, matching the repo's fail-fast precedent for unsatisfiable caller configs. Tests now use provider-valid values and cover the floor and the error path.
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.
AgentScope Version
2.0.10
Description
Fixes #3288.
With manual thinking enabled,
await model(msgs, max_tokens=4096)used to send a thinking budget of 4096 because AgentScope calculated it from the default output limit of 8192. The request left no room for the answer. AgentScope now uses the effective limit for that call and sends a budget of 2048.The effective
max_tokenscomes from the first available value in this order:extra_body.max_tokens, the per-callmax_tokens, the model parameter, then the framework default of 8192.max(1024, max_tokens // 2). A limit of 1500 yields a budget of 1024. At or below 1024, AgentScope raisesValueErrorbefore sending the request because it cannot fit an automatic budget below the limit.budget + 1024to leave room for the answer.thinkingdirectly or throughextra_bodybypasses budget generation. Adaptive and disabled modes do not generate a budget.Tests
pytest -q tests/model_anthropic_test.py tests/formatter_anthropic_test.pypassed (53 tests and 17 subtests). The regression tests also inspect the HTTP request body produced by the Anthropic SDK using a mock transport, covering explicit limits andextra_bodyoverrides.Checklist