Skip to content

Conversation

@BCook98
Copy link
Contributor

@BCook98 BCook98 commented Dec 3, 2025

Title

(Fix) Prometheus time-to-first-token metric to use dynamic labels

Relevant issues

N/A

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

Updated the _set_latency_metrics method to use the prometheus_label_factory for the time-to-first-token metric, ensuring it respects the configured metric labels instead of using hardcoded parameters.

Without this recording this metric results in an error

{"message": "LiteLLM.LoggingError: [Non-Blocking] Exception occurred while success logging Traceback (most recent call last):\n  File \"/app/litellm/litellm_core_utils/litellm_logging.py\", line 2409, in async_success_handler\n    await callback.async_log_success_event(\n    ...<6 lines>...\n    )\n  File \"/app/litellm/integrations/prometheus.py\", line 899, in async_log_success_event\n    self._set_latency_metrics(\n    ~~~~~~~~~~~~~~~~~~~~~~~~~^\n        kwargs=kwargs,\n        ^^^^^^^^^^^^^^\n    ...<8 lines>...\n        enum_values=enum_values,\n        ^^^^^^^^^^^^^^^^^^^^^^^^\n    )\n    ^\n  File \"/app/litellm/integrations/prometheus.py\", line 1111, in _set_latency_metrics\n    self.litellm_llm_api_time_to_first_token_metric.labels(\n    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^\n        model,\n        ^^^^^^\n    ...<3 lines>...\n        user_api_team_alias,\n        ^^^^^^^^^^^^^^^^^^^^\n    ).observe(time_to_first_token_seconds)\n    ^\n  File \"/usr/lib/python3.13/site-packages/prometheus_client/metrics.py\", line 199, in labels\n    raise ValueError('Incorrect label count')\nValueError: Incorrect label count\n", "level": "ERROR", "timestamp": "2025-12-03T09:53:01.562188"}

This change:

  • Removes unused parameters (model, user_api_key, user_api_key_alias, user_api_team, user_api_team_alias) from _set_latency_metrics
  • Uses prometheus_label_factory to generate labels dynamically based on the metric's supported labels
  • Ensures consistency with other metrics in the PrometheusLogger class
image

BCook98 and others added 4 commits December 3, 2025 21:35
Updated the `_set_latency_metrics` method to use the `prometheus_label_factory`
for the time-to-first-token metric, ensuring it respects the configured
metric labels instead of using hardcoded parameters.

This change:
- Removes unused parameters (model, user_api_key, user_api_key_alias,
  user_api_team, user_api_team_alias) from `_set_latency_metrics`
- Uses `prometheus_label_factory` to generate labels dynamically based on
  the metric's supported labels
- Ensures consistency with other metrics in the PrometheusLogger class

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Added test to verify that the time-to-first-token metric uses
prometheus_label_factory for dynamic label generation instead of
hardcoded parameters.

The test ensures that:
- The metric is properly registered
- Metric samples are recorded during streaming requests
- Labels are configured dynamically via get_labels_for_metric

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Added test to verify that the time-to-first-token metric properly uses
dynamic label filtering via prometheus_label_factory.

The test:
- Configures the metric to only include specific labels (model, hashed_api_key, api_key_alias)
- Excludes team and team_alias labels
- Verifies that excluded labels do not appear in the recorded metrics
- Ensures that included labels are present

This validates that the recent fix to use prometheus_label_factory
correctly respects the configured metric labels.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@vercel
Copy link

vercel bot commented Dec 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Ready Ready Preview Comment Dec 3, 2025 10:56am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant