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 remaining metric zero values (#27348)
ishaan-berri854456f58ef6
diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.pyindex 65b8a9fc4b1..f9b1c666439 100644--- a/litellm/integrations/prometheus.py+++ b/litellm/integrations/prometheus.py@@ -1443,12 +1443,12 @@ class PrometheusLogger(CustomLogger):)remaining_tokens_variable_name = f"litellm-key-remaining-tokens-{model_group}"- remaining_requests = (- metadata.get(remaining_requests_variable_name, sys.maxsize) or sys.maxsize- )- remaining_tokens = (- metadata.get(remaining_tokens_variable_name, sys.maxsize) or sys.maxsize- )+ remaining_requests = metadata.get(remaining_requests_variable_name)+ if remaining_requests is None:+ remaining_requests = sys.maxsize+ remaining_tokens = metadata.get(remaining_tokens_variable_name)+ if remaining_tokens is None:+ remaining_tokens = sys.maxsizeenum_values = UserAPIKeyLabelValues(hashed_api_key=user_api_key,diff --git a/tests/test_litellm/integrations/test_prometheus_custom_metadata_label_counts.py b/tests/test_litellm/integrations/test_prometheus_custom_metadata_label_counts.pyindex 05d1eab8cc8..99eb5abb7b5 100644--- a/tests/test_litellm/integrations/test_prometheus_custom_metadata_label_counts.py+++ b/tests/test_litellm/integrations/test_prometheus_custom_metadata_label_counts.py@@ -1,4 +1,5 @@import logging+import sysimport pytestfrom prometheus_client import REGISTRY@@ -123,3 +124,36 @@ def test_virtual_key_rate_limit_metrics_accept_custom_metadata_labels(and sample.value == 3for sample in samples)+++def test_virtual_key_rate_limit_metrics_preserve_zero_remaining_values(+ monkeypatch: pytest.MonkeyPatch,+):+ prometheus_logger = _create_prometheus_logger_with_custom_labels(monkeypatch)+ metadata = {+ "model_group": "gpt-4o-mini",+ "litellm-key-remaining-requests-gpt-4o-mini": 0,+ "litellm-key-remaining-tokens-gpt-4o-mini": 0,+ }+ kwargs = {+ "litellm_params": {+ "metadata": metadata,+ },+ "standard_logging_object": _standard_logging_payload_with_requester_metadata(),+ }++ prometheus_logger._set_virtual_key_rate_limit_metrics(+ user_api_key="test-hash",+ user_api_key_alias="test-alias",+ kwargs=kwargs,+ metadata=metadata,+ model_id="model-123",+ )++ request_samples = _metric_samples("litellm_remaining_api_key_requests_for_model")+ token_samples = _metric_samples("litellm_remaining_api_key_tokens_for_model")++ assert any(sample.value == 0 for sample in request_samples)+ assert any(sample.value == 0 for sample in token_samples)+ assert not any(sample.value == sys.maxsize for sample in request_samples)+ assert not any(sample.value == sys.maxsize for sample in token_samples)