Repository navigation
fix(utils): resolve catalog max_input_tokens in trim_messages for prefixed models - #50645
Open
princejain756 wants to merge 1 commit into
Open
princejain756 wants to merge 1 commit into
princejain756 wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Handwritten 1-3 sentence description
Calling
trim_messageswith a provider-prefixed model name likeopenai/gpt-4ooranthropic/claude-haiku-4-5now trims the conversation to fit the model's context window instead of returning the over-limit messages unchanged. When the exact model string is not a top-level key inlitellm.model_cost,trim_messagesfalls back to the local catalog lookup so prefixed models whose cost map entries are keyed by the bare model name still resolve their input token limit.Details
TLDR
Problem this solves:
trim_messagesonly checkedmodel in litellm.model_costby exact keyopenai/gpt-4oandanthropic/claude-haiku-4-5missed the bare-keyed cost map rows and skipped trimmingHow it solves it:
model in litellm.model_costlookup firstlookup_catalog_model_info(model, None)when the exact key misses andmax_tokensis unsetmax_input_tokensUser Flow
Before: a Python SDK caller trimming messages for
openai/gpt-4ooranthropic/claude-haiku-4-5gets the untrimmed list backtrim_messages(messages, model="openai/gpt-4o")on a 301,412-token message listtrim_messagesreturns all 301,412 tokens untouched instead of trimming to 75% of the 128,000-token context windowlitellm.completionsends an over-limit request to the providerAfter: the same call trims the conversation to ~75% of the model's context window
trim_messages(messages, model="openai/gpt-4o")on the same 301,412-token message listtrim_messagesresolves the 128,000-token context window from the catalog and trims the messages down to 95,928 tokensRelevant issues
Fixes #50639
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.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
Shared reproduction command (from #50639, no API key required):
Before (abee9c9)
abee9c945ba652f9c971eca8eabfca2600fdbfd1openai/gpt-4oandanthropic/claude-haiku-4-5return all 301,412 tokens unchanged:After (a0c6114)
a0c61146c0e8b78da88f832c5f7bea795833463euv run pytest tests/unit/test_utils.py -k "trim" -qpasses (18 passed):Type
🐛 Bug Fix
Final Attestation