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/litellm_internal_staging' into key-budget-window-usage

Thijmen Stavenuiterab9a3f4c6f14
diff --cc litellm/proxy/management_endpoints/key_management_endpoints.py
index 8d2a21ab078,54f567b7aa2..2b37d0bc8fc
--- a/litellm/proxy/management_endpoints/key_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/key_management_endpoints.py
@@@ -3462,152 -3531,16 +3532,107 @@@ async def delete_key_fn
async def _build_model_max_budget_usage(
api_key_hash: str,
model_max_budget: Mapping[str, Mapping[str, object]],
- user_api_key_cache: UserApiKeyCache | None,
+ user_api_key_cache: DualCache | None,
) -> dict[str, dict[str, object]]:
- if user_api_key_cache is None or not model_max_budget:
- return {}
-
- result: Final[dict[str, dict[str, object]]] = {}
- for model, budget_info in model_max_budget.items():
- try:
- budget_config = BudgetConfig.model_validate(budget_info)
- if budget_config.budget_duration is None:
- continue
- duration_in_seconds(budget_config.budget_duration)
- except Exception: # noqa: BLE001
- continue
- spend = await _get_model_max_budget_current_spend(
- api_key_hash=api_key_hash,
- model=model,
- budget_config=budget_config,
- user_api_key_cache=user_api_key_cache,
- )
- result[model] = {
- "current_spend": round(spend, 4),
- "budget_limit": budget_config.max_budget,
- "time_period": budget_config.budget_duration,
- }
- return result
+ return await build_model_max_budget_usage(
+ entity_type=Litellm_EntityType.KEY,
+ entity_id=api_key_hash,
+ model_max_budget=model_max_budget,
+ cache=user_api_key_cache,
+ )
+def _budget_window_to_dict(window: object) -> Mapping[str, object] | None:
+ """Coerce a budget_limits entry to a dict; None when the entry is unusable."""
+ if isinstance(window, dict):
+ return window
+ model_dump: Final = getattr(window, "model_dump", None)
+ if not callable(model_dump):
+ return None
+ try:
+ dumped: Final = model_dump()
+ except Exception: # noqa: BLE001 # model_dump implementations can raise arbitrary errors
+ return None
+ return dumped if isinstance(dumped, dict) else None
+
+
+def _coerce_budget_limits(budget_limits: object) -> Sequence[object] | None:
+ """Coerce budget_limits to a sequence of windows, parsing JSON strings; None when unusable."""
+ if isinstance(budget_limits, str):
+ try:
+ parsed: Final = json.loads(budget_limits)
+ except (TypeError, ValueError):
+ return None
+ return parsed if isinstance(parsed, list) else None
+ return budget_limits if isinstance(budget_limits, list) else None
+
+
+def _parse_window_max_budget(value: object) -> float | None:
+ """Coerce a window's max_budget to float; None when absent or unparseable."""
+ if isinstance(value, (int, float, str)):
+ try:
+ return float(value)
+ except (TypeError, ValueError):
+ return None
+ return None
+
+
+async def _budget_window_with_usage(window: Mapping[str, object], api_key_hash: str) -> Mapping[str, object]:
+ """
+ Return a copy of a budget window with current-window spend attached.
+
+ Per-window spend is not persisted in the DB; it lives in the cross-pod spend
+ counters (spend:key:{hashed_token}:window:{budget_duration}) that
+ _virtual_key_multi_budget_check enforces against, so we read the same
+ counters via get_current_spend. Passing max_budget + window_start makes the
+ read re-check against the authoritative spend-log aggregate when the counter
+ is stale-low (e.g. after a Redis flush), same as the enforcement path.
+ """
+ from litellm.proxy.proxy_server import get_current_spend
+
+ duration: Final = window.get("budget_duration")
+ if not duration:
+ return dict(window) # mutable-ok: per-window response copy, built once per window
+ spend: Final = await get_current_spend(
+ counter_key=f"spend:key:{api_key_hash}:window:{duration}",
+ fallback_spend=0.0,
+ max_budget=_parse_window_max_budget(window.get("max_budget")),
+ window_entity_type="Key",
+ window_entity_id=api_key_hash,
+ window_start=get_budget_window_start(window),
+ )
+ return {**window, "current_spend": round(spend, 4)} # mutable-ok: per-window response copy, built once per window
+
+
+async def _budget_limits_entry_with_usage(window: object, api_key_hash: str) -> object:
+ """Return the window as an enriched dict when dict-coercible; the original entry otherwise."""
+ coerced: Final = _budget_window_to_dict(window)
+ if not coerced:
+ return window
+ return await _budget_window_with_usage(window=coerced, api_key_hash=api_key_hash)
+
+
+async def _budget_limits_with_usage(budget_limits: object, api_key_hash: str) -> Sequence[object] | None:
+ """
+ Return budget_limits as window dicts with current-window spend attached.
+
+ None when budget_limits is not a usable (possibly JSON-encoded) list; the
+ caller keeps the original value then. Entries that are not dict-coercible
+ are preserved as-is.
+ """
+ windows: Final = _coerce_budget_limits(budget_limits)
+ if windows is None:
+ return None
+ return [ # mutable-ok: entries are awaited, so they cannot be built inside a frozen wrapper
+ await _budget_limits_entry_with_usage(window=window, api_key_hash=api_key_hash) for window in windows
+ ]
+
+
+# Caps per-request fan-out: each key with budget windows costs one spend-counter
+# read (worst case a SpendLogs aggregation) per window.
+MAX_KEY_INFO_KEYS_PER_REQUEST: Final = 100
+
+
@router.post(
"/v2/key/info",
tags=["key management"],
@@@ -3698,15 -3623,8 +3726,15 @@@ async def info_key_fn_v2
k_dict["model_max_budget_usage"] = await _build_model_max_budget_usage(
api_key_hash=k_token_hash,
model_max_budget=model_max_budget,
- user_api_key_cache=user_api_key_cache,
+ user_api_key_cache=model_max_budget_limiter.dual_cache,
)
+ if k_token_hash:
+ budget_limits_usage = await _budget_limits_with_usage(
+ budget_limits=k_dict.get("budget_limits"),
+ api_key_hash=k_token_hash,
+ )
+ if budget_limits_usage is not None:
+ k_dict["budget_limits"] = budget_limits_usage
filtered_key_info.append(k_dict)
return {"key": data.keys, "info": filtered_key_info}
@@@ -3820,14 -3738,8 +3851,14 @@@ async def info_key_fn
key_info["model_max_budget_usage"] = await _build_model_max_budget_usage(
api_key_hash=key_token_hash,
model_max_budget=model_max_budget,
- user_api_key_cache=user_api_key_cache,
+ user_api_key_cache=model_max_budget_limiter.dual_cache,
)
+ budget_limits_usage: Final = await _budget_limits_with_usage(
+ budget_limits=key_info.get("budget_limits"),
+ api_key_hash=key_token_hash,
+ )
+ if budget_limits_usage is not None:
+ key_info["budget_limits"] = budget_limits_usage
# Attach object_permission if object_permission_id is set
key_info = await attach_object_permission_to_dict(key_info, prisma_client)
diff --cc tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
index c05380dd054,0c615cbaa32..7f354795f25
--- a/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_key_management_endpoints.py
@@@ -13840,380 -13794,14 +13845,385 @@@ async def test_info_key_fn_v2_budget_ta
mock_prisma_client.db.query_raw.assert_not_awaited()
+async def test_info_key_fn_budget_limits_includes_current_spend(monkeypatch):
+ """
+ /key/info should attach current_spend to each budget_limits window, read from
+ the same spend counter (spend:key:{token}:window:{duration}) that budget
+ enforcement uses.
+ """
+ from unittest.mock import AsyncMock, MagicMock
+
+ from litellm.proxy._types import LiteLLM_VerificationToken
+ from litellm.proxy.management_endpoints.key_management_endpoints import info_key_fn
+
+ test_key_token = "hashed_token_window_test"
+ budget_limits = [
+ {
+ "reset_at": "2026-08-15T18:00:00+00:00",
+ "max_budget": 2.0,
+ "budget_duration": "1h",
+ }
+ ]
+
+ mock_prisma_client = AsyncMock()
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)
+ mock_user_api_key_cache = AsyncMock()
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.user_api_key_cache", mock_user_api_key_cache
+ )
+ mock_get_current_spend = AsyncMock(return_value=0.73)
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.get_current_spend", mock_get_current_spend
+ )
+
+ mock_key_info = MagicMock(spec=LiteLLM_VerificationToken)
+ mock_key_info.token = test_key_token
+ mock_key_info.object_permission_id = None
+ mock_key_info.user_id = "user-w"
+ mock_key_info.team_id = None
+ mock_key_info.litellm_budget_table = None
+ mock_key_info.model_dump.return_value = {
+ "token": test_key_token,
+ "budget_limits": [dict(w) for w in budget_limits],
+ "user_id": "user-w",
+ "team_id": None,
+ "object_permission_id": None,
+ "litellm_budget_table": None,
+ }
+ mock_key_info.dict.return_value = mock_key_info.model_dump.return_value
+
+ mock_prisma_client.db.litellm_verificationtoken.find_unique = AsyncMock(
+ return_value=mock_key_info
+ )
+
+ user_api_key_dict = UserAPIKeyAuth(
+ user_role=LitellmUserRoles.PROXY_ADMIN,
+ api_key="sk-test-window-key",
+ )
+
+ result = await info_key_fn(
+ key="sk-test-window-key",
+ user_api_key_dict=user_api_key_dict,
+ )
+
+ windows = result["info"]["budget_limits"]
+ assert len(windows) == 1
+ assert windows[0]["current_spend"] == 0.73
+ assert windows[0]["max_budget"] == 2.0
+ assert windows[0]["budget_duration"] == "1h"
+
+ mock_get_current_spend.assert_awaited_once()
+ call_kwargs = mock_get_current_spend.await_args.kwargs
+ assert call_kwargs["counter_key"] == f"spend:key:{test_key_token}:window:1h"
+ assert call_kwargs["max_budget"] == 2.0
+ assert call_kwargs["window_entity_type"] == "Key"
+ assert call_kwargs["window_entity_id"] == test_key_token
+ assert call_kwargs["window_start"] is not None
+
+
+async def test_info_key_fn_no_budget_limits_skips_spend_lookup(monkeypatch):
+ """Keys without budget_limits should not trigger window spend lookups."""
+ from unittest.mock import AsyncMock, MagicMock
+
+ from litellm.proxy._types import LiteLLM_VerificationToken
+ from litellm.proxy.management_endpoints.key_management_endpoints import info_key_fn
+
+ test_key_token = "hashed_token_no_windows"
+
+ mock_prisma_client = AsyncMock()
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)
+ mock_user_api_key_cache = AsyncMock()
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.user_api_key_cache", mock_user_api_key_cache
+ )
+ mock_get_current_spend = AsyncMock(return_value=0.0)
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.get_current_spend", mock_get_current_spend
+ )
+
+ mock_key_info = MagicMock(spec=LiteLLM_VerificationToken)
+ mock_key_info.token = test_key_token
+ mock_key_info.object_permission_id = None
+ mock_key_info.user_id = "user-nw"
+ mock_key_info.team_id = None
+ mock_key_info.litellm_budget_table = None
+ mock_key_info.model_dump.return_value = {
+ "token": test_key_token,
+ "budget_limits": None,
+ "user_id": "user-nw",
+ "team_id": None,
+ "object_permission_id": None,
+ "litellm_budget_table": None,
+ }
+ mock_key_info.dict.return_value = mock_key_info.model_dump.return_value
+
+ mock_prisma_client.db.litellm_verificationtoken.find_unique = AsyncMock(
+ return_value=mock_key_info
+ )
+
+ user_api_key_dict = UserAPIKeyAuth(
+ user_role=LitellmUserRoles.PROXY_ADMIN,
+ api_key="sk-test-no-window-key",
+ )
+
+ result = await info_key_fn(
+ key="sk-test-no-window-key",
+ user_api_key_dict=user_api_key_dict,
+ )
+
+ assert result["info"]["budget_limits"] is None
+ mock_get_current_spend.assert_not_awaited()
+
+
+async def test_info_key_fn_v2_budget_limits_includes_current_spend(monkeypatch):
+ """/v2/key/info should attach current_spend to each budget_limits window."""
+ from unittest.mock import AsyncMock, MagicMock
+
+ from litellm.proxy._types import KeyRequest, LiteLLM_VerificationToken
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ info_key_fn_v2,
+ )
+
+ test_key_token = "hashed_token_v2_window_test"
+
+ mock_prisma_client = AsyncMock()
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)
+ mock_user_api_key_cache = AsyncMock()
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.user_api_key_cache", mock_user_api_key_cache
+ )
+ mock_get_current_spend = AsyncMock(return_value=1.25)
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.get_current_spend", mock_get_current_spend
+ )
+
+ mock_key = MagicMock(spec=LiteLLM_VerificationToken)
+ mock_key.token = test_key_token
+ mock_key.user_id = "user-v2-w"
+ mock_key.team_id = None
+ mock_key.model_dump.return_value = {
+ "token": test_key_token,
+ "budget_limits": [
+ {
+ "reset_at": "2026-08-15T18:00:00+00:00",
+ "max_budget": 2.0,
+ "budget_duration": "1h",
+ },
+ {
+ "reset_at": "2026-08-16T00:00:00+00:00",
+ "max_budget": 20.0,
+ "budget_duration": "1d",
+ },
+ ],
+ "user_id": "user-v2-w",
+ "team_id": None,
+ "litellm_budget_table": None,
+ }
+ mock_key.dict.return_value = mock_key.model_dump.return_value
+
+ mock_prisma_client.get_data = AsyncMock(return_value=[mock_key])
+
+ user_api_key_dict = UserAPIKeyAuth(
+ user_role=LitellmUserRoles.PROXY_ADMIN,
+ api_key="sk-admin-v2-w",
+ )
+
+ result = await info_key_fn_v2(
+ data=KeyRequest(keys=[test_key_token]),
+ user_api_key_dict=user_api_key_dict,
+ )
+
+ assert len(result["info"]) == 1
+ windows = result["info"][0]["budget_limits"]
+ assert len(windows) == 2
+ assert windows[0]["current_spend"] == 1.25
+ assert windows[1]["current_spend"] == 1.25
+ assert mock_get_current_spend.await_count == 2
+ counter_keys = {
+ call.kwargs["counter_key"] for call in mock_get_current_spend.await_args_list
+ }
+ assert counter_keys == {
+ f"spend:key:{test_key_token}:window:1h",
+ f"spend:key:{test_key_token}:window:1d",
+ }
+
+
+async def test_info_key_fn_v2_rejects_oversized_batch(monkeypatch):
+ """/v2/key/info must reject over-cap batches before doing any DB work."""
+ from unittest.mock import AsyncMock
+
+ from litellm.proxy._types import KeyRequest, ProxyException
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ MAX_KEY_INFO_KEYS_PER_REQUEST,
+ info_key_fn_v2,
+ )
+
+ mock_prisma_client = AsyncMock()
+ monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)
+ monkeypatch.setattr("litellm.proxy.proxy_server.user_api_key_cache", AsyncMock())
+
+ user_api_key_dict = UserAPIKeyAuth(
+ user_role=LitellmUserRoles.PROXY_ADMIN,
+ api_key="sk-admin-batch-cap",
+ )
+
+ with pytest.raises(ProxyException) as exc_info:
+ await info_key_fn_v2(
+ data=KeyRequest(
+ keys=[f"hash-{i}" for i in range(MAX_KEY_INFO_KEYS_PER_REQUEST)],
+ key_aliases=["alias-over-cap"],
+ ),
+ user_api_key_dict=user_api_key_dict,
+ )
+
+ assert exc_info.value.code == "422"
+ mock_prisma_client.get_data.assert_not_awaited()
+
+
+async def test_budget_limits_with_usage_json_string_input(monkeypatch):
+ """budget_limits stored as a JSON string should be parsed and annotated."""
+ import json as json_module
+ from unittest.mock import AsyncMock
+
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ _budget_limits_with_usage,
+ )
+
+ mock_get_current_spend = AsyncMock(return_value=0.5)
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.get_current_spend", mock_get_current_spend
+ )
+
+ raw = json_module.dumps(
+ [{"budget_duration": "1h", "max_budget": 2.0, "reset_at": None}]
+ )
+ result = await _budget_limits_with_usage(budget_limits=raw, api_key_hash="hash-1")
+
+ assert result == [
+ {
+ "budget_duration": "1h",
+ "max_budget": 2.0,
+ "reset_at": None,
+ "current_spend": 0.5,
+ }
+ ]
+ mock_get_current_spend.assert_awaited_once()
+
+
+async def test_budget_limits_with_usage_skips_unusable_inputs(monkeypatch):
+ """Invalid JSON strings, non-list values, and malformed windows are skipped."""
+ from unittest.mock import AsyncMock
+
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ _budget_limits_with_usage,
+ )
+
+ mock_get_current_spend = AsyncMock(return_value=0.0)
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.get_current_spend", mock_get_current_spend
+ )
+
+ # invalid JSON string and non-list values return None: callers keep the original
+ assert await _budget_limits_with_usage(budget_limits="{not json", api_key_hash="hash-1") is None
+ assert await _budget_limits_with_usage(budget_limits={"budget_duration": "1h"}, api_key_hash="hash-1") is None
+
+ # windows that are falsy, missing budget_duration, or not dict-like
+ windows = [
+ {},
+ {"max_budget": 2.0},
+ {"budget_duration": "1h", "max_budget": "not-a-number"},
+ 42,
+ ]
+ result = await _budget_limits_with_usage(budget_limits=windows, api_key_hash="hash-1")
+
+ # only the well-formed window (with unparseable max_budget coerced to None)
+ # triggers a spend lookup
+ mock_get_current_spend.assert_awaited_once()
+ call_kwargs = mock_get_current_spend.await_args.kwargs
+ assert call_kwargs["counter_key"] == "spend:key:hash-1:window:1h"
+ assert call_kwargs["max_budget"] is None
+ assert result is not None
+ assert result[0] == {}
+ assert result[1] == {"max_budget": 2.0}
+ assert result[2] == {"budget_duration": "1h", "max_budget": "not-a-number", "current_spend": 0.0}
+ assert result[3] == 42
+ # input is not mutated
+ assert windows[2] == {"budget_duration": "1h", "max_budget": "not-a-number"}
+
+
+async def test_budget_limits_with_usage_window_without_max_budget(monkeypatch):
+ """A window with only budget_duration still gets current_spend, read without a budget ceiling."""
+ from unittest.mock import AsyncMock
+
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ _budget_limits_with_usage,
+ )
+
+ mock_get_current_spend = AsyncMock(return_value=0.75)
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.get_current_spend", mock_get_current_spend
+ )
+
+ result = await _budget_limits_with_usage(
+ budget_limits=[{"budget_duration": "2d"}], api_key_hash="hash-no-max"
+ )
+
+ assert result == [{"budget_duration": "2d", "current_spend": 0.75}]
+ call_kwargs = mock_get_current_spend.await_args.kwargs
+ assert call_kwargs["counter_key"] == "spend:key:hash-no-max:window:2d"
+ assert call_kwargs["max_budget"] is None
+
+
+async def test_budget_limits_with_usage_pydantic_windows(monkeypatch):
+ """Window objects with model_dump() are converted to dicts; failing windows pass through."""
+ from unittest.mock import AsyncMock, MagicMock
+
+ from litellm.proxy.management_endpoints.key_management_endpoints import (
+ _budget_limits_with_usage,
+ )
+
+ mock_get_current_spend = AsyncMock(return_value=1.0)
+ monkeypatch.setattr(
+ "litellm.proxy.proxy_server.get_current_spend", mock_get_current_spend
+ )
+
+ good_window = MagicMock()
+ good_window.model_dump.return_value = {
+ "budget_duration": "7d",
+ "max_budget": 10.0,
+ "reset_at": None,
+ }
+ bad_window = MagicMock()
+ bad_window.model_dump.side_effect = ValueError("boom")
+
+ result = await _budget_limits_with_usage(
+ budget_limits=[good_window, bad_window], api_key_hash="hash-2"
+ )
+
+ # good window converted to dict and annotated; failing window left as-is
+ assert result is not None
+ assert isinstance(result[0], dict)
+ assert result[0]["current_spend"] == 1.0
+ assert result[1] is bad_window
+ mock_get_current_spend.assert_awaited_once()
+
+
@pytest.mark.asyncio
- async def test_info_key_fn_provider_prefix_spend_fallback(monkeypatch):
- """Cached spend for 'gpt-4o' matches budget key 'openai/gpt-4o' via suffix match."""
+ async def test_info_key_fn_reads_the_configured_budget_model_key(monkeypatch):
+ """/key/info reads the one counter enforcement reads: the configured budget model.
+
+ It used to probe a second, provider-stripped key because the counter was
+ written under the request model instead, which is what let a key report zero
+ usage while being blocked at 429.
+ """
from unittest.mock import AsyncMock, MagicMock
from litellm.proxy._types import LiteLLM_VerificationToken