Skip to content

fix(otel): histogram bucket boundaries - #44619

Open
ArjunXvarma wants to merge 12 commits into
BerriAI:mainfrom
ArjunXvarma:bugfix/otel-histogram-bucket-boundaries
Open

ArjunXvarma wants to merge 12 commits into
BerriAI:mainfrom
ArjunXvarma:bugfix/otel-histogram-bucket-boundaries

Conversation

@ArjunXvarma

@ArjunXvarma ArjunXvarma commented Oct 5, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • GenAI latency histograms use the SDK's millisecond-sized default buckets
  • Values are recorded in seconds, so most requests share one bucket
  • p50/p95/p99 latency charts are meaningless

How it solves it:

  • New opt-in env var LITELLM_OTEL_SEMCONV_HISTOGRAM_BUCKETS, default false
  • When on, histograms use the OTel GenAI semconv bucket boundaries
  • Boundaries are an advisory, so operator-configured Views still win
  • Same behavior in both the v1 and v2 OTel engines
  • Older opentelemetry-api (< 1.30.0) falls back to defaults with one warning
  • Cost histogram unchanged: semconv defines no boundaries for it

Off by default because changing bucket layouts breaks percentile continuity with data already stored. Happy to flip the default to true if maintainers prefer shipping the fix on by default

User Flow

Before: an operator charting LLM latency sees the same percentiles no matter how fast requests are

  1. They run the proxy with OpenTelemetry metrics exported to Prometheus/Grafana
  2. Their apps send POST https://litellm-domain/v1/chat/completions, taking 0.3 s to 4.9 s each
  3. They chart p50 and p95 of gen_ai.client.operation.duration
  4. Every request lands in one (0, 5 s] bucket, so p50 reads ~2.5 s and p95 ~4.75 s regardless of real latency

After: the operator opts in and the same chart reflects real latency

  1. They set LITELLM_OTEL_SEMCONV_HISTOGRAM_BUCKETS=true and restart the proxy with the same exporter
  2. Their apps send the same POST https://litellm-domain/v1/chat/completions requests
  3. They chart p50 and p95 of gen_ai.client.operation.duration
  4. Requests spread across buckets from 10 ms to 82 s, so the percentiles track actual latency

Operators who don't set the variable see no change after upgrading

Relevant issues

Fixes #44558

Pre-Submission checklist

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

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Live proxy on localhost:4000 with the default OTel engine, real calls to Gemini (gemini-3.5-flash-lite), and metrics printed by the OTel console exporter every 5 s. Each run sends the same four requests, asking for 5, 60, 250 and 500 words so latencies spread from about 1 s to 4 s, then reads every gen_ai.client.operation.duration data point from the proxy log

Shared setup, config.yaml:

model_list:
    - model_name: gemini-3.5-flash-lite
      litellm_params:
          model: gemini/gemini-3.5-flash-lite
          api_key: os.environ/GEMINI_API_KEY
litellm_settings:
    callbacks: ["otel"]
general_settings:
    master_key: sk-1234

Start command (the flag line is added or removed per case):

LITELLM_DANGEROUSLY_PERMIT_WEAK_OR_UNSET_MASTER_KEY=true \
OTEL_EXPORTER=console \
LITELLM_OTEL_INTEGRATION_ENABLE_METRICS=true \
LITELLM_OTEL_SEMCONV_HISTOGRAM_BUCKETS=true \
python litellm/proxy/proxy_cli.py --config config.yaml --port 4000 > litellm.log 2>&1

Requests:

for words in 5 60 250 500; do
  curl -s localhost:4000/v1/chat/completions -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" \
    -d "{\"model\": \"gemini-3.5-flash-lite\", \"messages\": [{\"role\": \"user\", \"content\": \"Write about Paris in exactly $words words.\"}]}" \
    | jq -c '{model, completion_tokens: .usage.completion_tokens, error: .error.message}'
done

Reading the histogram after the next export:

awk '/^\{$/,/^\}$/' litellm.log | jq -c 'select(.resource_metrics)' | tail -1 \
  | jq -c '.resource_metrics[].scope_metrics[].metrics[] | select(.name == "gen_ai.client.operation.duration")
           | .data.data_points[] | {latency_s: .sum, explicit_bounds, bucket_counts}'

Every request lands in its own data point because the default metric attributes include per-request metadata. That is existing behavior and unrelated to this PR

Before (d4619c4)

Flag on

  1. Start the proxy with LITELLM_OTEL_SEMCONV_HISTOGRAM_BUCKETS=true, which this commit does not read yet
  2. Send the four requests:
{"model":"gemini-3.5-flash-lite","completion_tokens":6,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":74,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":289,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":618,"error":null}
  1. Read the histogram:
{"latency_s":1.192238,"explicit_bounds":[0.0,5.0,10.0,25.0,50.0,75.0,100.0,250.0,500.0,750.0,1000.0,2500.0,5000.0,7500.0,10000.0],"bucket_counts":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
{"latency_s":1.105935,"explicit_bounds":[0.0,5.0,10.0,25.0,50.0,75.0,100.0,250.0,500.0,750.0,1000.0,2500.0,5000.0,7500.0,10000.0],"bucket_counts":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
{"latency_s":2.143542,"explicit_bounds":[0.0,5.0,10.0,25.0,50.0,75.0,100.0,250.0,500.0,750.0,1000.0,2500.0,5000.0,7500.0,10000.0],"bucket_counts":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
{"latency_s":3.388643,"explicit_bounds":[0.0,5.0,10.0,25.0,50.0,75.0,100.0,250.0,500.0,750.0,1000.0,2500.0,5000.0,7500.0,10000.0],"bucket_counts":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}

Requests taking 1.1 s and 3.4 s all land in the same (0, 5] bucket, so p50 and p95 can't tell them apart

After (df1fbe8)

Flag unset (default)

  1. Start the proxy without LITELLM_OTEL_SEMCONV_HISTOGRAM_BUCKETS
  2. Send the four requests:
{"model":"gemini-3.5-flash-lite","completion_tokens":6,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":72,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":295,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":652,"error":null}
  1. Read the histogram:
{"latency_s":0.998942,"explicit_bounds":[0.0,5.0,10.0,25.0,50.0,75.0,100.0,250.0,500.0,750.0,1000.0,2500.0,5000.0,7500.0,10000.0],"bucket_counts":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
{"latency_s":1.166566,"explicit_bounds":[0.0,5.0,10.0,25.0,50.0,75.0,100.0,250.0,500.0,750.0,1000.0,2500.0,5000.0,7500.0,10000.0],"bucket_counts":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
{"latency_s":2.586207,"explicit_bounds":[0.0,5.0,10.0,25.0,50.0,75.0,100.0,250.0,500.0,750.0,1000.0,2500.0,5000.0,7500.0,10000.0],"bucket_counts":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}
{"latency_s":4.164467,"explicit_bounds":[0.0,5.0,10.0,25.0,50.0,75.0,100.0,250.0,500.0,750.0,1000.0,2500.0,5000.0,7500.0,10000.0],"bucket_counts":[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}

Same boundaries and bucketing as Before, so upgrading without opting in changes nothing

Flag on

  1. Start the proxy with LITELLM_OTEL_SEMCONV_HISTOGRAM_BUCKETS=true
  2. Send the four requests:
{"model":"gemini-3.5-flash-lite","completion_tokens":6,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":79,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":313,"error":null}
{"model":"gemini-3.5-flash-lite","completion_tokens":637,"error":null}
  1. Read the histogram:
{"latency_s":0.92136,"explicit_bounds":[0.01,0.02,0.04,0.08,0.16,0.32,0.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92],"bucket_counts":[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]}
{"latency_s":1.277478,"explicit_bounds":[0.01,0.02,0.04,0.08,0.16,0.32,0.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92],"bucket_counts":[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0]}
{"latency_s":2.049087,"explicit_bounds":[0.01,0.02,0.04,0.08,0.16,0.32,0.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92],"bucket_counts":[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0]}
{"latency_s":4.226235,"explicit_bounds":[0.01,0.02,0.04,0.08,0.16,0.32,0.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92],"bucket_counts":[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0]}

The semconv boundaries apply, and the requests spread across (0.64, 1.28], (1.28, 2.56] and (2.56, 5.12], so percentiles now track real latency

Type

Bug Fix
Test

Caveats (if any)

Medium

Low

  • Opting in breaks percentile continuity for data stored before the switch
    • Old and new histograms can't be merged; charts spanning it are briefly wrong
  • Needs opentelemetry-api >= 1.30.0 for the flag to take effect
    • Older versions keep the defaults and log one warning
  • gen_ai.usage.cost keeps the SDK defaults
    • Semconv defines no boundaries for it
  • Time per output token starts at 10 ms, coarse for fast hosted models
    • This follows the semconv server-side recommendation
  • GenAI semconv is still experimental
    • The spec has since split token usage into separate input and output token metrics

@codspeed

codspeed Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 31 untouched benchmarks


Comparing ArjunXvarma:bugfix/otel-histogram-bucket-boundaries (f1899ff) with main (7ff5010)

Open in CodSpeed

@codecov

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@CLAassistant

CLAassistant commented Oct 6, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ArjunXvarma ArjunXvarma changed the title Bugfix/otel histogram bucket boundaries fix/otel histogram bucket boundaries Oct 6, 2026
@ArjunXvarma ArjunXvarma changed the title fix/otel histogram bucket boundaries fix(otel): histogram bucket boundaries Oct 6, 2026
@ArjunXvarma
ArjunXvarma force-pushed the bugfix/otel-histogram-bucket-boundaries branch 2 times, most recently from 1a1d940 to d67988b Compare October 6, 2026 10:17
@ArjunXvarma
ArjunXvarma marked this pull request as ready for review October 6, 2026 10:25
@greptile-apps

greptile-apps Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adds optional histogram bucket configuration for metrics.

The PR appears safe to merge; no outstanding findings or new actionable issues were identified.

Summary

The PR adds opt-in GenAI histogram boundaries to both OpenTelemetry engines, preserves SDK defaults when the flag is off, and tests the boundary layouts and compatibility paths. The latest changes make direct v1 configuration honor the environment flag and simplify test comments.

Reviews (3) · Last reviewed commit: "fix(otel): honor histogram bucket opt-in..."

Comment thread litellm/integrations/opentelemetry.py Resolved
@ArjunXvarma
ArjunXvarma marked this pull request as draft October 6, 2026 10:48
@ArjunXvarma
ArjunXvarma marked this pull request as ready for review October 6, 2026 12:39
Comment thread litellm/integrations/opentelemetry.py Resolved
@ArjunXvarma
ArjunXvarma marked this pull request as draft October 6, 2026 12:44
@ArjunXvarma
ArjunXvarma marked this pull request as ready for review October 6, 2026 13:10
@ArjunXvarma

Copy link
Copy Markdown
Author

Note on the failing test_env_keys.py check

This check fails on two environment variables:

  • LITELLM_OTEL_SEMCONV_HISTOGRAM_BUCKETS is introduced by this PR and is documented in Document LITELLM_OTEL_SEMCONV_HISTOGRAM_BUCKETS env var litellm-docs#2113, which adds a row to the environment variables reference table in docs/proxy/config_settings.md. This check will pass for it once that PR merges.
  • DEFER_PYDANTIC_BUILD is unrelated to this change. It's read elsewhere on main and is undocumented there, so the same check currently fails for every PR.

No other checks are affected.

The metric bucket constants were taken from the gen-ai-token-metrics.md
and gen-ai-metrics.md files from the open telemetry semantic convetions
for gen ai repository.

The token cost constant was not considered because the gen ai semantic
conventions do not define any boundaries for it
Add tests for GenAI histogram bucket boundaries

- test_histograms_use_semconv_bucket_boundaries: each GenAI histogram
  exports the semconv boundaries from MetricBuckets, and records at
  least one data point so the check cannot pass vacuously.
- test_operator_view_overrides_default_boundaries: the boundaries are
  passed as an advisory, so an operator-configured View still takes
  precedence.
- test_distinct_latencies_land_in_distinct_buckets: a 0.3 s and a
  4.9 s call land in different duration buckets; with the SDK's
  ms-sized defaults both fell into (0, 5].
- test_bucket_boundaries_are_positive_and_strictly_increasing: guards
  the constants against typos when they are updated to a new spec
  revision.
@ArjunXvarma
ArjunXvarma force-pushed the bugfix/otel-histogram-bucket-boundaries branch from 846cbed to f1899ff Compare October 10, 2026 09:15
@ArjunXvarma

Copy link
Copy Markdown
Author

The 4 CI failures are only the env-key docs check; the variable is documented in BerriAI/litellm-docs#2113, and CI will pass once it merges

This branch has not been deployed

No deployments
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.

[Bug]: OTel GenAI histograms use the SDK's default (millisecond) bucket boundaries; seconds-valued durations collapse into one bucket

2 participants