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]

Merge remote-tracking branch 'origin' into litellm_user_promethus_metrics

yuneng-jiang1e314ce2033c
diff --cc tests/test_litellm/proxy/auth/test_user_api_key_auth.py
index 46a7b0ff435,5f49db66089..1f379f4371e
--- a/tests/test_litellm/proxy/auth/test_user_api_key_auth.py
+++ b/tests/test_litellm/proxy/auth/test_user_api_key_auth.py
@@@ -348,59 -359,3 +359,60 @@@ async def test_proxy_admin_expired_key_
# Clean up - restore original values if needed
pass
+
++
+async def test_return_user_api_key_auth_obj_user_spend_and_budget():
+ """
+ Test that _return_user_api_key_auth_obj correctly sets user_spend and user_max_budget
+ from user_obj attributes.
+ """
+ from datetime import datetime
+
+ from litellm.proxy._types import UserAPIKeyAuth
+ from litellm.proxy.auth.user_api_key_auth import _return_user_api_key_auth_obj
+
+ user_obj = type(
+ "LiteLLM_UserTable",
+ (),
+ {
+ "tpm_limit": 1000,
+ "rpm_limit": 100,
+ "user_email": "[email protected]",
+ "spend": 250.0,
+ "max_budget": 1000.0,
+ "user_role": "internal_user",
+ },
+ )
+
+ api_key = "sk-test-key"
+ valid_token_dict = {
+ "user_id": "test-user",
+ "org_id": "test-org",
+ }
+ route = "/chat/completions"
+ start_time = datetime.now()
+
+ mock_service_logger = MagicMock()
+ mock_service_logger.async_service_success_hook = AsyncMock()
+
+ with patch(
+ "litellm.proxy.auth.user_api_key_auth.user_api_key_service_logger_obj",
+ new=mock_service_logger,
+ ):
+ result = await _return_user_api_key_auth_obj(
+ user_obj=user_obj,
+ api_key=api_key,
+ parent_otel_span=None,
+ valid_token_dict=valid_token_dict,
+ route=route,
+ start_time=start_time,
+ user_role=None,
+ )
+
+ assert isinstance(result, UserAPIKeyAuth)
+ assert result.user_spend == 250.0
+ assert result.user_max_budget == 1000.0
+ assert result.user_tpm_limit == 1000
+ assert result.user_rpm_limit == 100
+ assert result.user_email == "[email protected]"