mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(proxy): carry the virtual-key flag in the key's logged metadata
Pass-through routes and LiteLLM-run batches build their spend metadata from get_sanitized_user_information_from_key without the flag, so their tag spend from team keys landed under no team. The flag now lives in that shared helper, which every metadata builder already calls, instead of being set separately in pre-call and the failure hook
This commit is contained in:
parent
8f567b63f0
commit
6f251a0652
6 changed files with 36 additions and 15 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue