The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(interactions): stop the unpollable-create path from firing a false cost-tracking alert
Cursor Agent0ab8ef60bf86
diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.pyindex 46cf62ede1c..70677521d6d 100644--- a/litellm/proxy/hooks/proxy_track_cost_callback.py+++ b/litellm/proxy/hooks/proxy_track_cost_callback.py@@ -319,8 +319,10 @@ class _ProxyDBLogger(CustomLogger):elif budget_reservation is not None:await _release_budget_reservation(budget_reservation=budget_reservation)else:- if _is_unbilled_in_progress_interaction(completion_response):- if BACKGROUND_INTERACTION_COST_POLLING_ENABLED:+ if _is_unbilled_interaction_response(completion_response):+ if BACKGROUND_INTERACTION_COST_POLLING_ENABLED and _is_unbilled_in_progress_interaction(+ completion_response+ ):verbose_proxy_logger.debug("Cost tracking deferred for in-progress background interaction; ""the budget reservation stays open until the poll task logs the final usage"@@ -328,8 +330,8 @@ class _ProxyDBLogger(CustomLogger):returnawait _release_budget_reservation(budget_reservation=budget_reservation)verbose_proxy_logger.debug(- "Background interaction cost polling is disabled; released the budget "- "reservation for an in-progress interaction that will not be billed"+ "Released the budget reservation for an interaction create with no usage "+ "that no poll task will settle")returnawait _release_budget_reservation(budget_reservation=budget_reservation)@@ -477,6 +479,12 @@ def _write_spend_metadata_to_kwargs(kwargs: dict, metadata: dict) -> None:bucket[key] = value+def _is_unbilled_interaction_response(completion_response: object) -> bool:+ from litellm.types.interactions import InteractionsAPIResponse++ return isinstance(completion_response, InteractionsAPIResponse) and completion_response.usage is None++def _is_unbilled_in_progress_interaction(completion_response: object) -> bool:from litellm.interactions.background_cost_polling import is_pollable_background_interactionfrom litellm.types.interactions import InteractionsAPIResponsediff --git a/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py b/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.pyindex 93e0cbec596..fd79f9ea3b3 100644--- a/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py+++ b/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py@@ -871,6 +871,10 @@ async def test_track_cost_callback_releases_reservation_for_unpollable_interacticallback must release it there and then, or the pre-call estimate staysadded to the key, user, team and org spend counters and starts refusingtraffic against budget that was never actually spent.++ A no-usage terminal create is also not a cost-tracking failure, so the+ callback must not fire ``failed_tracking_alert``: doing so would flood+ operators with false alerts and mask real cost-tracking failures."""from litellm.types.interactions import InteractionsAPIResponse@@ -897,6 +901,7 @@ async def test_track_cost_callback_releases_reservation_for_unpollable_interacti)assert reservation["finalized"] is True+ mock_proxy_logging.failed_tracking_alert.assert_not_called()@pytest.mark.asyncio@@ -904,7 +909,8 @@ async def test_track_cost_callback_releases_reservation_for_interaction_without_"""The scheduler also refuses a response with no id, since it has nothing topoll for, so the callback must not defer to a poll task that will never- exist.+ exist, and it must not fire ``failed_tracking_alert`` for what is a+ legitimate no-usage response rather than a cost-tracking failure."""from litellm.types.interactions import InteractionsAPIResponse@@ -931,6 +937,7 @@ async def test_track_cost_callback_releases_reservation_for_interaction_without_)assert reservation["finalized"] is True+ mock_proxy_logging.failed_tracking_alert.assert_not_called()@pytest.mark.parametrize(