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
[Bug fix] Multiple API Keys Created on Startup When max_budget is Enabled (#12436)
Ishaan Jaffd720b3d369ec
diff --git a/litellm/proxy/_experimental/out/onboarding.html b/litellm/proxy/_experimental/out/onboarding.htmldeleted file mode 100644index 061a432ac30..00000000000--- a/litellm/proxy/_experimental/out/onboarding.html+++ /dev/null@@ -1 +0,0 @@-<!DOCTYPE html><html id="__next_error__"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="preload" as="script" fetchPriority="low" href="/litellm-asset-prefix/_next/static/chunks/webpack-a426aae3231a8df1.js"/><script src="/litellm-asset-prefix/_next/static/chunks/fd9d1056-205af899b895cbac.js" async=""></script><script src="/litellm-asset-prefix/_next/static/chunks/117-a0da667066d322b6.js" async=""></script><script src="/litellm-asset-prefix/_next/static/chunks/main-app-475d6efe4080647d.js" async=""></script><title>LiteLLM Dashboard</title><meta name="description" content="LiteLLM Proxy Admin UI"/><link rel="icon" href="/favicon.ico" type="image/x-icon" sizes="16x16"/><link rel="icon" href="./favicon.ico"/><meta name="next-size-adjust"/><script src="/litellm-asset-prefix/_next/static/chunks/polyfills-42372ed130431b0a.js" noModule=""></script></head><body><script src="/litellm-asset-prefix/_next/static/chunks/webpack-a426aae3231a8df1.js" async=""></script><script>(self.__next_f=self.__next_f||[]).push([0]);self.__next_f.push([2,null])</script><script>self.__next_f.push([1,"1:HL[\"/litellm-asset-prefix/_next/static/media/e4af272ccee01ff0-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n2:HL[\"/litellm-asset-prefix/_next/static/css/31b7f215e119031e.css\",\"style\"]\n3:HL[\"/litellm-asset-prefix/_next/static/css/d7889674c16b55a5.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"4:I[12846,[],\"\"]\n6:I[19107,[],\"ClientPageRoot\"]\n7:I[12011,[\"665\",\"static/chunks/3014691f-b7b79b78e27792f3.js\",\"402\",\"static/chunks/402-d418d0a7bf158a22.js\",\"899\",\"static/chunks/899-3dc1e1a0832876e3.js\",\"250\",\"static/chunks/250-ef2b37352f059268.js\",\"461\",\"static/chunks/app/onboarding/page-14caffd05bb17048.js\"],\"default\",1]\n8:I[4707,[],\"\"]\n9:I[36423,[],\"\"]\nb:I[61060,[],\"\"]\nc:[]\n"])</script><script>self.__next_f.push([1,"0:[\"$\",\"$L4\",null,{\"buildId\":\"MMdp1-El_-0lE4vSV9loU\",\"assetPrefix\":\"/litellm-asset-prefix\",\"urlParts\":[\"\",\"onboarding\"],\"initialTree\":[\"\",{\"children\":[\"onboarding\",{\"children\":[\"__PAGE__\",{}]}]},\"$undefined\",\"$undefined\",true],\"initialSeedData\":[\"\",{\"children\":[\"onboarding\",{\"children\":[\"__PAGE__\",{},[[\"$L5\",[\"$\",\"$L6\",null,{\"props\":{\"params\":{},\"searchParams\":{}},\"Component\":\"$7\"}],null],null],null]},[null,[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"onboarding\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L9\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\"}]],null]},[[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/litellm-asset-prefix/_next/static/css/31b7f215e119031e.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/litellm-asset-prefix/_next/static/css/d7889674c16b55a5.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]],[\"$\",\"html\",null,{\"lang\":\"en\",\"children\":[\"$\",\"body\",null,{\"className\":\"__className_b0dd8a\",\"children\":[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L9\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[[\"$\",\"title\",null,{\"children\":\"404: This page could not be found.\"}],[\"$\",\"div\",null,{\"style\":{\"fontFamily\":\"system-ui,\\\"Segoe UI\\\",Roboto,Helvetica,Arial,sans-serif,\\\"Apple Color Emoji\\\",\\\"Segoe UI Emoji\\\"\",\"height\":\"100vh\",\"textAlign\":\"center\",\"display\":\"flex\",\"flexDirection\":\"column\",\"alignItems\":\"center\",\"justifyContent\":\"center\"},\"children\":[\"$\",\"div\",null,{\"children\":[[\"$\",\"style\",null,{\"dangerouslySetInnerHTML\":{\"__html\":\"body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}\"}}],[\"$\",\"h1\",null,{\"className\":\"next-error-h1\",\"style\":{\"display\":\"inline-block\",\"margin\":\"0 20px 0 0\",\"padding\":\"0 23px 0 0\",\"fontSize\":24,\"fontWeight\":500,\"verticalAlign\":\"top\",\"lineHeight\":\"49px\"},\"children\":\"404\"}],[\"$\",\"div\",null,{\"style\":{\"display\":\"inline-block\"},\"children\":[\"$\",\"h2\",null,{\"style\":{\"fontSize\":14,\"fontWeight\":400,\"lineHeight\":\"49px\",\"margin\":0},\"children\":\"This page could not be found.\"}]}]]}]}]],\"notFoundStyles\":[]}]}]}]],null],null],\"couldBeIntercepted\":false,\"initialHead\":[null,\"$La\"],\"globalErrorComponent\":\"$b\",\"missingSlots\":\"$Wc\"}]\n"])</script><script>self.__next_f.push([1,"a:[[\"$\",\"meta\",\"0\",{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1\"}],[\"$\",\"meta\",\"1\",{\"charSet\":\"utf-8\"}],[\"$\",\"title\",\"2\",{\"children\":\"LiteLLM Dashboard\"}],[\"$\",\"meta\",\"3\",{\"name\":\"description\",\"content\":\"LiteLLM Proxy Admin UI\"}],[\"$\",\"link\",\"4\",{\"rel\":\"icon\",\"href\":\"/favicon.ico\",\"type\":\"image/x-icon\",\"sizes\":\"16x16\"}],[\"$\",\"link\",\"5\",{\"rel\":\"icon\",\"href\":\"./favicon.ico\"}],[\"$\",\"meta\",\"6\",{\"name\":\"next-size-adjust\"}]]\n5:null\n"])</script></body></html>\ No newline at end of filediff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.pyindex 13f0d1753d0..e9097f4c2f7 100644--- a/litellm/proxy/management_endpoints/key_management_endpoints.py+++ b/litellm/proxy/management_endpoints/key_management_endpoints.py@@ -1497,11 +1497,7 @@ async def generate_key_helper_fn( # noqa: PLR0915] = None, # object_permission_id <-> LiteLLM_ObjectPermissionTableobject_permission: Optional[LiteLLM_ObjectPermissionBase] = None,):- from litellm.proxy.proxy_server import (- litellm_proxy_budget_name,- premium_user,- prisma_client,- )+ from litellm.proxy.proxy_server import premium_user, prisma_clientif prisma_client is None:raise Exception(@@ -1660,10 +1656,8 @@ async def generate_key_helper_fn( # noqa: PLR0915table_name="user",update_key_values=update_key_values,)- if user_id == litellm_proxy_budget_name or (- table_name is not None and table_name == "user"- ):- # do not create a key for litellm_proxy_budget_name or if table name is set to just 'user'+ if table_name is not None and table_name == "user":+ # do not create a key if table name is set to just 'user'# we only need to ensure this exists in the user table# the LiteLLM_VerificationToken table will increase in size if we don't do this checkreturn user_datadiff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 3e15a2ecf50..eaef3c08879 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -3305,6 +3305,7 @@ class ProxyStartupEvent:asyncio.create_task(generate_key_helper_fn( # type: ignorerequest_type="user",+ table_name="user",user_id=litellm_proxy_budget_name,duration=None,models=[],diff --git a/tests/test_litellm/proxy/test_proxy_server.py b/tests/test_litellm/proxy/test_proxy_server.pyindex 75fffeac5a9..d33b637f8e3 100644--- a/tests/test_litellm/proxy/test_proxy_server.py+++ b/tests/test_litellm/proxy/test_proxy_server.py@@ -611,3 +611,53 @@ async def test_get_config_from_file(tmp_path, monkeypatch):result = await proxy_config._get_config_from_file(None)assert result == test_config+++async def test_add_proxy_budget_to_db_only_creates_user_no_keys():+ """+ Test that _add_proxy_budget_to_db only creates a user and no keys are added.++ This validates that generate_key_helper_fn is called with table_name="user"+ which should prevent key creation in LiteLLM_VerificationToken table.+ """+ from unittest.mock import AsyncMock, patch++ import litellm+ from litellm.proxy.proxy_server import ProxyStartupEvent++ # Set up required litellm settings+ litellm.budget_duration = "30d"+ litellm.max_budget = 100.0++ litellm_proxy_budget_name = "litellm-proxy-budget"++ # Mock generate_key_helper_fn to capture its call arguments+ mock_generate_key_helper = AsyncMock(return_value={+ "user_id": litellm_proxy_budget_name,+ "max_budget": 100.0,+ "budget_duration": "30d",+ "spend": 0,+ "models": [],+ })++ # Patch generate_key_helper_fn in proxy_server where it's being called from+ with patch("litellm.proxy.proxy_server.generate_key_helper_fn", mock_generate_key_helper):+ # Call the function under test+ ProxyStartupEvent._add_proxy_budget_to_db(litellm_proxy_budget_name)++ # Allow async task to complete+ import asyncio+ await asyncio.sleep(0.1)++ # Verify that generate_key_helper_fn was called+ mock_generate_key_helper.assert_called_once()+ call_args = mock_generate_key_helper.call_args++ # Verify critical parameters that prevent key creation+ assert call_args.kwargs["request_type"] == "user"+ assert call_args.kwargs["table_name"] == "user"+ assert call_args.kwargs["user_id"] == litellm_proxy_budget_name+ assert call_args.kwargs["max_budget"] == 100.0+ assert call_args.kwargs["budget_duration"] == "30d"+ assert call_args.kwargs["query_type"] == "update_data"