fix(llm-gateway): refresh provider sets when model_cost reloads#60634
Merged
richardsolomou merged 3 commits intoMay 29, 2026
Conversation
litellm.get_llm_provider resolves bare model names against provider sets (anthropic_models, open_ai_chat_completion_models, …), which are populated only at import time. Refreshing litellm.model_cost without re-running add_known_models leaves those sets stale, so models added upstream after process start (e.g. claude-opus-4-8) raise "LLM Provider NOT provided". Generated-By: PostHog Code Task-Id: 35ad8683-ced4-4a98-9721-fa6606b6dca0
k11kirky
approved these changes
May 29, 2026
Contributor
Prompt To Fix All With AIFix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
services/llm-gateway/tests/test_cost_refresh.py:46-73
**Missing test coverage for `ModelCostService._refresh_cache`**
The regression test verifies `add_known_models` is called via `CostRefreshService.refresh()`, but the same change was made to `ModelCostService._refresh_cache()` with no corresponding test. If `ModelCostService` follows a different code path (e.g., its `_refresh_cache` silently swallows an error before or after `add_known_models`), there is no test to catch the regression.
Reviews (1): Last reviewed commit: "fix(llm-gateway): refresh provider sets ..." | Re-trigger Greptile |
Address PR review: - Add TestModelCostServiceRefresh covering the second refresh path. - Snapshot/restore litellm.model_cost and provider sets in an autouse fixture so tests don't leak state into siblings. Generated-By: PostHog Code Task-Id: 35ad8683-ced4-4a98-9721-fa6606b6dca0
Previous regression test asserted on the post-state of litellm's provider sets, which is sensitive to test ordering in CI (other tests had already populated the same global). Mock add_known_models and assert it's invoked with the freshly fetched map — that's the contract our change makes. Generated-By: PostHog Code Task-Id: 35ad8683-ced4-4a98-9721-fa6606b6dca0
mayteio
pushed a commit
that referenced
this pull request
May 29, 2026
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.
Problem
LiteLLM's
get_llm_providerresolves bare model names against module-level sets (anthropic_models,open_ai_chat_completion_models, etc.) populated only at import time. Refreshinglitellm.model_costdoesn't touch them, so models added upstream after process start (e.g.claude-opus-4-8added 7h ago) raiseLLM Provider NOT providedon the gateway until a redeploy.Changes
Call
litellm.add_known_models(model_cost)after everylitellm.model_cost = …reassignment inCostRefreshService.refreshandModelCostService._refresh_cache. This is what LiteLLM itself does at import.How did you test this code?
Agent-authored. Ran
uv run pytest tests/test_cost_refresh.pyand the broadertest_model_registry.py/test_models_api.py/callbacks/test_rate_limiting.pysuite (80 tests) — all green. New regression test asserts a freshly fetched model lands in bothlitellm.anthropic_modelsandlitellm.open_ai_chat_completion_models.Automatic notifications
Docs update
N/A
🤖 Agent context
Authored by PostHog Code. Diagnosis path: traced the gateway's 400 from
litellm.anthropic_messagesforclaude-opus-4-8toget_llm_provider_logic.py:395, which checkslitellm.anthropic_modelsrather thanlitellm.model_cost. Confirmed the v1.83.7-stable bundledmodel_prices_and_context_window_backup.jsonlacks bothclaude-opus-4-7andclaude-opus-4-8(the live import-time fetch is what seeds known-good models today), and that neither refresh path calledadd_known_modelsafter reassignment.Considered prefixing
anthropic/on the model in_handle_anthropic_messages(mirroring thebedrock/prefix already used). Rejected: only patches the Anthropic route; the same staleness affects any provider whose models land in the upstream JSON after startup. Fixing at the refresh boundary covers every provider.Aside for a future PR:
CostRefreshServiceandModelCostServiceare two singletons racing on the samelitellm.model_costglobal — worth consolidating, out of scope here.Created with PostHog Code