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 'litellm_window_spend_schema' into litellm_window_spend_writer
ryan-crabbe-berri140950f52dd0
diff --cc litellm/proxy/common_utils/reset_budget_job.pyindex 2bf5b7b9283,55539ded02b..1682cf12f4e--- a/litellm/proxy/common_utils/reset_budget_job.py+++ b/litellm/proxy/common_utils/reset_budget_job.py@@@ -35,8 -33,11 +35,12 @@@ from litellm.proxy.common_utils.timezoncompute_budget_reset_at,get_budget_reset_settings,)- from litellm.proxy.common_utils.user_api_key_cache import tag_cache_key+ from litellm.proxy.common_utils.user_api_key_cache import (+ model_access_group_cache_key,+ model_access_group_spend_counter_key,+ tag_cache_key,+ )+from litellm.proxy.db.budget_window_spend_writer import roll_window_spend_rowfrom litellm.proxy.db.db_transaction_queue.pod_lock_manager import PodLockManagerfrom litellm.proxy.db.exception_handler import call_with_db_reconnect_retryfrom litellm.proxy.utils import PrismaClient, ProxyLoggingdiff --cc litellm/proxy/hooks/proxy_track_cost_callback.pyindex a8ffb650ff0,d0e4f548c99..f02901f0e97--- a/litellm/proxy/hooks/proxy_track_cost_callback.py+++ b/litellm/proxy/hooks/proxy_track_cost_callback.py@@@ -572,9 -584,10 +584,10 @@@ async def _update_database_and_spend_coresponse_cost: float,budget_reservation: dict | None,request_tags: list[str] | None = None,+ model_access_groups: Sequence[str] | None = None,) -> None:try:- await proxy_logging_obj.db_spend_update_writer.update_database(+ spend_log_request_id = await proxy_logging_obj.db_spend_update_writer.update_database(token=user_api_key,response_cost=response_cost,user_id=user_id,@@@ -610,8 -623,7 +623,9 @@@budget_reservation=budget_reservation,end_user_id=end_user_id,tags=request_tags,+ request_id=spend_log_request_id,+ request_started_at=start_time,+ model_access_groups=model_access_groups,)except Exception:if budget_reservation is not None:diff --cc litellm/proxy/proxy_server.pyindex 2722e000578,2bad742ad02..7b4f264b463--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@@ -2651,8 -2650,7 +2653,9 @@@ async def increment_spend_countersbudget_reservation: dict | None = None,end_user_id: str | None = None,tags: list[str] | None = None,+ request_id: str | None = None,+ request_started_at: datetime | None = None,+ model_access_groups: Sequence[str] | None = None,):"""Atomically increment spend counters for budget enforcement.diff --cc tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.pyindex 8fd8730d409,5160dd3431e..2a540f4f522--- a/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py+++ b/tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py@@@ -601,8 -599,7 +602,9 @@@ async def test_update_database_and_spenbudget_reservation=budget_reservation,end_user_id="test_end_user_id",tags=["tag-a"],+ request_id="chatcmpl-abc123",+ request_started_at=start_time,+ model_access_groups=("premium",),)@@@ -1882,56 -1879,111 +1884,166 @@@ async def test_track_cost_callback_logs)+async def test_update_database_and_spend_counters_forwards_the_spend_log_request_id():+ """The budget-window flush excludes the log rows its increments already+ cover. That only works if the id update_database recorded the row under is+ handed to the counter update, so this seam is load-bearing."""+ proxy_logging_obj = MagicMock()+ proxy_logging_obj.db_spend_update_writer.update_database = AsyncMock(+ return_value="chatcmpl-abc123"+ )+ increment_spend_counters = AsyncMock()++ await _update_database_and_spend_counters(+ proxy_logging_obj=proxy_logging_obj,+ increment_spend_counters=increment_spend_counters,+ user_api_key="test_api_key",+ user_id="test_user_id",+ end_user_id=None,+ team_id="test_team_id",+ org_id="test_org_id",+ kwargs={},+ completion_response=None,+ start_time=datetime.now(),+ end_time=datetime.now(),+ response_cost=0.2,+ budget_reservation=None,+ )++ assert increment_spend_counters.await_args.kwargs["request_id"] == "chatcmpl-abc123"+++async def test_update_database_and_spend_counters_forwards_a_missing_request_id_as_none():+ proxy_logging_obj = MagicMock()+ proxy_logging_obj.db_spend_update_writer.update_database = AsyncMock(return_value=None)+ increment_spend_counters = AsyncMock()++ await _update_database_and_spend_counters(+ proxy_logging_obj=proxy_logging_obj,+ increment_spend_counters=increment_spend_counters,+ user_api_key="test_api_key",+ user_id="test_user_id",+ end_user_id=None,+ team_id="test_team_id",+ org_id="test_org_id",+ kwargs={},+ completion_response=None,+ start_time=datetime.now(),+ end_time=datetime.now(),+ response_cost=0.2,+ budget_reservation=None,+ )++ assert increment_spend_counters.await_args.kwargs["request_id"] is None+++++ class _FakeDeploymentLookup:+ """Deployment lookup returning the access groups each deployment declares."""++ def __init__(self, deployments):+ self._deployments = deployments++ def get_model_info(self, id):+ if id not in self._deployments:+ return None+ return {"model_name": "premium-haiku", "model_info": {"id": id, "access_groups": list(self._deployments[id])}}+++ def _model_access_group_kwargs(granted, served_model_id=None):+ metadata = {"user_api_key": "hashed-key", "user_api_key_user_id": "user-1"}+ if granted is not None:+ metadata[MODEL_ACCESS_GROUP_METADATA_KEY] = list(granted)+ return {+ "call_type": "acompletion",+ "model": "premium-haiku",+ "litellm_call_id": "test-call-id",+ "litellm_params": {"metadata": metadata},+ "stream": False,+ "standard_logging_object": {"response_cost": 0.25, "request_tags": None, "model_id": served_model_id},+ }+++ async def _groups_charged_by_the_callback(kwargs, deployments=None):+ """The groups the callback hands the spend counters for one request.++ The callback resolves ``proxy_logging_obj`` and the router by importing them off+ ``proxy_server`` inside its own body, so there is no seam to inject either through.+ """+ logger = _ProxyDBLogger()+ with (+ patch( # test-quality-ok: callback imports proxy_logging_obj off proxy_server in its body, no seam+ "litellm.proxy.proxy_server.proxy_logging_obj"+ ) as mock_proxy_logging,+ patch( # test-quality-ok: the arguments to this call are the boundary under test+ "litellm.proxy.hooks.proxy_track_cost_callback._update_database_and_spend_counters",+ new=AsyncMock(),+ ) as mock_update,+ patch( # test-quality-ok: llm_router is a proxy_server global the callback reads lazily, no seam+ "litellm.proxy.proxy_server.llm_router", new=_FakeDeploymentLookup(deployments or {})+ ),+ ):+ mock_proxy_logging.failed_tracking_alert = AsyncMock()+ mock_proxy_logging.slack_alerting_instance.customer_spend_alert = AsyncMock()+ mock_proxy_logging.db_spend_update_writer = MagicMock()+ mock_proxy_logging.db_spend_update_writer.update_database = AsyncMock()++ await logger._PROXY_track_cost_callback(+ kwargs=kwargs,+ completion_response=None,+ start_time=datetime.now(),+ end_time=datetime.now(),+ )++ return mock_update.await_args.kwargs["model_access_groups"]+++ @pytest.mark.asyncio+ async def test_track_cost_callback_charges_the_model_access_groups_auth_stamped():+ """Auth stamps the matched groups onto request metadata; the callback has to carry them through.++ Without this hop nothing writes ``spend:model_access_group:*`` on the normal path, so with+ reservations disabled the budget check reads a counter no one maintains.+ """+ charged = await _groups_charged_by_the_callback(+ kwargs=_model_access_group_kwargs(granted=["premium", "starter"]),+ )++ assert charged == ("premium", "starter")+++ @pytest.mark.asyncio+ async def test_track_cost_callback_charges_no_model_access_group_when_none_were_stamped():+ """A request no budgeted group authorized must not debit anything."""+ charged = await _groups_charged_by_the_callback(+ kwargs=_model_access_group_kwargs(granted=None),+ )++ assert charged == ()+++ @pytest.mark.asyncio+ async def test_spend_counters_only_debit_the_group_the_served_deployment_belongs_to():+ """A caller granted two pools that both cover the model group only draws down the pool that served.++ The database writer already narrows by served deployment, so passing the unnarrowed set to the+ live counters let one request block a pool the persisted spend never debited.+ """+ charged = await _groups_charged_by_the_callback(+ kwargs=_model_access_group_kwargs(granted=["premium", "tier0"], served_model_id="deployment-premium"),+ deployments={"deployment-premium": ["premium"], "deployment-tier0": ["tier0"]},+ )++ assert charged == ("premium",)+++ @pytest.mark.asyncio+ async def test_spend_counters_keep_every_granted_group_when_the_deployment_is_unknown():+ """An unidentifiable deployment leaves the auth-time set standing, so nothing silently stops billing."""+ charged = await _groups_charged_by_the_callback(+ kwargs=_model_access_group_kwargs(granted=["premium", "tier0"], served_model_id="deployment-gone"),+ deployments={"deployment-premium": ["premium"]},+ )++ assert charged == ("premium", "tier0")