Skip to content

fix(model): derive anthropic thinking budget from per-call max_tokens - #3292

Merged
qbc2016 merged 7 commits into
agentscope-ai:mainfrom
BlueX888:fix/prep-anthropic-budget-ignores-per-call-max-tokens
Nov 4, 2026
Merged

qbc2016 merged 7 commits into
agentscope-ai:mainfrom
BlueX888:fix/prep-anthropic-budget-ignores-per-call-max-tokens

Conversation

@BlueX888

@BlueX888 BlueX888 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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_tokens comes from the first available value in this order: extra_body.max_tokens, the per-call max_tokens, the model parameter, then the framework default of 8192.

  • Automatic budget. In manual thinking mode, an omitted budget is calculated as max(1024, max_tokens // 2). A limit of 1500 yields a budget of 1024. At or below 1024, AgentScope raises ValueError before sending the request because it cannot fit an automatic budget below the limit.
  • Explicit budget. AgentScope forwards the configured budget for the provider to validate. For example, a budget of 512 stays 512. If the caller specifies both a budget of 4096 and a limit of 1500, both values are forwarded as supplied.
  • Framework default. Only when the caller omits the output limit can AgentScope expand it. If an explicit budget reaches or exceeds the 8192-token default, AgentScope sets the limit to budget + 1024 to leave room for the answer.
  • Provider-native configuration. Passing thinking directly or through extra_body bypasses budget generation. Adaptive and disabled modes do not generate a budget.

Tests

pytest -q tests/model_anthropic_test.py tests/formatter_anthropic_test.py passed (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 and extra_body overrides.

Checklist

_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.

@qbc2016 qbc2016 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@qbc2016
qbc2016 merged commit f37adb1 into agentscope-ai:main Nov 4, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr/awaiting-review Waiting for maintainers to review this PR

Projects

None yet

2 participants