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
Inject credential name as tag in standard logging payload for Usage page filtering
yuneng-jiang8c5d48348c2b
diff --git a/litellm/litellm_core_utils/get_litellm_params.py b/litellm/litellm_core_utils/get_litellm_params.pyindex 36a8dfdb5a6..986782b2dfa 100644--- a/litellm/litellm_core_utils/get_litellm_params.py+++ b/litellm/litellm_core_utils/get_litellm_params.py@@ -32,6 +32,7 @@ _OPTIONAL_KWARGS_KEYS = frozenset({"aws_bedrock_runtime_endpoint","tpm","rpm",+ "litellm_credential_name",})diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.pyindex 76e37010109..c3e1642d72b 100644--- a/litellm/litellm_core_utils/litellm_logging.py+++ b/litellm/litellm_core_utils/litellm_logging.py@@ -5076,6 +5076,11 @@ def get_standard_logging_object_payload(litellm_params=litellm_params, proxy_server_request=proxy_server_request)+ # Inject credential name as tag for spend tracking+ credential_name = litellm_params.get("litellm_credential_name")+ if credential_name and credential_name not in request_tags:+ request_tags.append(credential_name)+# cleanup timestamps(start_time_float,diff --git a/tests/test_litellm/litellm_core_utils/test_get_litellm_params.py b/tests/test_litellm/litellm_core_utils/test_get_litellm_params.pyindex dbcb048c250..77e2c1bd37f 100644--- a/tests/test_litellm/litellm_core_utils/test_get_litellm_params.py+++ b/tests/test_litellm/litellm_core_utils/test_get_litellm_params.py@@ -125,3 +125,13 @@ class TestGetLitellmParamsExplicitFields:def test_no_log_from_explicit_param(self):result = get_litellm_params(no_log=True)assert result["no-log"] is True++ def test_litellm_credential_name_captured(self):+ """litellm_credential_name should be captured via _OPTIONAL_KWARGS_KEYS."""+ result = get_litellm_params(litellm_credential_name="my-credential")+ assert result["litellm_credential_name"] == "my-credential"++ def test_litellm_credential_name_absent(self):+ """When litellm_credential_name is not passed, it should not appear."""+ result = get_litellm_params()+ assert "litellm_credential_name" not in resultdiff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.pyindex 734d52918ba..35fd710f910 100644--- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py+++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py@@ -1355,3 +1355,204 @@ def test_get_error_information_error_code_priority():result = StandardLoggingPayloadSetup.get_error_information(no_code_exception)assert result["error_code"] == ""assert result["error_class"] == "NoCodeException"+++def test_credential_name_injected_as_tag():+ """+ Test that litellm_credential_name from litellm_params is injected into+ request_tags in the standard logging payload.++ In the real flow, litellm_credential_name is captured into litellm_params+ by get_litellm_params() via _OPTIONAL_KWARGS_KEYS.+ """+ from litellm.litellm_core_utils.litellm_logging import (+ get_standard_logging_object_payload,+ Logging,+ )+ from datetime import datetime++ logging_obj = Logging(+ model="gpt-4o",+ messages=[{"role": "user", "content": "Hello"}],+ stream=False,+ call_type="completion",+ start_time=datetime.now(),+ litellm_call_id="test-cred-tag",+ function_id="test-function",+ )++ mock_response = {+ "id": "chatcmpl-123",+ "object": "chat.completion",+ "model": "gpt-4o",+ "usage": {+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "total_tokens": 30,+ },+ "choices": [+ {+ "index": 0,+ "message": {"role": "assistant", "content": "Hi!"},+ "finish_reason": "stop",+ }+ ],+ }++ kwargs = {+ "model": "gpt-4o",+ "messages": [{"role": "user", "content": "Hello"}],+ "response_cost": 0.001,+ "custom_llm_provider": "openai",+ "litellm_params": {+ "litellm_credential_name": "my-openai-credential",+ },+ }++ start_time = datetime.now()+ end_time = datetime.now()++ payload = get_standard_logging_object_payload(+ kwargs=kwargs,+ init_response_obj=mock_response,+ start_time=start_time,+ end_time=end_time,+ logging_obj=logging_obj,+ status="success",+ )++ assert payload is not None+ assert "my-openai-credential" in payload["request_tags"]+++def test_credential_name_not_injected_when_absent():+ """+ Test that when litellm_credential_name is not in litellm_params,+ request_tags are unchanged.+ """+ from litellm.litellm_core_utils.litellm_logging import (+ get_standard_logging_object_payload,+ Logging,+ )+ from datetime import datetime++ logging_obj = Logging(+ model="gpt-4o",+ messages=[{"role": "user", "content": "Hello"}],+ stream=False,+ call_type="completion",+ start_time=datetime.now(),+ litellm_call_id="test-no-cred",+ function_id="test-function",+ )++ mock_response = {+ "id": "chatcmpl-456",+ "object": "chat.completion",+ "model": "gpt-4o",+ "usage": {+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "total_tokens": 30,+ },+ "choices": [+ {+ "index": 0,+ "message": {"role": "assistant", "content": "Hi!"},+ "finish_reason": "stop",+ }+ ],+ }++ kwargs = {+ "model": "gpt-4o",+ "messages": [{"role": "user", "content": "Hello"}],+ "response_cost": 0.001,+ "custom_llm_provider": "openai",+ }++ start_time = datetime.now()+ end_time = datetime.now()++ payload = get_standard_logging_object_payload(+ kwargs=kwargs,+ init_response_obj=mock_response,+ start_time=start_time,+ end_time=end_time,+ logging_obj=logging_obj,+ status="success",+ )++ assert payload is not None+ # No credential-related tags should be present+ for tag in payload["request_tags"]:+ assert tag.startswith("User-Agent:")+++def test_credential_name_not_duplicated_in_tags():+ """+ Test that if the credential name already exists in the tags list,+ it is not duplicated.+ """+ from litellm.litellm_core_utils.litellm_logging import (+ get_standard_logging_object_payload,+ Logging,+ )+ from datetime import datetime++ logging_obj = Logging(+ model="gpt-4o",+ messages=[{"role": "user", "content": "Hello"}],+ stream=False,+ call_type="completion",+ start_time=datetime.now(),+ litellm_call_id="test-dup-cred",+ function_id="test-function",+ )++ mock_response = {+ "id": "chatcmpl-789",+ "object": "chat.completion",+ "model": "gpt-4o",+ "usage": {+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "total_tokens": 30,+ },+ "choices": [+ {+ "index": 0,+ "message": {"role": "assistant", "content": "Hi!"},+ "finish_reason": "stop",+ }+ ],+ }++ kwargs = {+ "model": "gpt-4o",+ "messages": [{"role": "user", "content": "Hello"}],+ "response_cost": 0.001,+ "custom_llm_provider": "openai",+ "litellm_params": {+ "litellm_credential_name": "my-openai-credential",+ "metadata": {"tags": ["my-openai-credential", "other-tag"]},+ },+ }++ start_time = datetime.now()+ end_time = datetime.now()++ payload = get_standard_logging_object_payload(+ kwargs=kwargs,+ init_response_obj=mock_response,+ start_time=start_time,+ end_time=end_time,+ logging_obj=logging_obj,+ status="success",+ )++ assert payload is not None+ credential_count = payload["request_tags"].count("my-openai-credential")+ assert credential_count == 1, (+ f"Expected credential name once, found {credential_count} times"+ )