Skip to content

fix(anthropic): default max_tokens from the deployment's max_output_tokens - #46433

Closed
uje-m wants to merge 1 commit into
BerriAI:mainfrom
uje-m:fix/anthropic-default-max-tokens-from-deployment
Closed

uje-m wants to merge 1 commit into
BerriAI:mainfrom
uje-m:fix/anthropic-default-max-tokens-from-deployment

Conversation

@uje-m

@uje-m uje-m commented Oct 14, 2026

Copy link
Copy Markdown

Relevant issues

Fixes #46432. Related: #40363 / #40376, the same 4096 symptom for versioned Vertex Claude ids. I commented on #40376 about how the two fit together.

Problem

A chat-completions request without max_tokens to a proxy deployment like anthropic/<custom-model> with model_info.max_output_tokens: 131072 goes upstream with max_tokens: 4096.

transform_request gets the default from get_config(model=model), and model is provider-stripped by then. The router registers the deployment under the prefixed name, so the lookup misses and falls back to DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS. The deployment's model_info already reaches transform_request in litellm_params["model_info"]; nothing reads it.

Change

In AnthropicConfig.transform_request, when the request has no max_tokens and litellm.AnthropicConfig.max_tokens is unset, use litellm_params["model_info"]["max_output_tokens"] if it is a positive int.

Precedence after the change: request value > class-level default > deployment max_output_tokens > existing catalog lookup > 4096.

Deployments without the field behave exactly as before. It sits above the ## Load Config block that #40376 edits, so the two PRs touch different lines.

Tests

tests/unit/llms/anthropic/chat/test_anthropic_chat_transformation.py:

  • test_transform_request_default_max_tokens_from_deployment, parametrized:
    • deployment limit used (131072);
    • an explicit request value wins;
    • no model_info → 4096;
    • max_output_tokens: None → 4096.
  • test_router_sends_deployment_max_output_tokens_when_request_omits_max_tokens: goes through litellm.Router with the Anthropic endpoint mocked by respx, and asserts the outgoing bodies carry [131072, 512] for a call without max_tokens and a call with max_tokens=512.

Mutation: with the transform_request change reverted, the deployment case and the router test fail (assert 4096 == 131072, [4096, 512] == [131072, 512]). Restored, all pass.

Local runs:

  • tests/unit/llms/anthropic, azure_ai, vertex_ai/vertex_ai_partner_models, bedrock/chat and router_utils: 5738 passed.
  • type_check_gate, type_discipline_gate and test_quality_gate against main: OK.
  • ruff check and ruff format: clean.

Type

🐛 Bug Fix

…okens

A chat-completions request without max_tokens gets its default from get_config(), which looks the
model up by its provider-stripped name. A proxy deployment such as `anthropic/<custom-model>` is
registered under the prefixed name, so the lookup misses and the request goes upstream capped at
DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS (4096) even though model_info sets max_output_tokens.

transform_request now uses litellm_params["model_info"]["max_output_tokens"] when the request has no
max_tokens and no class-level default is set. An explicit request value and AnthropicConfig(max_tokens=...)
still win; deployments without the field keep the existing lookup and 4096 fallback.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


ujangm seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codspeed

codspeed Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 31 untouched benchmarks


Comparing uje-m:fix/anthropic-default-max-tokens-from-deployment (b9d54d1) with main (aa0279e)

Open in CodSpeed

@codecov

codecov Bot commented Oct 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

Main already uses the deployment's max_output_tokens as the default max_tokens since #49952, so closing this. Reopen if it still happens or something is missing

@mateo-berri mateo-berri closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: chat completions to a custom anthropic/<model> deployment go upstream with max_tokens=4096 despite model_info.max_output_tokens

3 participants