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(prometheus.py): fix total token increment + add semantic tests for prometheus metrics (#11718)
Krish Dholakia7df76175b4aa
diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.pyindex 28f73c5874b..96ba23fb9cc 100644--- a/litellm/integrations/prometheus.py+++ b/litellm/integrations/prometheus.py@@ -558,10 +558,6 @@ class PrometheusLogger(CustomLogger):enum_values=enum_values,)- self.litellm_proxy_total_requests_metric.labels(**_labels).inc(- standard_logging_payload["total_tokens"]- )-_labels = prometheus_label_factory(supported_enum_labels=PrometheusMetricLabels.get_labels(label_name="litellm_total_tokens_metric"@@ -649,8 +645,18 @@ class PrometheusLogger(CustomLogger):),enum_values=enum_values,)+self.litellm_requests_metric.labels(**_labels).inc()+ _labels = prometheus_label_factory(+ supported_enum_labels=PrometheusMetricLabels.get_labels(+ label_name="litellm_proxy_total_requests_metric"+ ),+ enum_values=enum_values,+ )++ self.litellm_proxy_total_requests_metric.labels(**_labels).inc()+self.litellm_spend_metric.labels(end_user_id,user_api_key,diff --git a/tests/test_litellm/integrations/test_prometheus.py b/tests/test_litellm/integrations/test_prometheus.pyindex 1a79e0a98d2..fe57e4fc7bb 100644--- a/tests/test_litellm/integrations/test_prometheus.py+++ b/tests/test_litellm/integrations/test_prometheus.py@@ -5,6 +5,7 @@ Mock prometheus unit tests, these don't rely on LLM API callsimport jsonimport osimport sys+import pytestfrom fastapi.testclient import TestClient@@ -12,7 +13,11 @@ sys.path.insert(0, os.path.abspath("../../..")) # Adds the parent directory to the system path+from unittest.mock import patch++import pytest_asynciofrom apscheduler.schedulers.asyncio import AsyncIOScheduler+from prometheus_client import REGISTRYimport litellmfrom litellm.constants import PROMETHEUS_BUDGET_METRICS_REFRESH_INTERVAL_MINUTES@@ -182,3 +187,350 @@ def test_future_metrics_with_end_user_are_filtered():finally:# Restore original settinglitellm.enable_end_user_cost_tracking_prometheus_only = original_setting+++# ==============================================================================+# SEMANTIC VALIDATION TESTS - Detect logical errors in metric increments+# ==============================================================================+++class MockCounter:+ """Mock counter for testing metric increments"""++ def __init__(self, name):+ self.name = name+ self.labels_calls = []+ self.inc_calls = []++ def labels(self, *args, **kwargs):+ self.labels_calls.append(kwargs)+ return self++ def inc(self, value=1):+ self.inc_calls.append(value)+++class MockHistogram:+ """Mock histogram for testing metric observations"""++ def __init__(self, name):+ self.name = name+ self.labels_calls = []+ self.observe_calls = []++ def labels(self, *args, **kwargs):+ self.labels_calls.append(kwargs)+ return self++ def observe(self, value):+ self.observe_calls.append(value)+++def mock_prometheus_logger():+ """Create a PrometheusLogger with mocked metrics to test increment logic"""+ from unittest.mock import patch++ collectors = list(REGISTRY._collector_to_names.keys())+ for collector in collectors:+ REGISTRY.unregister(collector)++ with patch("litellm.proxy.proxy_server.premium_user", True):+ logger = PrometheusLogger()++ # Replace metrics with mocks to capture increment calls+ logger.litellm_proxy_total_requests_metric = MockCounter(+ "litellm_proxy_total_requests_metric"+ )+ logger.litellm_tokens_metric = MockCounter("litellm_total_tokens")+ logger.litellm_input_tokens_metric = MockCounter("litellm_input_tokens")+ logger.litellm_output_tokens_metric = MockCounter("litellm_output_tokens")+ logger.litellm_spend_metric = MockCounter("litellm_spend_metric")+ logger.litellm_requests_metric = MockCounter("litellm_requests_metric")++ return logger+++async def test_request_counter_semantic_validation(mock_prometheus_logger):+ """+ CRITICAL TEST: Validates that request counters are incremented by 1, not by token count.+ This test specifically catches the bug where litellm_proxy_total_requests_metric+ is incorrectly incremented by total_tokens instead of 1.+ """+ from datetime import datetime, timedelta++ # Test data with large token count that should NOT affect request counter+ kwargs = {+ "model": "gpt-3.5-turbo",+ "litellm_params": {"metadata": {}},+ "start_time": datetime.now() - timedelta(seconds=1),+ "end_time": datetime.now(),+ "api_call_start_time": datetime.now() - timedelta(seconds=0.5),+ "standard_logging_object": {+ "total_tokens": 999, # Large number - this should NOT be used for request counter+ "prompt_tokens": 600,+ "completion_tokens": 399,+ "response_cost": 0.005,+ "model_group": "gpt-3.5-turbo",+ "model_id": "test-model-id",+ "api_base": "https://api.openai.com/v1",+ "custom_llm_provider": "openai",+ "stream": False,+ "request_tags": [],+ "metadata": {+ "user_api_key_user_id": "test-user",+ "user_api_key_hash": "test-hash",+ "user_api_key_alias": "test-alias",+ "user_api_key_team_id": "test-team",+ "user_api_key_team_alias": "test-team-alias",+ "user_api_key_user_email": "[email protected]",+ },+ "hidden_params": {+ "additional_headers": {},+ },+ },+ }++ # Call the success event+ await mock_prometheus_logger.async_log_success_event(+ kwargs, None, kwargs["start_time"], kwargs["end_time"]+ )++ # CRITICAL ASSERTION: Request counter should be incremented by 1, NOT by token count+ total_requests_metric = mock_prometheus_logger.litellm_proxy_total_requests_metric++ assert (+ len(total_requests_metric.inc_calls) > 0+ ), "Request metric should be incremented"++ # Check that ALL request counter increments are by 1 (not by token count)+ for inc_value in total_requests_metric.inc_calls:+ assert inc_value == 1, (+ f"SEMANTIC BUG DETECTED: Request counter incremented by {inc_value} instead of 1. "+ f"This indicates the bug where request counters are incremented by token counts."+ )++ # Verify token counters ARE incremented by token counts (this should work correctly)+ tokens_metric = mock_prometheus_logger.litellm_tokens_metric+ assert (+ 999 in tokens_metric.inc_calls+ ), "Token metric should be incremented by total_tokens (999)"+++async def test_multiple_requests_counter_semantics(mock_prometheus_logger):+ """+ Test that demonstrates the scaling issue: with multiple requests,+ request counters should scale by number of requests, not total tokens.+ """+ from datetime import datetime, timedelta++ num_requests = 3+ tokens_per_request = 500 # High token count to make the bug obvious++ for i in range(num_requests):+ kwargs = {+ "model": "gpt-3.5-turbo",+ "litellm_params": {"metadata": {}},+ "start_time": datetime.now() - timedelta(seconds=1),+ "end_time": datetime.now(),+ "api_call_start_time": datetime.now() - timedelta(seconds=0.5),+ "standard_logging_object": {+ "total_tokens": tokens_per_request,+ "prompt_tokens": tokens_per_request // 2,+ "completion_tokens": tokens_per_request // 2,+ "response_cost": 0.001,+ "model_group": "gpt-3.5-turbo",+ "model_id": "test-model-id",+ "api_base": "https://api.openai.com/v1",+ "custom_llm_provider": "openai",+ "stream": False,+ "request_tags": [],+ "metadata": {+ "user_api_key_user_id": "test-user",+ "user_api_key_hash": "test-hash",+ "user_api_key_alias": "test-alias",+ "user_api_key_team_id": "test-team",+ "user_api_key_team_alias": "test-team-alias",+ "user_api_key_user_email": "[email protected]",+ },+ "hidden_params": {+ "additional_headers": {},+ },+ },+ }++ await mock_prometheus_logger.async_log_success_event(+ kwargs, None, kwargs["start_time"], kwargs["end_time"]+ )++ # Calculate total increments+ total_request_increments = sum(+ mock_prometheus_logger.litellm_proxy_total_requests_metric.inc_calls+ )+ total_token_increments = sum(mock_prometheus_logger.litellm_tokens_metric.inc_calls)++ # CRITICAL ASSERTION: Request increments should equal number of requests+ expected_total_tokens = num_requests * tokens_per_request # 3 * 500 = 1500++ # With the bug, total_request_increments would be 1500 instead of 3+ assert total_request_increments == num_requests, (+ f"SEMANTIC BUG: Request counter total increments = {total_request_increments}, "+ f"expected {num_requests}. This suggests request counters are being incremented "+ f"by token counts instead of request counts."+ )++ # Token counter should correctly equal total tokens+ assert (+ total_token_increments == expected_total_tokens+ ), f"Token counter should sum to {expected_total_tokens}, got {total_token_increments}"+++async def test_streaming_request_counter_semantics(mock_prometheus_logger):+ """+ Test that streaming requests are also counted correctly (by 1, not by token count)+ """+ from datetime import datetime, timedelta++ kwargs = {+ "model": "gpt-3.5-turbo",+ "litellm_params": {"metadata": {}},+ "start_time": datetime.now() - timedelta(seconds=1),+ "end_time": datetime.now(),+ "api_call_start_time": datetime.now() - timedelta(seconds=0.5),+ "standard_logging_object": {+ "total_tokens": 750, # High token count for streaming+ "prompt_tokens": 300,+ "completion_tokens": 450,+ "response_cost": 0.003,+ "model_group": "gpt-3.5-turbo",+ "model_id": "test-model-id",+ "api_base": "https://api.openai.com/v1",+ "custom_llm_provider": "openai",+ "stream": True, # This is a streaming request+ "request_tags": [],+ "metadata": {+ "user_api_key_user_id": "test-user",+ "user_api_key_hash": "test-hash",+ "user_api_key_alias": "test-alias",+ "user_api_key_team_id": "test-team",+ "user_api_key_team_alias": "test-team-alias",+ "user_api_key_user_email": "[email protected]",+ },+ "hidden_params": {+ "additional_headers": {},+ },+ },+ }++ await mock_prometheus_logger.async_log_success_event(+ kwargs, None, kwargs["start_time"], kwargs["end_time"]+ )++ # Streaming requests should also be counted as 1 request, not 750+ for (+ inc_value+ ) in mock_prometheus_logger.litellm_proxy_total_requests_metric.inc_calls:+ assert (+ inc_value == 1+ ), f"SEMANTIC BUG: Streaming request counter incremented by {inc_value} instead of 1"+++def test_metric_increment_invariants():+ """+ Test invariants that should always hold for different metric types+ """+ # Invariant 1: Request counters should never be incremented by large values+ suspicious_request_increments = [+ 100,+ 500,+ 1000,+ 1500,+ ] # These look like token counts+ for increment in suspicious_request_increments:+ # If we see request counters incremented by these values, it's likely a bug+ assert (+ increment > 10+ ), f"Request increment of {increment} is suspiciously large - likely a semantic bug"++ # Invariant 2: Token counters should never be incremented by 1 (unless it's a 1-token response)+ # This would indicate the reverse bug (using request count for token counter)++ # Invariant 3: Cost increments should be small positive floats+ reasonable_costs = [0.001, 0.01, 0.1, 1.0]+ for cost in reasonable_costs:+ assert 0 < cost < 100, f"Cost {cost} should be in reasonable range"+++def test_token_counter_semantics():+ """+ Test that token counters should be incremented by actual token values, not by 1+ """+ # These are correct patterns for token counters+ correct_token_increments = [50, 100, 250, 500, 1000, 2000]++ for tokens in correct_token_increments:+ # Token counters should be incremented by actual token counts+ assert tokens > 1, f"Token increment of {tokens} is reasonable"++ # These would be incorrect for token counters (suggests using request count for tokens)+ incorrect_token_increments = [1] # Unless it's actually a 1-token response++ # This test documents the expected behavior - token counters should use token values+++async def test_spend_counter_semantics(mock_prometheus_logger):+ """+ Test that spend counters are incremented by cost amounts, not by 1 or token counts+ """+ from datetime import datetime, timedelta++ kwargs = {+ "model": "gpt-3.5-turbo",+ "litellm_params": {"metadata": {}},+ "start_time": datetime.now() - timedelta(seconds=1),+ "end_time": datetime.now(),+ "api_call_start_time": datetime.now() - timedelta(seconds=0.5),+ "standard_logging_object": {+ "total_tokens": 100,+ "prompt_tokens": 60,+ "completion_tokens": 40,+ "response_cost": 0.0015, # This should be used for spend metrics+ "model_group": "gpt-3.5-turbo",+ "model_id": "test-model-id",+ "api_base": "https://api.openai.com/v1",+ "custom_llm_provider": "openai",+ "stream": False,+ "request_tags": [],+ "metadata": {+ "user_api_key_user_id": "test-user",+ "user_api_key_hash": "test-hash",+ "user_api_key_alias": "test-alias",+ "user_api_key_team_id": "test-team",+ "user_api_key_team_alias": "test-team-alias",+ "user_api_key_user_email": "[email protected]",+ },+ "hidden_params": {+ "additional_headers": {},+ },+ },+ }++ await mock_prometheus_logger.async_log_success_event(+ kwargs, None, kwargs["start_time"], kwargs["end_time"]+ )++ # Verify spend counter is incremented by cost amount+ spend_metric = mock_prometheus_logger.litellm_spend_metric+ assert len(spend_metric.inc_calls) > 0, "Spend metric should be incremented"+ assert (+ 0.0015 in spend_metric.inc_calls+ ), "Spend metric should be incremented by response_cost (0.0015)"+++# ==============================================================================+# END SEMANTIC VALIDATION TESTS+# ==============================================================================