Skip to content

feat(prometheus): add requested_model label to spend and requests metrics - #31410

Merged
yucheng-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_lit_3796_requested_model
Jun 26, 2026
Merged

feat(prometheus): add requested_model label to spend and requests metrics#31410
yucheng-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_lit_3796_requested_model

Conversation

@yucheng-berri

@yucheng-berri yucheng-berri commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Relevant issues

Linear ticket

Resolves LIT-3796

Pre-Submission checklist

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

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Screenshots / Proof of Fix

Local proxy with two deployments behind one alias, real OpenAI traffic:

model_list:
  - model_name: aliased-fast-model
    litellm_params: {model: openai/gpt-4o-mini, api_key: os.environ/OPENAI_API_KEY}
    model_info: {id: openai/gpt-4o-mini-deployment-A}
  - model_name: aliased-fast-model
    litellm_params: {model: openai/gpt-4o-mini, api_key: os.environ/OPENAI_API_KEY}
    model_info: {id: openai/gpt-4o-mini-deployment-B}
litellm_settings: {callbacks: ["prometheus"]}
general_settings: {master_key: sk-1234}
for i in 1 2 3 4 5; do
  curl -sS http://localhost:4000/v1/chat/completions \
    -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" \
    -d '{"model":"aliased-fast-model","messages":[{"role":"user","content":"say hi in 1 word"}]}' > /dev/null
done
curl -sSL http://localhost:4000/metrics -H "Authorization: Bearer sk-1234" \
  | grep -E '^litellm_(spend|requests)_metric_total'

Before this PR (note: no requested_model label):

litellm_spend_metric_total{api_provider="openai",...,model="gpt-4o-mini",model_id="openai/gpt-4o-mini-deployment-A",...} 1.26e-05
litellm_spend_metric_total{api_provider="openai",...,model="gpt-4o-mini",model_id="openai/gpt-4o-mini-deployment-B",...} 3.15e-06
litellm_requests_metric_total{api_provider="openai",...,model="gpt-4o-mini",model_id="openai/gpt-4o-mini-deployment-A",...} 4.0
litellm_requests_metric_total{api_provider="openai",...,model="gpt-4o-mini",model_id="openai/gpt-4o-mini-deployment-B",...} 1.0

After this PR (note: requested_model="aliased-fast-model" now appears on every row, all other labels unchanged):

litellm_spend_metric_total{api_provider="openai",...,model="gpt-4o-mini",model_id="openai/gpt-4o-mini-deployment-A",requested_model="aliased-fast-model",...} 6.3e-06
litellm_spend_metric_total{api_provider="openai",...,model="gpt-4o-mini",model_id="openai/gpt-4o-mini-deployment-B",requested_model="aliased-fast-model",...} 9.45e-06
litellm_requests_metric_total{api_provider="openai",...,model="gpt-4o-mini",model_id="openai/gpt-4o-mini-deployment-A",requested_model="aliased-fast-model",...} 2.0
litellm_requests_metric_total{api_provider="openai",...,model="gpt-4o-mini",model_id="openai/gpt-4o-mini-deployment-B",requested_model="aliased-fast-model",...} 3.0

Adding a label is backward compatible for Prometheus and for any existing sum() or sum by (existing_label) queries; only queries that explicitly enumerate the full label set change shape

Type

🐛 Bug Fix

Changes

litellm_spend_metric and litellm_requests_metric both gained UserAPIKeyLabelNames.REQUESTED_MODEL in their labelnames list (litellm/types/integrations/prometheus.py). The value is already populated on UserAPIKeyLabelValues.requested_model upstream from standard_logging_payload["model_group"], and the shared _increment_top_level_request_and_spend_metrics call site reads it through the existing helper, so no plumbing changes are needed. The sibling token metrics (litellm_input_tokens_metric, litellm_output_tokens_metric, litellm_total_tokens_metric) already carried the label; this makes the counter set consistent

Regression test test_requested_model_in_spend_and_requests_metrics in tests/test_litellm/integrations/test_prometheus_labels.py asserts the label is present on every metric in that family; reverting either source line makes the test fail with the exact missing-label assertion. One enterprise unit test (test_increment_top_level_request_and_spend_metrics) hard-coded the full kwargs of labels(...) and required the new requested_model=None entry to keep its assert_called_once_with aligned

@yucheng-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@greptile-apps

greptile-apps Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds requested_model as a label to litellm_spend_metric and litellm_requests_metric, making them consistent with the sibling token metrics (litellm_input_tokens_metric, litellm_output_tokens_metric, litellm_total_tokens_metric) which already carried this label. No plumbing changes are needed because enum_values.requested_model is already populated from standard_logging_payload["model_group"] and is read by the existing _inc_labeled_counter helper.

  • litellm/types/integrations/prometheus.py: UserAPIKeyLabelNames.REQUESTED_MODEL.value is inserted into litellm_requests_metric and litellm_spend_metric label lists, between user_agent and model_id to match the order used by the token metrics.
  • tests/test_litellm/integrations/test_prometheus_labels.py: New regression test test_requested_model_in_spend_and_requests_metrics asserts requested_model is present in all five related metric label sets; the test uses no network calls and is correctly placed in the unit-test directory.

Confidence Score: 5/5

Safe to merge — two-line label addition backed by a targeted regression test and no changes to request handling logic.

The change is entirely additive: two new entries in two label-name lists, with the value already flowing through the existing enum_values.requested_model field. The call site (_increment_top_level_request_and_spend_metrics) and the label resolution helper (prometheus_label_factory) are untouched. Adding a Prometheus label is backward-compatible for all existing queries. The new test exercises the full set of related metrics and would fail on revert.

No files require special attention.

Important Files Changed

Filename Overview
litellm/types/integrations/prometheus.py Adds REQUESTED_MODEL label to litellm_requests_metric and litellm_spend_metric label lists; minimal, correct, and consistent with sibling token metrics.
tests/test_litellm/integrations/test_prometheus_labels.py Adds a pure unit regression test covering all five affected metrics; no network calls, no mock weakening.

Reviews (1): Last reviewed commit: "feat(prometheus): add requested_model la..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds the requested_model label to litellm_spend_metric and litellm_requests_metric, making them consistent with the sibling token metrics (input_tokens, output_tokens, total_tokens) which already carried this label. No new plumbing is needed because UserAPIKeyLabelValues.requested_model is already populated upstream and prometheus_label_factory maps it automatically via model_dump().

  • litellm/types/integrations/prometheus.py: Two one-line additions insert UserAPIKeyLabelNames.REQUESTED_MODEL into the litellm_requests_metric and litellm_spend_metric label name lists; both metrics are initialized via get_labels_for_metric() so the counter definition and the type definition stay in sync automatically.
  • tests/test_litellm/integrations/test_prometheus_labels.py: New mock-only regression test asserts requested_model is present in all five relevant metrics, and will fail explicitly if either source line is reverted.

Confidence Score: 5/5

Safe to merge — the change is two one-line label additions in a type-definition file with no runtime logic changes.

Both changed files are tight and well-contained: the type definition and the counter initialization both read from the same get_labels_for_metric() call, so there is no risk of a label-cardinality mismatch. The requested_model field is already populated on UserAPIKeyLabelValues and flows through prometheus_label_factory automatically, so no new plumbing was required. The new regression test is mock-only, follows the existing test pattern in the same file, and will catch any future revert of either source line.

No files require special attention.

Important Files Changed

Filename Overview
litellm/types/integrations/prometheus.py Adds UserAPIKeyLabelNames.REQUESTED_MODEL to litellm_requests_metric and litellm_spend_metric label lists, making them consistent with the existing token metrics.
tests/test_litellm/integrations/test_prometheus_labels.py Adds regression test test_requested_model_in_spend_and_requests_metrics that asserts requested_model is present in all five relevant metric label lists; mock-only, no network calls.

Reviews (2): Last reviewed commit: "feat(prometheus): add requested_model la..." | Re-trigger Greptile

@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…rics

litellm_spend_metric_total and litellm_requests_metric_total previously
exposed only the resolved backend model_id and friendly model name, so
operators could not group spend or request counts by the model alias the
caller actually asked for when a router fronts multiple deployments
behind one name.

This adds the existing UserAPIKeyLabelNames.REQUESTED_MODEL to both
labelname lists; the value is already populated upstream from
standard_logging_payload["model_group"] and flows through the shared
_increment_top_level_request_and_spend_metrics call site. The sibling
token metrics (input/output/total) already carry the label, so this
also restores cross-metric consistency.

Resolves LIT-3796
@yucheng-berri
yucheng-berri force-pushed the litellm_lit_3796_requested_model branch from 2799d06 to 422b611 Compare June 26, 2026 06:17
@yucheng-berri
yucheng-berri merged commit ec4e014 into litellm_internal_staging Jun 26, 2026
124 checks passed
@yucheng-berri
yucheng-berri deleted the litellm_lit_3796_requested_model branch June 26, 2026 22:26
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.

2 participants