Repository navigation
refactor: drop MAX_TOKENS entries that exactly duplicate LiteLLM - #3220
Conversation
…ests - Cache key is now 'related_tickets_<md5(pr_url)>' instead of global 'related_tickets', preventing stale tickets leaking between unrelated PRs - Added cache_tickets=true config flag under [pr_reviewer] to allow disabling - Fixed Dynaconf merge_enabled=True list append bug in both production code (env_bridge.py uses case-insensitive pop before set) and test helpers (restore_settings now calls _remove_key before every set) - Added azure-identity dependency for Azure DevOps provider availability - 1043 tests pass, 0 failures
…aks in tests" This reverts commit 7f0d154.
The hardcoded MAX_TOKENS registry contained 181 entries reporting the same value litellm.get_model_info() already returns, and get_max_tokens() falls back to LiteLLM at step 4. Remove the 89 static literal duplicates (the behavioral value is identical via the fallback); the remaining generated Claude entries are retained because they also drive the no-temperature and extended-thinking registries, and their 1M-context handling is a separate, deliberate judgement. Values verified with litellm.get_model_info() for every removed model (0 mismatches). Add a regression test asserting no static entry duplicates LiteLLM, and a comment explaining why the registry stays lean. Fixes The-PR-Agent#3196 (step 1).
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Thanks for the duplicate audit and for shipping a guard test alongside it. The counts check out: 89 rows, all exact LiteLLM duplicates, and no understated row touched.
Two things inline. The first is a blocker for this shape of the change.
| 'gpt-4-32k': 32000, | ||
| 'gpt-4-1106-preview': 128000, # 128K, but may be limited by config.max_model_tokens | ||
| 'gpt-4-0125-preview': 128000, # 128K, but may be limited by config.max_model_tokens | ||
| 'gpt-4o': 128000, # 128K, but may be limited by config.max_model_tokens |
There was a problem hiding this comment.
This row is the clearest case of the problem. Three sites read this registry directly to bypass max_model_tokens, with a comment saying so: pr_help_message.py:139, pr_help_docs.py:471, and the same block in pr_code_suggestions.py. Each falls through to get_max_tokens when a model is absent, and that clamps to the shipped 32000. So gpt-4o drops from 128000 to 32000 at those sites and gpt-4.1 from 1047576 to 32000. 88 of the 89 rows behave that way, command-nightly being the only exception because 4096 is already under the clamp.
It is inconsistent as well: gemini/gemini-3.5-pro keeps 1048576 while gemini/gemini-3.8-flash gets 32000 in the same code path. And against LiteLLM's bundled map, the backup it falls back to when the import-time fetch fails, five of the deleted models raise rather than resolve, including both openrouter grok rows.
| assert claude_thinking.isdisjoint(claude_no_temp) | ||
|
|
||
|
|
||
| class TestNoLiteLLMDuplicates: |
There was a problem hiding this comment.
Your sequencing note is right, but the mechanism is worth spelling out, because this new test is the thing that breaks. #2998 adds four rows that duplicate LiteLLM exactly, two of them (gemini/gemini-3.8-flash and vertex_ai/gemini-3.8-flash) rows you delete here, and #2890 adds two more. Whichever of you lands second fails this assertion.
One wrinkle in it: the except Exception: continue means both gemini-3.8-flash rows are skipped silently when LiteLLM falls back to its bundled map, so #2998 can pass this guard on an air-gapped run and still fail it in CI.
|
The blocker is resolved. The three direct registry readers kept their unreduced context. pr_help_message, pr_help_docs and pr_code_suggestions each read MAX_TOKENS directly to bypass the config.max_model_tokens clamp. Their old fallback to get_max_tokens clamped everything to the shipped 32000, so gpt-4o would have dropped from 128000 to 32000 and gpt-4.1 from 1047576 to 32000. get_max_tokens now takes ignore_max_model_tokens=True to skip that clamp, and all three sites route through it. The same raw value comes back whether the model stayed in the registry or is resolved from LiteLLM, so the deletions are now behaviour preserving at those sites and the gemini inconsistency is gone. A regression test locks gpt-4o, gpt-4.1 and gemini/gemini-3.8-flash to the unreduced value with the flag while the default path still clamps. On the guard test wrinkle and sequencing, I acknowledged the point in the description: the except continue can mask the bundled map on an air gapped run, and whichever of #2998, #2890 or this lands second has to reconcile with the assertion. Full unit suite: 4366 passed, 1 skipped, 1 xfailed. Ruff and pre commit are clean on the five touched files. |
|
Thanks for the Merged onto main with Restoring those five rows fixes five, measured. The sixth is your new Three edits get it green under both maps: restore the five rows, exempt them in |
Five of the models dropped by the dedup are not carried by LiteLLM's bundled backup cost map (used when the import-time fetch fails and under LITELLM_LOCAL_MODEL_COST_MAP=true): gemini/gemini-3.8-flash, vertex_ai/gemini-3.8-flash, openrouter/x-ai/grok-4.5, openrouter/x-ai/grok-4.6 and xai/grok-build-latest. With the registry entry gone, get_max_tokens() raises the 'Ensure X is defined in MAX_TOKENS' error for them on air-gapped runs instead of returning a value, so the dedup was not behavior preserving. Restore those five rows, exempt them from TestNoLiteLLMDuplicates so the guard cannot flag the deliberate pins, and drop the gemini-3.8-flash case from the ignore_max_model_tokens parametrize list since the bundled map cannot resolve it. Verified under both maps: full unit suite 4365 passed, 1 skipped, 1 xfailed.
|
The blocker is resolved with the three edits you outlined. Restored the five rows that LiteLLM's bundled backup cost map does not carry, verified the same way you measured it (
Exempted them in Dropped the Verified under both maps: |
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Thanks for restoring the five and for tagging each with why it stays.
Approving. I checked all five against LiteLLM's bundled map with LITELLM_LOCAL_MODEL_COST_MAP=true and none of them are in it, so pinning is right. 84 rows go, which is your 89 minus these five, and the merge with today's main is 7425 passed against 7422.
One sequencing note, not an ask on this PR: #2998 is still open and adds gemini/gemini-3.1-flash-lite and vertex_ai/gemini-3.1-flash-lite at 1048576, which is exactly what the bundled map carries, so your guard test will fail it. I will raise that there rather than hold this.
Fixes step 1 of #3196 (step 2, the 25 understated entries, stays in the issue to avoid mixing the behaviour preserving removal with value changes).
First PR of the suggested two-step plan: delete the 181
MAX_TOKENSentries that are byte identical to what LiteLLM reports. This is behaviour preserving by construction, becauseget_max_tokens()already falls back tolitellm.get_model_info()(step 4 of its documented lookup order) for anything not in the registry.Scope and counts
A static read of the dict literal understates the runtime count, because
**_claude_tokensatpr_agent/algo/__init__.py:278expands another 106 entries in. On this branch the runtime total is 252, down from 341 on main:_CLAUDE_MODEL_FAMILIES(bedrock cross region, vertex, anthropic expansions). They are not deleted here: the same generator declarations feedNO_SUPPORT_TEMPERATURE_MODELSandCLAUDE_EXTENDED_THINKING_MODELS, and the deliberate 1M context handling is the part of the issue that needs separate judgement.get_max_tokens()path to resolve to the identical value via the LiteLLM fallback, with 0 mismatches.pr_similar_issue.pyonly indexes a kept entry.Direct registry readers keep their unreduced context
Three sites read
MAX_TOKENSdirectly to bypass theconfig.max_model_tokensclamp (pr_help_message.py,pr_help_docs.py,pr_code_suggestions.py). With the duplicates removed, their old fallback toget_max_tokens()would have clamped them all to the shipped 32000: gpt-4o would drop from 128000 to 32000 and gpt-4.1 from 1047576 to 32000.get_max_tokens()now acceptsignore_max_model_tokens=Trueto skip that clamp, and all three sites route through it, so they keep resolving the raw model context whether the model is in the registry or resolved from LiteLLM. A regression test locks gpt-4o, gpt-4.1 andgemini/gemini-3.8-flashto return the unreduced value with the flag while the default path still clamps.What happens when the LiteLLM fallback fails or drifts
This is not purely deduplication: the 89 models move from a value pinned in our tree to one resolved at runtime.
litellm.model_cost, which backslitellm.get_model_info(), is network fetched at import from the litellm repository (with a bundled backup used if that fetch fails, andLITELLM_LOCAL_MODEL_COST_MAPto force the local copy).get_max_tokens()catches a non resolving lookup and continues to the next candidate, and ultimately raises the existing "Ensure X is defined in MAX_TOKENS" error. That is the same behaviour as for any model never in the registry, so an air gapped host may now raise for one of these models where main would return a stale static value.The trade-off is accepted because the static values were already identical to one point in time snapshot of litellm's map, so reproducing them needed the very dependency being audited in #3192.
Regression guard
TestNoLiteLLMDuplicatesasserts that no static registry entry (excluding the generator expanded Claude families, which are exempt for the reasons above) exactly duplicates LiteLLM, so the dead entries cannot regrow.Sequencing
#2998 and #2890 are both open and both add entries to this dict, so this lands after them or rebases around them. Whichever lands second has to reconcile with the guard test. The diff touches
pr_agent/algo/__init__.py,pr_agent/algo/utils.py, the three direct readers, and the tests.Verification
Full unit suite passes (4366 passed, 1 skipped, 1 xfailed); ruff and pre-commit clean on the five touched files.