Repository navigation
fix(vertex_ai): resolve correct default max_tokens for versioned claude ids - #40376
Conversation
Merging this PR will not alter performance1 warning
Comparing |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
[Medium impact] The reviewed code appears safe to merge, with no new blocking finding. SummaryResolves missing
Reviews (9) · Last reviewed commit: "Merge branch 'main' into fix-vertex-clau..." · Reviewed by Greptile |
2924e67 to
cccedd9
Compare
mihidumh
left a comment
There was a problem hiding this comment.
Reporter of #40363 here — thanks for picking it up. Two observations after reading the diff:
- The versioned-id lookup is vertex-only.
get_max_tokens_for_modelis overridden onVertexAIAnthropicConfigand keys onvertex_ai/<model>. The same defect class exists for everyAnthropicConfigsubclass:AzureAnthropicConfigruns the sameget_configfill, and its rows resolve underazure_ai/<model>keys, so an id that is not a bare map key still lands on the 4096 default there. A provider-aware lookup in the shared config (get_max_tokens_for_model(model, custom_llm_provider)→ tryf"{provider}/{model}"before defaulting) fixes all subclasses in one place. That is what my held branch does, offered for cherry-pick if useful: litellm_internal_staging...mihidumh:litellm:fix/anthropic-max-tokens-provider-aware-default (3 tests, 375/375 across the two test files). One trap I hit there:get_model_infowidens unknown ids via its family heuristic (an unmapped id came back as 64000), so I read the exact key only. - The
AnthropicConfig.__dict__block inget_configre-implementsBaseConfig.get_configfor a fixed class. Same filter, same shape —BaseConfig.get_config.__func__(AnthropicConfig)(or a small shared helper) would avoid the second copy. The switch fromlitellm.AnthropicConfig.get_config(...)toself.get_config(...)intransform_requestis the part that actually makes subclass overrides reachable, and looks right.
The map hunk matches what I measured: a max_tokens: 64000 request on claude-haiku-4-5@20251001 via Vertex returned 15,290 output tokens, so 64000 is the real cap on both vertex_ai/claude-haiku-4-5* keys (Google's page says the same). Note #40374's map-only fix was closed as "carried by #31884" — as of today that PR's diff has no vertex_ai/claude-haiku-4-5 entry, so this PR is the only one carrying the map half.
|
thanks for the detailed read, good points on both. i'm keeping this pr scoped to the vertex_ai defect from #40363 bcs the provider-aware default changes the fill for every AnthropicConfig subclass at once, so that half (and the BaseConfig.get_config reuse) is its own change, your branch is a good base for it — let's see what the maintainers prefer on the shared-config direction |
|
Thanks. Scoping this PR to One correction to my review: the two map entries already landed on |
cccedd9 to
d6ac17d
Compare
|
thanks for the double check, i verified #31884 and you are right, the map hunk is already on main so i dropped it from this pr, the get_max_tokens_for_model override is what stays now, tests are green on the new head |
|
Heads-up @hktitof: this PR still targets |
|
thanks for the heads up, retargeted the pr to main, it applies clean with no conflicts so no rebase was needed. keeping this one alive since the code half of #40363 is still not fixed upstream |
|
Heads-up about a related fix, so the two don't collide. The same silent 4096 default also hits proxy deployments of custom Anthropic-compatible models: I'm opening a separate PR for that case. It reads After both land: a Vertex deployment with |
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
… for a missing max_tokens A Vertex @Version id now reads the cost-map row of its id without the version before the vertex_ai row, so claude-sonnet-4-6@default keeps the 128000 default that Vertex accepts while versioned Haiku, Opus, and Sonnet 4.5 ids still get 64000.
…ead of the unversioned and provider rows An operator who set DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS, for example to 4096 as the v1.109.0 notes suggest, got that value for every Claude id with no own cost-map row. The new unversioned and provider-row steps ranked above it, so vertex_ai/claude-haiku-4-5@20251001 went from 4096 to 64000 and claude-sonnet-4-6@default from 4096 to 128000 for them. An explicit value now comes right after the model's own row, so only deployments on the built-in fallback change
…llback after the unversioned and provider rows Reverts the previous commit. With DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS set to any value, versioned Vertex Claude ids ignored their model's row again: at 4096 a Haiku 4.5 reply still stopped at 4096 tokens, the symptom the linked issue reports, and at 128000 Vertex still answered 400. The env var is documented as the default for models missing from the cost map, and a versioned id whose model has a row is not missing, so it now gets the same cap as its unversioned id
… the lookup on other routes The unversioned lookup went through get_max_tokens, which prints the provider banner a second time when the id has no row, so SDK calls on ids like claude-3-5-haiku@20241022 printed it twice. Reading the row directly gives the same max_tokens for every cost-map Claude id on every route. New tests drive the provider and unversioned rows on the Anthropic, Azure AI and Claude Platform routes
|
bugbot run |
|
bugbot run |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 0cb8559. Configure here.
mateo-berri
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the contribution!
…1_108_x fix(vertex_ai): backport #40376 versioned Claude max_tokens to lts/1.108.x
|
Thanks for the quick review @mateo-berri, glad it's in, cheers! |
Backport of BerriAI#40376 to lts/1.108.x. Applied from the PR's net diff at 3570922 and adapted to the line, which has no deployment max_output_tokens and no Bedrock Invoke get_config override. Co-authored-by: hktitof <hktitof@users.noreply.github.com>
Handwritten 1-3 sentence description
[insert handwritten description here]
Details
TLDR
Problem this solves:
DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS=4096, as the v1.109.0 notes suggest, the same ids stop at 4096 tokens instead, which is the cut-off the issue reportsHow it solves it:
@versionid then reads the row of its id without the version, e.g.claude-haiku-4-5vertex_ai/claude-3-5-haiku@20241022DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS, 128000 unless setmodel_info.max_output_tokensor a globalAnthropicConfig(max_tokens=...)still winsUser Flow
Before: a LibreChat user on a Vertex Claude Haiku 4.5 deployment gets a 400
model: vertex_ai/claude-haiku-4-5@20251001baseURL: https://litellm-domain/v1, picks that model, and sends a messagemax_tokensmax_tokens: 128000 > 64000, which is the maximum allowed number of output tokens for claude-haiku-4-5-20251001After: the same message gets the model's reply
Relevant issues
Fixes #40363
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/llms/vertex_ai/vertex_ai_partner_models/anthropic/test_vertex_ai_partner_models_anthropic_transformation.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Last updated: 0cb8559, a clean merge of main's d8e8621 so the required lint job finds a script main moved. QA ran on 6046f5b, and the LibreChat long-reply case with the env var at 4096 ran on 3570922. Changes since QA: 53d146b moved an explicit
DEFAULT_ANTHROPIC_CHAT_MAX_TOKENSahead of the new rows and 8fdc906 reverted it, then 3570922 reads the unversioned row straight from the cost map and adds route tests, 054e585 is empty, and 0cb8559 merges main. Proof remains valid because that read returns the same row without printing the provider banner a second time, and the merge leaves this PR's changed lines identical and changes nomax_tokensline or Claude cap. Verification: over 4585 Claude id and provider pairs, max_tokens is the same before and after 3570922 with the env var unset and at 4096, 7398 focused unit tests pass, and the 885 tests in the two changed test files pass at 0cb8559Live Vertex AI calls, no mocks. Two proxies with 2 workers each, one booted from the merge base 83c4706 and one from the PR tip 6046f5b, same config, no database. The reporter's client, LibreChat v0.8.8, talks to both through one custom endpoint per proxy and sends no
max_tokens. The OpenAI Python SDK cases and amax_tokensprobe through/utils/transform_requestrun against the same proxies@default@default, chat@defaultlengthstopSetup: config.yaml, proxy start, client
Env var names only:
VERTEXAI_PROJECT,GOOGLE_APPLICATION_CREDENTIALS,LITELLM_MASTER_KEY, plusLITELLM_LOCAL_MODEL_COST_MAP=TrueandPYTHON_DOTENV_DISABLED=1so each tree reads its own cost map and no.envqa-config.yamlboot_leg.sh <tree> <port>, run once per treeqa_client.pyrun_cases.sh <port>, which prints each command from the same string it runsSetup: LibreChat v0.8.8 with one custom endpoint per proxy
LibreChat runs from its release image
registry.librechat.ai/librechat-ai/librechat:v0.8.8withmongo:8.0.20, both on the host network. The env file setsMONGO_URI,CREDS_KEY,CREDS_IV,JWT_SECRET,JWT_REFRESH_SECRET,ALLOW_REGISTRATION=true,ALLOW_UNVERIFIED_EMAIL_LOGIN=true,ENDPOINTS=custom, andLITELLM_KEY(the proxy master key). A QA user registers throughPOST /api/auth/registerlibrechat.yaml, with nomax_tokensanywhere, as in the issuedrive_chat.pysigns in, opens a new chat on the named endpoint and model, sends one message, and screenshots the reply. It is the Playwright script used for every LibreChat casetransform_probe.pyasks each proxy'sPOST /utils/transform_requestwhichmax_tokensit would send to VertexBefore (83c4706)
LibreChat, Haiku 4.5 versioned
LiteLLM base 83c47061e1endpoint with modelvertex-claude-haiku-4-5-versioned, and send "Reply with the single word OK."After (6046f5b)
LibreChat, Haiku 4.5 versioned
LiteLLM head 6046f5bc9cendpoint with modelvertex-claude-haiku-4-5-versioned, and send "Reply with the single word OK."LibreChat, Opus 4.5 versioned
Before (83c4706)
After (6046f5b)
LibreChat, Sonnet 4.6 `@default` control
Before (83c4706)
After (6046f5b)
SDK, Haiku 4.5 versioned, chat
Before (83c4706)
After (6046f5b)
SDK, Haiku 4.5 versioned, stream
Before (83c4706)
After (6046f5b)
SDK, Haiku 4.5 versioned, /v1/responses
Before (83c4706)
After (6046f5b)
SDK, Opus 4.5 versioned, chat
Before (83c4706)
After (6046f5b)
SDK, Sonnet 4.6 `@default`, chat
Before (83c4706)
After (6046f5b)
SDK, Sonnet 4.6 bare id control, chat
Before (83c4706)
After (6046f5b)
SDK, Haiku 4.5 versioned, explicit max_tokens 50 control
Before (83c4706)
After (6046f5b)
max_tokens sent, Haiku 4.5 versioned
Before (83c4706)
After (6046f5b)
max_tokens sent, Sonnet 4.6 `@default`
Before (83c4706)
After (6046f5b)
Setup: env var and /live-pr-risk rig
lpr-config.yaml. The non-Vertex deployments carry placeholder credentials, since those routes only go through/utils/transform_requestlpr_boot.sh <tree> <port> [DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS], run once per treelpr_probe.py <base url> <key file> <out json> <model...>asksPOST /utils/transform_requestfor the body each raw model id would sendLibreChat, Haiku 4.5 versioned, long reply with `DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS=4096`
Both proxies booted with
DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS=4096 boot_leg.sh <tree> <port>, with LibreChat set up as above.drive_long_chat.pysends the long-story prompt, waits up to 10 minutes for the stream to end, and prints the reply's word count, its last line, and whether LibreChat offers Continuevertex-claude-haiku-4-5-versioned, and send "Write an original 6000-word short story about a lighthouse keeper. Keep writing until the story is complete."Before (83c4706)
After (3570922)
Haiku 4.5 versioned, long reply with `DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS=4096`
Both proxies booted with
lpr_boot.sh <tree> <port> 4096, the value the v1.109.0 notes give for keeping the old default. The request sends nomax_tokens. The base leg shows the cut-off the issue reportsBefore (83c4706)
After (6046f5b)
/live-pr-risk at 6046f5b
Verdict: no breaking change found. The run was at 6046f5b. 3570922 only changes how the unversioned row is read, and max_tokens is the same before and after it for every Claude id and provider in the cost map. Both proxies booted from the base and 6046f5b with 2 workers and
lpr-config.yaml, andlpr_probe.pysent each raw model id throughPOST /utils/transform_request, once withDEFAULT_ANTHROPIC_CHAT_MAX_TOKENSunset and once at 4096vertex_ai/claude-haiku-4-5@20251001vertex_ai/claude-3-5-haiku@20241022vertex_ai/claude-sonnet-4-6@defaultvertex_ai/claude-unlisted-model@20990101anthropic/claude-haiku-4-5anthropic/claude-unlisted-modelazure_ai/claude-haiku-4-5azure_ai/claude-opus-5-5-fastbedrock/invoke/us.anthropic.claude-haiku-4-5-20251001-v1:0bedrock/claude_platform/claude-haiku-4-5opencode_go/claude-haiku-5-5databricks/databricks-claude-haiku-4-5Every other request body key matched on all 12 routes in both runs
Breaking: none. With the env var unset only Vertex ids change, and Vertex rejects 128000 on each of them. Over all 44 Vertex Claude cost-map ids, 11 change, each from 128000 to its mapped cap
vertex_ai/claude-3-5-haikuvertex_ai/claude-3-5-haiku@20241022vertex_ai/claude-3-5-sonnetvertex_ai/claude-3-5-sonnet@20240620vertex_ai/claude-3-haikuvertex_ai/claude-3-haiku@20240307vertex_ai/claude-3-sonnetvertex_ai/claude-3-sonnet@20240229vertex_ai/claude-haiku-4-5@20251001vertex_ai/claude-opus-4-5@20251101vertex_ai/claude-sonnet-4-5@20250929Backward incompatible: none. With the env var set, an id whose model has a row now gets that row, as its unversioned id already did. At 4096 that is 64000 on versioned Haiku 4.5, and 128000 on the Azure AI names
claude-opus-5-5-fast,claude-fable-5-2-fastandclaude-mythos-5-2-fast, whose only row is theazure_ai/one. Azure AI names with a bare row already ignored the env var on the base, e.g.azure_ai/claude-haiku-4-5sends 64000 at 4096 on both sides. The env var stays the default for ids with no row, which is what the v1.109.0 notes describe. This run first flagged the 4096 case and 53d146b put the env value ahead of the rows, but that kept the issue's 4096 cut-off and the 400 at an explicit 128000, so 8fdc906 reverts itRegression risk: low. A deployment
model_info.max_output_tokensof 1000 still caps a real Haiku 4.5 reply at 1000 on both sides. The provider-row and@steps run on every Anthropic route, and tests now drive them on the Anthropic, Azure AI and Claude Platform routes as well as VertexDependency graph:
AnthropicConfig.get_configis the only caller ofget_max_tokens_for_model, and every route above goes through it (verified live by the sweep). Bedrock Invoke now calls the sharedmapped_max_output_tokenshelper (verified live, 64000 on both sides). Everytests/integrationClaude id resolves the same on both sides exceptvertex_ai/claude-3-haikuandvertex_ai/claude-haiku-4-5@20251001, and those Vertex suites sendmax_tokensthemselvesNot verified: real provider calls on the Anthropic, Azure AI, Bedrock, Claude Platform, OpenCode Go and Databricks routes, which ran transform-only with placeholder credentials
Observations
@defaultkeeps the 128000 default Vertex accepts/v1/messageswithout max_tokens answers 400 on both legsType
🐛 Bug Fix
Caveats (if any)
Decisions
DEFAULT_ANTHROPIC_CHAT_MAX_TOKENS, 128000 unless setclaude-sonnet-4-6andclaude-sonnet-4-6@defaultfrom 128000 to 64000, though Vertex accepts 128000 todayvertex_ai/claude-sonnet-4-6rows here, since a cost-map value change needs its own source@split runs on every route. Other ids with@, like the Bedrockanthropic.claude-haiku-4-5@20251001and Cloudflare@cf/...keys, never reach this step, so other routes see no changeclaude-opus-5-5-fast,claude-fable-5-2-fastandclaude-mythos-5-2-faston Azure AI now get theirazure_ai/row of 128000 instead of the env value, like their siblings. With it unset, no Azure AI name changesFinal Attestation
Bugbot summary