Skip to content

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]

feat: add user email to cloudzero

Yuta Saitoda05b56756d0
diff --git a/litellm/integrations/cloudzero/cloudzero.py b/litellm/integrations/cloudzero/cloudzero.py
index 403829deba0..9da8ea52b5c 100644
--- a/litellm/integrations/cloudzero/cloudzero.py
+++ b/litellm/integrations/cloudzero/cloudzero.py
@@ -317,6 +317,7 @@ class CloudZeroLogger(CustomLogger):
)
cbf_table.add_column("team_id", style="cyan", no_wrap=False)
cbf_table.add_column("team_alias", style="cyan", no_wrap=False)
+ cbf_table.add_column("user_email", style="cyan", no_wrap=False)
cbf_table.add_column("api_key_alias", style="yellow", no_wrap=False)
cbf_table.add_column(
"usage/amount", style="yellow", justify="right", no_wrap=False
@@ -339,6 +340,7 @@ class CloudZeroLogger(CustomLogger):
entity_id = str(record.get("entity_id", "N/A"))
team_id = str(record.get("resource/tag:team_id", "N/A"))
team_alias = str(record.get("resource/tag:team_alias", "N/A"))
+ user_email = str(record.get("resource/tag:user_email", "N/A"))
api_key_alias = str(record.get("resource/tag:api_key_alias", "N/A"))
cbf_table.add_row(
@@ -348,6 +350,7 @@ class CloudZeroLogger(CustomLogger):
entity_id,
team_id,
team_alias,
+ user_email,
api_key_alias,
usage_amount,
resource_id,
diff --git a/litellm/integrations/cloudzero/database.py b/litellm/integrations/cloudzero/database.py
index 83ca01a5c0e..2128b55bf83 100644
--- a/litellm/integrations/cloudzero/database.py
+++ b/litellm/integrations/cloudzero/database.py
@@ -79,10 +79,12 @@ class LiteLLMDatabase:
dus.updated_at,
vt.team_id,
vt.key_alias as api_key_alias,
- tt.team_alias
+ tt.team_alias,
+ ut.user_email as user_email
FROM "LiteLLM_DailyUserSpend" dus
LEFT JOIN "LiteLLM_VerificationToken" vt ON dus.api_key = vt.token
LEFT JOIN "LiteLLM_TeamTable" tt ON vt.team_id = tt.team_id
+ LEFT JOIN "LiteLLM_UserTable" ut ON dus.user_id = ut.user_id
{where_clause}
ORDER BY dus.date DESC, dus.created_at DESC
"""
diff --git a/litellm/integrations/cloudzero/transform.py b/litellm/integrations/cloudzero/transform.py
index e0263295388..e06b944a419 100644
--- a/litellm/integrations/cloudzero/transform.py
+++ b/litellm/integrations/cloudzero/transform.py
@@ -98,6 +98,7 @@ class CBFTransformer:
# Handle team information with fallbacks
team_id = row.get('team_id')
team_alias = row.get('team_alias')
+ user_email = row.get('user_email')
# Use team_alias if available, otherwise team_id, otherwise fallback to 'unknown'
entity_id = str(team_alias) if team_alias else (str(team_id) if team_id else 'unknown')
@@ -112,6 +113,7 @@ class CBFTransformer:
'provider': str(row.get('custom_llm_provider', '')),
'api_key_prefix': api_key_hash,
'api_key_alias': str(row.get('api_key_alias', '')),
+ 'user_email': str(user_email) if user_email else '',
'api_requests': str(row.get('api_requests', 0)),
'successful_requests': str(row.get('successful_requests', 0)),
'failed_requests': str(row.get('failed_requests', 0)),
@@ -184,4 +186,3 @@ class CBFTransformer:
return None
-
diff --git a/tests/test_litellm/integrations/cloudzero/test_dry_run_endpoint.py b/tests/test_litellm/integrations/cloudzero/test_dry_run_endpoint.py
index 5ba6457f376..97daaa32557 100644
--- a/tests/test_litellm/integrations/cloudzero/test_dry_run_endpoint.py
+++ b/tests/test_litellm/integrations/cloudzero/test_dry_run_endpoint.py
@@ -32,6 +32,7 @@ class TestCloudZeroDryRunEndpoint:
'team_id': ['team1', 'team2'],
'team_alias': ['Team One', 'Team Two'],
'api_key_alias': ['key1', 'key2'],
+ 'user_email': ['[email protected]', None],
'prompt_tokens': [100, 200],
'completion_tokens': [50, 100],
'spend': [0.01, 0.02],
@@ -51,7 +52,8 @@ class TestCloudZeroDryRunEndpoint:
'entity_id': ['team1', 'team2'],
'resource/tag:team_id': ['team1', 'team2'],
'resource/tag:team_alias': ['Team One', 'Team Two'],
- 'resource/tag:api_key_alias': ['key1', 'key2']
+ 'resource/tag:api_key_alias': ['key1', 'key2'],
+ 'resource/tag:user_email': ['[email protected]', 'N/A']
})
with patch('litellm.integrations.cloudzero.database.LiteLLMDatabase') as mock_db_class, \
@@ -86,6 +88,7 @@ class TestCloudZeroDryRunEndpoint:
assert len(result['cbf_data']) == 2
assert result['cbf_data'][0]['cost/cost'] == 0.01
assert result['cbf_data'][1]['cost/cost'] == 0.02
+ assert result['cbf_data'][0]['resource/tag:user_email'] == '[email protected]'
# Verify summary
summary = result['summary']
@@ -122,4 +125,3 @@ class TestCloudZeroDryRunEndpoint:
assert result['summary']['total_records'] == 0
assert result['summary']['total_cost'] == 0
assert result['summary']['total_tokens'] == 0
-
diff --git a/tests/test_litellm/integrations/cloudzero/test_transform.py b/tests/test_litellm/integrations/cloudzero/test_transform.py
index 1f4db10cab8..468f96ece1d 100644
--- a/tests/test_litellm/integrations/cloudzero/test_transform.py
+++ b/tests/test_litellm/integrations/cloudzero/test_transform.py
@@ -116,6 +116,56 @@ class TestCBFTransformer:
assert result['usage/units'] == 'tokens'
assert result['resource/id'] == 'test-czrn'
+ def test_create_cbf_record_adds_user_email_tag(self):
+ """Test that user_email field is emitted as a resource tag when present."""
+ transformer = CBFTransformer()
+ with patch.object(transformer.czrn_generator, 'create_from_litellm_data') as mock_czrn, \
+ patch.object(transformer.czrn_generator, 'extract_components') as mock_extract:
+
+ mock_czrn.return_value = 'test-czrn'
+ mock_extract.return_value = ('service', 'provider', 'region', 'account', 'resource', 'local_id')
+
+ row = {
+ 'date': '2025-01-19',
+ 'spend': 1.0,
+ 'prompt_tokens': 10,
+ 'completion_tokens': 5,
+ 'model': 'gpt-4',
+ 'api_key': 'sk-useremail',
+ 'team_id': 'team-123',
+ 'team_alias': 'Dev Team',
+ 'user_email': '[email protected]'
+ }
+
+ result = transformer._create_cbf_record(row)
+
+ assert result['resource/tag:user_email'] == '[email protected]'
+
+ def test_create_cbf_record_omits_empty_user_email(self):
+ """Test that empty user_email values are not added as resource tags."""
+ transformer = CBFTransformer()
+ with patch.object(transformer.czrn_generator, 'create_from_litellm_data') as mock_czrn, \
+ patch.object(transformer.czrn_generator, 'extract_components') as mock_extract:
+
+ mock_czrn.return_value = 'test-czrn'
+ mock_extract.return_value = ('service', 'provider', 'region', 'account', 'resource', 'local_id')
+
+ row = {
+ 'date': '2025-01-19',
+ 'spend': 1.0,
+ 'prompt_tokens': 10,
+ 'completion_tokens': 5,
+ 'model': 'gpt-4',
+ 'api_key': 'sk-useremail',
+ 'team_id': 'team-123',
+ 'team_alias': 'Dev Team',
+ 'user_email': None
+ }
+
+ result = transformer._create_cbf_record(row)
+
+ assert 'resource/tag:user_email' not in result
+
def test_create_cbf_record_minimal_data(self):
"""Test _create_cbf_record method with minimal row data."""
transformer = CBFTransformer()
@@ -180,4 +230,4 @@ class TestCBFTransformer:
result = transformer._parse_date('2025-01-19T10:30:00Z')
assert isinstance(result, datetime)
- assert result.year == 2025
\ No newline at end of file
+ assert result.year == 2025