Skip to content

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]

fix(interactions): keep alerting when an interaction that produced output has no usage

mateo-berri521dc973c302
diff --git a/litellm/interactions/background_cost_polling.py b/litellm/interactions/background_cost_polling.py
index 5d7dca80b73..279d2d6b32e 100644
--- a/litellm/interactions/background_cost_polling.py
+++ b/litellm/interactions/background_cost_polling.py
@@ -49,6 +49,8 @@ if TYPE_CHECKING:
_TERMINAL_STATUSES = frozenset({"completed", "failed", "cancelled", "incomplete", "budget_exceeded", "requires_action"})
+_STATUSES_THAT_PRODUCED_OUTPUT = frozenset({"completed", "requires_action"})
+
@dataclass(frozen=True, slots=True)
class BackgroundInteractionPollContext:
@@ -195,6 +197,21 @@ def is_pollable_background_interaction(response: InteractionsAPIResponse) -> boo
return response.status == "in_progress" and bool(response.id)
+def missing_usage_is_expected(response: InteractionsAPIResponse) -> bool:
+ """
+ Whether a response arriving with no usage block is a normal outcome rather
+ than lost billing data. An interaction that is still running, or that
+ stopped at ``failed``, ``cancelled``, ``incomplete`` or ``budget_exceeded``,
+ has nothing to charge for and should not raise a cost-tracking alarm.
+
+ ``completed`` and ``requires_action`` both mean the model produced output,
+ so a usage block is always expected with them. If one arrives without it
+ the charge for real work has been lost, which is precisely what the
+ proxy's cost-tracking alert exists to surface.
+ """
+ return response.status not in _STATUSES_THAT_PRODUCED_OUTPUT
+
+
@dataclass(frozen=True, slots=True)
class _ActiveBackgroundPoll:
task: "asyncio.Task[None]"
diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.py
index 70677521d6d..6abfca1d3a0 100644
--- a/litellm/proxy/hooks/proxy_track_cost_callback.py
+++ b/litellm/proxy/hooks/proxy_track_cost_callback.py
@@ -480,9 +480,12 @@ def _write_spend_metadata_to_kwargs(kwargs: dict, metadata: dict) -> None:
def _is_unbilled_interaction_response(completion_response: object) -> bool:
+ from litellm.interactions.background_cost_polling import missing_usage_is_expected
from litellm.types.interactions import InteractionsAPIResponse
- return isinstance(completion_response, InteractionsAPIResponse) and completion_response.usage is None
+ if not isinstance(completion_response, InteractionsAPIResponse):
+ return False
+ return completion_response.usage is None and missing_usage_is_expected(completion_response)
def _is_unbilled_in_progress_interaction(completion_response: object) -> bool:
diff --git a/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py b/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py
index fd79f9ea3b3..d840611fc7b 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
@@ -862,7 +862,7 @@ async def test_track_cost_callback_releases_reservation_for_in_progress_interact
@pytest.mark.asyncio
@pytest.mark.parametrize(
"status",
- ["completed", "failed", "cancelled", "incomplete", "requires_action", "budget_exceeded"],
+ ["failed", "cancelled", "incomplete", "budget_exceeded"],
)
async def test_track_cost_callback_releases_reservation_for_unpollable_interaction(status):
"""
@@ -872,9 +872,10 @@ async def test_track_cost_callback_releases_reservation_for_unpollable_interacti
added to the key, user, team and org spend counters and starts refusing
traffic 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.
+ None of these statuses produced output, so their missing usage is a normal
+ outcome rather than a cost-tracking failure, and 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
@@ -904,6 +905,47 @@ async def test_track_cost_callback_releases_reservation_for_unpollable_interacti
mock_proxy_logging.failed_tracking_alert.assert_not_called()
[email protected]("status", ["completed", "requires_action"])
+async def test_track_cost_callback_alerts_when_an_interaction_that_produced_output_has_no_usage(status):
+ """
+ ``completed`` and ``requires_action`` both mean the model produced output,
+ so a usage block is always expected with them. One arriving without it
+ means the charge for real work was lost, which is exactly what the
+ cost-tracking alert is for: silencing it here would let an operator's
+ interactions bill nothing with no signal that anything went wrong.
+
+ The reservation still has to be released, since suppressing the alert was
+ never what freed it.
+ """
+ from litellm.types.interactions import InteractionsAPIResponse
+
+ logger = _ProxyDBLogger()
+ reservation = {"reserved_cost": 0.05, "entries": [], "finalized": False}
+ usageless_response = InteractionsAPIResponse(
+ id="interactions/bg-abc",
+ model="gemini-3-flash-preview",
+ status=status,
+ )
+
+ with patch(
+ "litellm.proxy.proxy_server.proxy_logging_obj",
+ ) as mock_proxy_logging:
+ mock_proxy_logging.failed_tracking_alert = AsyncMock()
+ mock_proxy_logging.db_spend_update_writer = MagicMock()
+ mock_proxy_logging.db_spend_update_writer.update_database = AsyncMock()
+
+ await logger._PROXY_track_cost_callback(
+ kwargs=_in_progress_interaction_kwargs(reservation),
+ completion_response=usageless_response,
+ start_time=datetime.now(),
+ end_time=datetime.now(),
+ )
+
+ assert reservation["finalized"] is True
+ mock_proxy_logging.failed_tracking_alert.assert_called_once()
+
+
@pytest.mark.asyncio
async def test_track_cost_callback_releases_reservation_for_interaction_without_an_id():
"""