:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(llm-gateway): refresh provider sets when model_cost reloads (#60634)
Richard Solomouf6ef9a374cdf
diff --git a/services/llm-gateway/src/llm_gateway/rate_limiting/cost_refresh.py b/services/llm-gateway/src/llm_gateway/rate_limiting/cost_refresh.pyindex 83c4372e743c..43a906c926db 100644--- a/services/llm-gateway/src/llm_gateway/rate_limiting/cost_refresh.py+++ b/services/llm-gateway/src/llm_gateway/rate_limiting/cost_refresh.py@@ -39,6 +39,9 @@ class CostRefreshService:try:model_cost = get_model_cost_map(url=model_cost_map_url)litellm.model_cost = model_cost+ # Provider sets (anthropic_models, etc.) back get_llm_provider's inference+ # for bare names — without re-running this they stay frozen at import time.+ litellm.add_known_models(model_cost)self._last_refresh = time.monotonic()logger.info("model_cost_refreshed", model_count=len(model_cost))except Exception:diff --git a/services/llm-gateway/src/llm_gateway/rate_limiting/model_cost_service.py b/services/llm-gateway/src/llm_gateway/rate_limiting/model_cost_service.pyindex 90126c984a51..4eeaad6c813f 100644--- a/services/llm-gateway/src/llm_gateway/rate_limiting/model_cost_service.py+++ b/services/llm-gateway/src/llm_gateway/rate_limiting/model_cost_service.py@@ -74,6 +74,8 @@ class ModelCostService:try:model_cost = get_model_cost_map(url=model_cost_map_url)litellm.model_cost = model_cost+ # Keep provider sets in sync — see cost_refresh.py.+ litellm.add_known_models(model_cost)self._costs = cast(dict[str, ModelCost], model_cost)new_limits: dict[str, ModelLimits] = {}for model, cost in self._costs.items():diff --git a/services/llm-gateway/tests/test_cost_refresh.py b/services/llm-gateway/tests/test_cost_refresh.pyindex 261522dc2cf0..8511d7618abc 100644--- a/services/llm-gateway/tests/test_cost_refresh.py+++ b/services/llm-gateway/tests/test_cost_refresh.py@@ -3,9 +3,29 @@ from __future__ import annotationsfrom collections.abc import Iteratorfrom unittest.mock import MagicMock, patch+import litellmimport pytestfrom llm_gateway.rate_limiting.cost_refresh import CostRefreshService+from llm_gateway.rate_limiting.model_cost_service import ModelCostService++[email protected](autouse=True)+def restore_litellm_globals() -> Iterator[None]:+ # refresh() mutates litellm globals — snapshot and restore so tests don't leak.+ snapshot = (+ litellm.model_cost,+ litellm.anthropic_models.copy(),+ litellm.open_ai_chat_completion_models.copy(),+ )+ try:+ yield+ finally:+ litellm.model_cost, anthropic, openai = snapshot+ litellm.anthropic_models.clear()+ litellm.anthropic_models.update(anthropic)+ litellm.open_ai_chat_completion_models.clear()+ litellm.open_ai_chat_completion_models.update(openai)class TestCostRefreshService:@@ -28,20 +48,34 @@ class TestCostRefreshService:@patch("llm_gateway.rate_limiting.cost_refresh.get_model_cost_map")def test_refresh_updates_litellm_model_cost(self, mock_get_cost_map: MagicMock) -> None:- import litellm-mock_costs = {"gpt-4": {"input_cost_per_token": 0.00003, "output_cost_per_token": 0.00006},"claude-3-opus": {"input_cost_per_token": 0.000015, "output_cost_per_token": 0.000075},}mock_get_cost_map.return_value = mock_costs- service = CostRefreshService.get_instance()- service.refresh()+ CostRefreshService.get_instance().refresh()assert litellm.model_cost == mock_costsmock_get_cost_map.assert_called_once()+ @patch("llm_gateway.rate_limiting.cost_refresh.litellm.add_known_models")+ @patch("llm_gateway.rate_limiting.cost_refresh.get_model_cost_map")+ def test_refresh_registers_provider_models(+ self,+ mock_get_cost_map: MagicMock,+ mock_add_known_models: MagicMock,+ ) -> None:+ # The provider sets (anthropic_models, etc.) back get_llm_provider's inference,+ # so we must re-register them on every refresh — otherwise the sets stay frozen+ # at import time and freshly added models raise "LLM Provider NOT provided".+ mock_costs = {"some-model": {"litellm_provider": "anthropic"}}+ mock_get_cost_map.return_value = mock_costs++ CostRefreshService.get_instance().refresh()++ mock_add_known_models.assert_called_once_with(mock_costs)+@patch("llm_gateway.rate_limiting.cost_refresh.get_model_cost_map")def test_ensure_fresh_skips_refresh_within_ttl(self, mock_get_cost_map: MagicMock) -> None:mock_get_cost_map.return_value = {}@@ -83,3 +117,27 @@ class TestCostRefreshService:service.ensure_fresh()assert service._last_refresh > 0+++class TestModelCostServiceRefresh:+ @pytest.fixture(autouse=True)+ def reset_singleton(self) -> Iterator[None]:+ ModelCostService.reset_instance()+ yield+ ModelCostService.reset_instance()++ @patch("llm_gateway.rate_limiting.model_cost_service.litellm.add_known_models")+ @patch("llm_gateway.rate_limiting.model_cost_service.get_model_cost_map")+ def test_refresh_registers_provider_models(+ self,+ mock_get_cost_map: MagicMock,+ mock_add_known_models: MagicMock,+ ) -> None:+ # Same contract as CostRefreshService — _refresh_cache must keep the+ # provider sets in sync with the freshly fetched cost map.+ mock_costs = {"some-model": {"litellm_provider": "anthropic"}}+ mock_get_cost_map.return_value = mock_costs++ ModelCostService.get_instance()._refresh_cache()++ mock_add_known_models.assert_called_once_with(mock_costs)