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
fix(passthrough): thread router-model attribution on the litellm_metadata bucket
mateo-berrib1035368f898
diff --git a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.pyindex fc891978ad5..55d6b88363f 100644--- a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py+++ b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py@@ -116,16 +116,24 @@ def get_passthrough_router_request_metadata(user_api_key_dict: UserAPIKeyAuth) -callback cannot attribute spend to the calling key and never releases thebudget reservation minted at auth time, so the shared spend counter driftsup until the key falsely trips ``BudgetExceededError``.++ The payload rides the ``litellm_metadata`` bucket, not ``metadata``: the+ router hop ``_ageneric_api_call_with_fallbacks`` canonicalises this call+ type into ``litellm_metadata``, and the cost callback reads spend+ attribution from that bucket while only backfilling ``user_api_key*`` keys+ from ``metadata``. Passing ``metadata=`` would silently drop the secondary+ attribution fields the helper sets (``agent_id``,+ ``user_api_end_user_max_budget``) before the callback ever sees them."""from litellm.proxy.litellm_pre_call_utils import LiteLLMProxyRequestSetup- request_data: Final = {"metadata": {}} # mutable-ok: attribution builder + litellm mutate this dict in place+ request_data: Final = {"litellm_metadata": {}} # mutable-ok: builder + litellm mutate this in placeLiteLLMProxyRequestSetup.add_user_api_key_auth_to_request_metadata(data=request_data,user_api_key_dict=user_api_key_dict,- _metadata_variable_name="metadata",+ _metadata_variable_name="litellm_metadata",)- return request_data["metadata"]+ return request_data["litellm_metadata"]async def llm_passthrough_factory_proxy_route(@@ -368,7 +376,7 @@ async def vllm_proxy_route(params=None,headers=None,cookies=None,- metadata=get_passthrough_router_request_metadata(user_api_key_dict),+ litellm_metadata=get_passthrough_router_request_metadata(user_api_key_dict),),)@@ -1498,7 +1506,7 @@ async def azure_proxy_route(params=None,headers=None,cookies=None,- metadata=get_passthrough_router_request_metadata(user_api_key_dict),+ litellm_metadata=get_passthrough_router_request_metadata(user_api_key_dict),)if is_streaming_request:diff --git a/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py b/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.pyindex 032cb360d4c..f789ba10490 100644--- a/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py+++ b/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py@@ -4076,6 +4076,8 @@ class TestPassthroughRouterModelBudgetReservation:user_id="u1",team_id="t1",budget_reservation=reservation,+ agent_id="agent-xyz",+ end_user_max_budget=42.0,)def _request(self) -> MagicMock:@@ -4106,11 +4108,17 @@ class TestPassthroughRouterModelBudgetReservation:def _assert_metadata_carries_attribution(self, captured: list[dict], user_api_key_dict: UserAPIKeyAuth) -> None:assert len(captured) == 1, "the router-model branch must dispatch exactly once"- metadata = captured[0]["metadata"]- assert metadata["user_api_key"] == user_api_key_dict.api_key- assert metadata["user_api_key_budget_reservation"] is user_api_key_dict.budget_reservation- assert metadata["user_api_key_user_id"] == user_api_key_dict.user_id- assert metadata["user_api_key_team_id"] == user_api_key_dict.team_id+ assert captured[0].get("metadata") is None, (+ "attribution must ride the litellm_metadata bucket the router canonicalizes on; "+ "the plain metadata bucket is dropped for every non-user_api_key field"+ )+ litellm_metadata = captured[0]["litellm_metadata"]+ assert litellm_metadata["user_api_key"] == user_api_key_dict.api_key+ assert litellm_metadata["user_api_key_budget_reservation"] is user_api_key_dict.budget_reservation+ assert litellm_metadata["user_api_key_user_id"] == user_api_key_dict.user_id+ assert litellm_metadata["user_api_key_team_id"] == user_api_key_dict.team_id+ assert litellm_metadata["agent_id"] == user_api_key_dict.agent_id+ assert litellm_metadata["user_api_end_user_max_budget"] == user_api_key_dict.end_user_max_budget@pytest.mark.asyncioasync def test_vllm_router_model_threads_key_metadata(self, monkeypatch):