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
Merge branch 'main' into litellm_budget_tier_enforcement_for_keys
Shivam Rawatc45a17bad4c3
diff --cc tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.pyindex 21be2ad69e8,b3f7b211951..65575167940--- a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py+++ b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py@@@ -5561,131 -5608,59 +5608,189 @@@ async def test_validate_key_list_check_assert "Key Hash not found" in exc_info.value.message+async def test_key_with_budget_id_does_not_store_budget_duration():+ """+ Test that when a key is created with budget_id but without explicit+ budget_duration, the key does NOT get budget_duration stored on it.++ Keys with budget_id follow their linked budget tier's reset schedule;+ reset_budget_for_keys_linked_to_budgets() resets them when the tier resets.+ This avoids duplicating budget_duration on keys so tier updates apply+ automatically to all linked keys.+ """+ from unittest.mock import AsyncMock, MagicMock, patch++ mock_prisma = MagicMock()++ mock_generate_key = AsyncMock(+ return_value={+ "key": "sk-test-key",+ "expires": None,+ "user_id": "test-user",+ "team_id": None,+ }+ )++ with patch(+ "litellm.proxy.proxy_server.prisma_client", mock_prisma+ ), patch(+ "litellm.proxy.proxy_server.llm_router", None+ ), patch(+ "litellm.proxy.proxy_server.premium_user", False+ ), patch(+ "litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"+ ), patch(+ "litellm.proxy.management_endpoints.key_management_endpoints.generate_key_helper_fn",+ mock_generate_key,+ ):+ await _common_key_generation_helper(+ data=GenerateKeyRequest(+ budget_id="7d-budget-tier",+ max_budget=10.0,+ # NOTE: budget_duration is intentionally NOT set here+ ),+ user_api_key_dict=UserAPIKeyAuth(+ user_role=LitellmUserRoles.PROXY_ADMIN,+ api_key="sk-1234",+ user_id="admin-user",+ ),+ litellm_changed_by=None,+ team_table=None,+ )++ mock_generate_key.assert_awaited_once()+ call_kwargs = mock_generate_key.call_args.kwargs++ # Key should NOT have key_budget_duration - it follows the budget tier's schedule+ assert call_kwargs.get("key_budget_duration") is None, (+ "key_budget_duration should be None for budget-linked keys without explicit "+ f"budget_duration; got: {call_kwargs.get('key_budget_duration')}"+ )++ # No budget tier lookup - we don't copy budget_duration onto the key+ mock_prisma.db.litellm_budgettable.find_unique.assert_not_called()+++async def test_key_does_not_override_explicit_budget_duration():+ """+ Test that when a key is created with both budget_id and an explicit+ budget_duration, the explicit budget_duration takes precedence over+ the budget tier's budget_duration.+ """+ from unittest.mock import AsyncMock, MagicMock, patch++ mock_prisma = MagicMock()+ # The budget tier has budget_duration="7d"+ mock_budget_row = MagicMock()+ mock_budget_row.budget_duration = "7d"+ mock_prisma.db.litellm_budgettable.find_unique = AsyncMock(+ return_value=mock_budget_row+ )++ mock_generate_key = AsyncMock(+ return_value={+ "key": "sk-test-key",+ "expires": None,+ "user_id": "test-user",+ "team_id": None,+ }+ )++ with patch(+ "litellm.proxy.proxy_server.prisma_client", mock_prisma+ ), patch(+ "litellm.proxy.proxy_server.llm_router", None+ ), patch(+ "litellm.proxy.proxy_server.premium_user", False+ ), patch(+ "litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"+ ), patch(+ "litellm.proxy.management_endpoints.key_management_endpoints.generate_key_helper_fn",+ mock_generate_key,+ ):+ await _common_key_generation_helper(+ data=GenerateKeyRequest(+ budget_id="7d-budget-tier",+ max_budget=10.0,+ budget_duration="30d", # explicit budget_duration should take precedence+ ),+ user_api_key_dict=UserAPIKeyAuth(+ user_role=LitellmUserRoles.PROXY_ADMIN,+ api_key="sk-1234",+ user_id="admin-user",+ ),+ litellm_changed_by=None,+ team_table=None,+ )++ mock_generate_key.assert_awaited_once()+ call_kwargs = mock_generate_key.call_args.kwargs++ # The explicit budget_duration should take precedence+ assert call_kwargs.get("key_budget_duration") == "30d", (+ "Explicit budget_duration should take precedence over the budget tier's value "+ f"but got: {call_kwargs.get('key_budget_duration')}"+ )++ # The budget tier should NOT have been looked up since budget_duration was explicit+ mock_prisma.db.litellm_budgettable.find_unique.assert_not_called()+++++ @pytest.mark.asyncio+ async def test_default_key_generate_params_duration(monkeypatch):+ """+ Test that default_key_generate_params with 'duration' is applied+ when no duration is provided in the key generation request.++ Regression test for bug where 'duration' was missing from the list+ of fields populated from default_key_generate_params.+ """+ import litellm++ mock_prisma_client = AsyncMock()+ mock_insert_data = AsyncMock(+ return_value=MagicMock(+ token="hashed_token_123", litellm_budget_table=None, object_permission=None+ )+ )+ mock_prisma_client.insert_data = mock_insert_data+ mock_prisma_client.db = MagicMock()+ mock_prisma_client.db.litellm_verificationtoken = MagicMock()+ mock_prisma_client.db.litellm_verificationtoken.find_unique = AsyncMock(+ return_value=None+ )+ mock_prisma_client.db.litellm_verificationtoken.find_many = AsyncMock(+ return_value=[]+ )+ mock_prisma_client.db.litellm_verificationtoken.count = AsyncMock(return_value=0)+ mock_prisma_client.db.litellm_verificationtoken.update = AsyncMock(+ return_value=MagicMock(+ token="hashed_token_123", litellm_budget_table=None, object_permission=None+ )+ )++ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)++ # Set default_key_generate_params with duration+ original_value = litellm.default_key_generate_params+ litellm.default_key_generate_params = {"duration": "180d"}++ try:+ request = GenerateKeyRequest() # No duration specified+ response = await _common_key_generation_helper(+ data=request,+ user_api_key_dict=UserAPIKeyAuth(+ user_role=LitellmUserRoles.PROXY_ADMIN,+ api_key="sk-1234",+ user_id="1234",+ ),+ litellm_changed_by=None,+ team_table=None,+ )++ # Verify duration was applied from defaults+ assert request.duration == "180d"+ finally:+ litellm.default_key_generate_params = original_value