Repository navigation
fix(streaming): preserve provider model for cost calculation - #38656
mateo-berri merged 3 commits into
Conversation
Greptile SummaryThis PR preserves the provider-selected model in private streaming metadata so routed responses retain their public model alias while cost calculation uses the serving model.
Confidence Score: 5/5The PR appears safe to merge. The previously reported stale first-chunk identity issue is addressed by selecting the first provider identity that differs from the route, with regression coverage confirming that router-to-concrete-to-router streams price the concrete serving model; no blocking failure remains.
|
| Filename | Overview |
|---|---|
| litellm/cost_calculator.py | Uses the private provider-response model for pricing while preserving explicit pricing, base-model, and applicable regional precedence. |
| litellm/litellm_core_utils/streaming_chunk_builder_utils.py | Assembles provider-model metadata by selecting the first stream identity that differs from the initial route identity, completing the previously requested fix. |
| litellm/litellm_core_utils/streaming_handler.py | Tracks the latest nonempty provider model privately across chunks without changing the public response model. |
| tests/test_litellm/litellm_core_utils/test_streaming_handler.py | Adds regression coverage for private model propagation and router-to-concrete-to-router cost selection. |
| tests/test_litellm/llms/fireworks_ai/chat/test_fireworks_ai_chat_transformation.py | Verifies routed Fireworks streams retain their public route while accounting uses the selected provider model. |
| tests/test_litellm/test_cost_calculator.py | Covers provider-model pricing precedence, explicit overrides, and Bedrock region handling. |
Reviews (2): Last reviewed commit: "fix(cost_calculator): scope region_name ..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
The unconditional region read let a base_model or custom pricing deployment resolve to the regional cost-map key: a bedrock kimi base_model shifted to regional rates and vertex claude-opus-5 with a us-east5 key priced 0.0. Region now applies only when the model name comes from the provider response (provider_response_model or the response's own model), matching the base branch. Restores the BerriAI#38069 regression test and adds region-on-provider-model and base-model-free cases
|
bugbot run |
| return next( | ||
| (model for model in models if model != first_chunk_model), | ||
| models[0] if models else None, | ||
| ) |
There was a problem hiding this comment.
Cost pick uses first chunk not route
Medium Severity
_get_provider_response_model treats the first chunk's public model as the route and then takes the first hidden identity that differs from it. Wrapper state also overwrites provider_response_model whenever a later chunk carries a model string, including a terminal reversion to the route. When the first emitted chunk already uses the serving model (Azure mutates self.model from the incoming chunk), a later route-named chunk is chosen instead, so assembled spend is priced from the route rather than the serving model.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit c11a1f0. Configure here.
There was a problem hiding this comment.
Azure public chunk models mirror the raw ones, so the selection matches pre-existing _get_model_from_chunks semantics; the hypothesized ordering reproduces base pricing, never worse.
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
1 issue from previous review remains unresolved.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit c11a1f0. Configure here.
mateo-berri
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the contribution!
d4f6b44
into
BerriAI:litellm_internal_staging


TLDR
Problem this solves:
How it solves it:
provider_response_model)base_modelstill win)User Flow
Before: a user streams through a FireRouter route and the spend log records 0 instead of the served model's price
firerouter-deepseek-kimipointing atfireworks_ai/accounts/fireworks/routers/firerouter/deepseek-v4-flash-0731/kimi-k3"model": "firerouter-deepseek-kimi","stream": true, and"stream_options": {"include_usage": true}"model": "firerouter-deepseek-kimi", and the usage chunk reports 90 prompt and 10 completion tokensstreambills 2.64e-05, so streamed FireRouter traffic rides freeAfter: the same streamed call bills the model FireRouter actually served
firerouter-deepseek-kimipointing atfireworks_ai/accounts/fireworks/routers/firerouter/deepseek-v4-flash-0731/kimi-k3"model": "firerouter-deepseek-kimi","stream": true, and"stream_options": {"include_usage": true}"model"still reads"firerouter-deepseek-kimi"everywhereRelevant issues
Linear ticket
Resolves LIT-6364
Pre-Submission checklist
ci/circleci:contexts come from a mirror-branch run of the legacy suites, red only on failures a per-test diff shows are identical tolitellm_internal_staging's own latest pipeline, zero attributable to this PR)Screenshots / Proof of Fix
Shared setup: live proxy at each hash with Postgres spend logs, model
firerouter-deepseek-kimimapped tofireworks_ai/accounts/fireworks/routers/firerouter/deepseek-v4-flash-0731/kimi-k3, plusgpt-5.5as a non-Fireworks control. Three real calls per leg:Before (3300fc3)
"model":"firerouter-deepseek-kimi", usage 90 prompt / 15 completion, spend row 0. The assembled response carries only the public alias, which is not in the cost map, so the cost silently computes as nothing. The same stream priced throughlitellm.completion_cost, where the response carries the route string, bills 0.000495, kimi-k3's rates (90 x 3e-06 + 15 x 1.5e-05), though FireRouter served DeepSeek V4 Flashx-litellm-response-cost: 2.64e-05, spend row 2.64e-05After (c11a1f0)
"model":"firerouter-deepseek-kimi"), usage 90 prompt / 10 completion, spend row 1.54e-05 = 90 x 1.4e-07 + 10 x 2.8e-07, DeepSeek V4 Flash's price for exactly those tokensx-litellm-response-cost: 2.64e-05and spend row 2.64e-05Type
Bug Fix
Caveats
Low
fireworks_ai/deepseek-v4-flash-0731vsfireworks_ai/accounts/fireworks/models/deepseek-v4-flash-0731), so non-streaming (priced by the route name) and streaming (priced by the provider-reported name) use different entries until LIT-6408 aligns thembase_modelstill take precedence, and region prefixing stays scoped to response-derived names as on the base branch, so abase_modeldeployment cannot drift onto a regional cost-map keyFinal Attestation
The tests check the right things, including edge cases and regressions
c11a1f0 passes /live-pr-risk. CircleCI at the tip (mirror pipeline 88691) is red on 8 legacy-suite jobs; every failed test also fails on
litellm_internal_staging's own latest pipeline at the same test list (zero PR-attributable), andusing_litellm_on_windowsfails there while passing here