diff --git a/litellm/proxy/hooks/proxy_track_cost_callback.py b/litellm/proxy/hooks/proxy_track_cost_callback.py index 7f879970fae..2951bc02d8c 100644 --- a/litellm/proxy/hooks/proxy_track_cost_callback.py +++ b/litellm/proxy/hooks/proxy_track_cost_callback.py @@ -13,11 +13,7 @@ import litellm from litellm._logging import verbose_proxy_logger from litellm.batches.batch_utils import batch_cost_is_final from litellm.caching.redis_batch import flush_post_call_redis_batches -from litellm.constants import ( - BACKGROUND_INTERACTION_COST_POLLING_ENABLED, - BILLED_RETRY_ATTEMPT_METADATA_KEY, - USER_API_KEY_IS_VIRTUAL_KEY_METADATA_KEY, -) +from litellm.constants import BACKGROUND_INTERACTION_COST_POLLING_ENABLED, BILLED_RETRY_ATTEMPT_METADATA_KEY from litellm.integrations.custom_logger import CustomLogger from litellm.litellm_core_utils.core_helpers import ( budget_reservation_from_metadata, @@ -460,9 +456,6 @@ class _ProxyDBLogger(CustomLogger): LiteLLMProxyRequestSetup.get_sanitized_user_information_from_key(user_api_key_dict=user_api_key_dict) ) _metadata["user_api_key"] = LiteLLMProxyRequestSetup.get_logged_api_key(user_api_key_dict) - _metadata[USER_API_KEY_IS_VIRTUAL_KEY_METADATA_KEY] = LiteLLMProxyRequestSetup.authenticated_with_virtual_key( - user_api_key_dict - ) _metadata["status"] = "failure" _error_information = StandardLoggingPayloadSetup.get_error_information( original_exception=original_exception, diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.py index e222aebe134..3c6ba2e8f2d 100644 --- a/litellm/proxy/litellm_pre_call_utils.py +++ b/litellm/proxy/litellm_pre_call_utils.py @@ -45,7 +45,6 @@ from litellm.constants import ( SESSION_ID_GENERATED_METADATA_KEY, SESSION_ID_OMITTED_METADATA_KEY, TYPESAFE_DANGER_ZONE_HEADER_PREFIX, - USER_API_KEY_IS_VIRTUAL_KEY_METADATA_KEY, VECTOR_STORE_CALLER_METADATA_KWARG, X_LITELLM_DISABLE_CALLBACKS, ) @@ -2058,6 +2057,7 @@ class LiteLLMProxyRequestSetup: user_api_key_dict.budget_reset_at.isoformat() if user_api_key_dict.budget_reset_at else None ), user_api_key_auth_metadata=auth_metadata, + user_api_key_is_virtual_key=LiteLLMProxyRequestSetup.authenticated_with_virtual_key(user_api_key_dict), ) return user_api_key_logged_metadata @@ -2071,9 +2071,8 @@ class LiteLLMProxyRequestSetup: @staticmethod def authenticated_with_virtual_key(user_api_key_dict: UserAPIKeyAuth) -> bool: - """A serializable copy of the unforgeable via_virtual_key marker, carried in request metadata so - spend paths that drop the auth object (collector, batch and background polling, failure logging) - still know the request came through a virtual key.""" + """A serializable copy of the unforgeable via_virtual_key marker, carried in the key's logged + metadata so spend paths that drop the auth object still know the request came through a virtual key.""" return user_api_key_dict.via_virtual_key and not user_api_key_dict.is_session_token @staticmethod @@ -2090,9 +2089,6 @@ class LiteLLMProxyRequestSetup: ) data[_metadata_variable_name].update(user_api_key_logged_metadata) data[_metadata_variable_name]["user_api_key"] = LiteLLMProxyRequestSetup.get_logged_api_key(user_api_key_dict) - data[_metadata_variable_name][USER_API_KEY_IS_VIRTUAL_KEY_METADATA_KEY] = ( - LiteLLMProxyRequestSetup.authenticated_with_virtual_key(user_api_key_dict) - ) # Key-owned agent_id for spend attribution; keep existing (e.g. from header) if key has none _key_agent_id: Final = getattr(user_api_key_dict, "agent_id", None) diff --git a/litellm/types/utils.py b/litellm/types/utils.py index 0c3440e88f7..300dc3db57b 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -3067,6 +3067,7 @@ class StandardLoggingUserAPIKeyMetadata(TypedDict): user_api_key_end_user_id: str | None user_api_key_request_route: str | None user_api_key_auth_metadata: dict[str, str] | None + user_api_key_is_virtual_key: ReadOnly[NotRequired[bool]] class StandardLoggingMCPToolCall(TypedDict, total=False): diff --git a/tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py b/tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py index 93292a3a864..c34ccf9d446 100644 --- a/tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py +++ b/tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py @@ -711,6 +711,23 @@ async def test_create_dispatches_each_row_with_the_batch_model_and_the_key_metad assert metadata["user_api_key_user_email"] == "user@example.com" +@pytest.mark.parametrize(("via_virtual_key", "expected"), [(True, "team-1"), (False, "")]) +async def test_each_row_keeps_the_virtual_key_team_for_tag_spend(via_virtual_key: bool, expected: str) -> None: + from litellm.proxy.db.db_spend_update_writer import _virtual_key_team_id + + user = UserAPIKeyAuth(api_key="sk-batch-key", team_id="team-1").model_copy( + update={"via_virtual_key": via_virtual_key} + ) + harness = make_runner(user=user) + await harness.create_and_finish() + + teams = [ + _virtual_key_team_id({"litellm_params": {"metadata": call.kwargs["metadata"]}}) + for call in harness.router.acompletion.await_args_list + ] + assert teams == [expected] * 2 + + async def test_create_dispatches_each_row_with_the_key_team_and_project_tags_as_inherited_tags() -> None: user = UserAPIKeyAuth( api_key="sk-batch-key", diff --git a/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py b/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py index a9098ac3d59..fcc5ffbf0e2 100644 --- a/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py +++ b/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py @@ -7793,6 +7793,19 @@ def _passthrough_kwargs_for_reservation( ) +@pytest.mark.parametrize(("via_virtual_key", "expected"), [(True, "team-a"), (False, "")]) +def test_pass_through_tag_spend_keeps_the_virtual_key_team(via_virtual_key: bool, expected: str) -> None: + from litellm.proxy.db.db_spend_update_writer import _virtual_key_team_id + + auth: Final = UserAPIKeyAuth(api_key="a" * 64, team_id="team-a").model_copy( + update={"via_virtual_key": via_virtual_key} + ) + + kwargs: Final = _passthrough_kwargs_for_reservation(auth) + + assert _virtual_key_team_id(kwargs) == expected + + async def _track_cost_for_passthrough_kwargs(kwargs: dict) -> AsyncMock: from datetime import datetime diff --git a/tests/unit/proxy/test_proxy_utils.py b/tests/unit/proxy/test_proxy_utils.py index 1b9cdd003ab..5d48226191f 100644 --- a/tests/unit/proxy/test_proxy_utils.py +++ b/tests/unit/proxy/test_proxy_utils.py @@ -600,6 +600,7 @@ def test_foward_litellm_user_info_to_backend_llm_call(): "x-litellm-user_api_key_hash": "test_api_key", "x-litellm-user_api_key_spend": "0.0", "x-litellm-user_api_key_auth_metadata": "{}", + "x-litellm-user_api_key_is_virtual_key": "False", } assert json.dumps(data, sort_keys=True) == json.dumps(expected_data, sort_keys=True)