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(proxy): track unauthenticated pass-through requests in spend logs (#32410)

devin-ai-integration[bot]8936d07be887
diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.py
index b6342f4fa1a..b839426fcda 100644
--- a/litellm/proxy/hooks/proxy_track_cost_callback.py
+++ b/litellm/proxy/hooks/proxy_track_cost_callback.py
@@ -28,11 +28,20 @@ from litellm.proxy.spend_tracking.spend_tracking_utils import (
)
from litellm.proxy.utils import ProxyUpdateSpend
from litellm.types.utils import (
+ CallTypes,
StandardLoggingPayload,
StandardLoggingPayloadErrorInformation,
)
from litellm.utils import get_end_user_id_for_cost_tracking
+_PASS_THROUGH_CALL_TYPES: frozenset[str] = frozenset(
+ {
+ CallTypes.pass_through.value,
+ CallTypes.llm_passthrough_route.value,
+ CallTypes.allm_passthrough_route.value,
+ }
+)
+
class _ProxyDBLogger(CustomLogger):
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):
@@ -219,11 +228,13 @@ class _ProxyDBLogger(CustomLogger):
verbose_proxy_logger.debug(
f"user_api_key {user_api_key}, user_id {user_id}, team_id {team_id}, end_user_id {end_user_id}"
)
+ call_type: Optional[str] = kwargs.get("call_type")
if _should_track_cost_callback(
user_api_key=user_api_key,
user_id=user_id,
team_id=team_id,
end_user_id=end_user_id,
+ call_type=call_type,
):
## UPDATE DATABASE
await _update_database_and_spend_counters(
@@ -412,9 +423,15 @@ def _should_track_cost_callback(
user_id: Optional[str],
team_id: Optional[str],
end_user_id: Optional[str],
+ call_type: Optional[str] = None,
) -> bool:
"""
Determine if the cost callback should be tracked based on the kwargs
+
+ Pass-through endpoints can be configured with ``auth=false``, which leaves
+ the request with no key/user/team/end-user to attribute spend to. Those
+ requests still forward real provider traffic that operators expect to see
+ in request/usage logs, so they are tracked even when unauthenticated.
"""
# don't run track cost callback if user opted into disabling spend
@@ -423,7 +440,7 @@ def _should_track_cost_callback(
if user_api_key is not None or user_id is not None or team_id is not None or end_user_id is not None:
return True
- return False
+ return call_type in _PASS_THROUGH_CALL_TYPES
def _get_budget_reservation_from_metadata(metadata: dict) -> Optional[dict]:
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 813a0c5e38f..f289148101a 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
@@ -14,6 +14,7 @@ from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.hooks.proxy_track_cost_callback import (
_ProxyDBLogger,
_get_budget_reservation_from_metadata,
+ _should_track_cost_callback,
_update_database_and_spend_counters,
)
@@ -1177,3 +1178,88 @@ async def test_track_cost_callback_enriches_user_id_for_mcp_style_metadata():
kwargs["litellm_params"]["metadata"]["user_api_key_user_id"]
)
+
+
+ "call_type, expected",
+ [
+ ("pass_through_endpoint", True),
+ ("llm_passthrough_route", True),
+ ("allm_passthrough_route", True),
+ ("acompletion", False),
+ ("call_mcp_tool", False),
+ (None, False),
+ ],
+)
+def test_should_track_cost_callback_pass_through_without_owner(call_type, expected):
+ """Regression for LIT-3782: unauthenticated pass-through requests (auth=false)
+ carry no key/user/team/end-user, yet must still be tracked so they land in
+ LiteLLM_SpendLogs. Other call types with no owner stay untracked."""
+ assert (
+ _should_track_cost_callback(
+ user_api_key=None,
+ user_id=None,
+ team_id=None,
+ end_user_id=None,
+ call_type=call_type,
+ )
+ is expected
+ )
+
+
+ "call_type, expect_spend_log",
+ [
+ ("pass_through_endpoint", True),
+ ("acompletion", False),
+ (None, False),
+ ],
+)
+async def test_track_cost_callback_logs_unauthenticated_pass_through_request(
+ call_type, expect_spend_log
+):
+ """Regression for LIT-3782: a pass-through request with auth=false reaches the
+ cost callback with no key/user/team/end-user. Before the fix the spend-log
+ write was skipped and the request never appeared in request/usage logs. It
+ must now be written for pass-through call types while other unauthenticated
+ calls remain skipped."""
+ logger = _ProxyDBLogger()
+
+ kwargs = {
+ "call_type": call_type,
+ "model": "unknown",
+ "litellm_params": {"metadata": {}},
+ "standard_logging_object": {
+ "response_cost": 0.0,
+ "request_tags": None,
+ },
+ "stream": False,
+ }
+
+ with (
+ patch(
+ "litellm.proxy.proxy_server.increment_spend_counters",
+ new_callable=AsyncMock,
+ ),
+ patch(
+ "litellm.proxy.proxy_server.update_cache",
+ new_callable=AsyncMock,
+ ),
+ patch(
+ "litellm.proxy.proxy_server.proxy_logging_obj",
+ ) as mock_proxy_logging,
+ ):
+ mock_proxy_logging.db_spend_update_writer.update_database = AsyncMock()
+ mock_proxy_logging.slack_alerting_instance.customer_spend_alert = AsyncMock()
+
+ await logger._PROXY_track_cost_callback(
+ kwargs=kwargs,
+ completion_response=None,
+ start_time=datetime.now(),
+ end_time=datetime.now(),
+ )
+
+ assert mock_proxy_logging.db_spend_update_writer.update_database.await_count == (
+ 1 if expect_spend_log else 0
+ )