Skip to content

fix(logging): spilled over PTU requests without base_model log the served model's rates - #47655

Merged
klu-berri merged 5 commits into
mainfrom
litellm_ptu_spillover_zeroed_rates
Oct 20, 2026
Merged

klu-berri merged 5 commits into
mainfrom
litellm_ptu_spillover_zeroed_rates

Conversation

@klu-berri

@klu-berri klu-berri commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

When Azure spills a PTU request onto pay-as-you-go capacity the proxy already bills it at the served model's per token rates, but the spend log's model_map_information still showed the PTU deployment's zeroed rates whenever the deployment had no base_model. This PR makes that log entry name the served model and its real rates, so the rates in the row explain the spend in the row. Regular PTU requests keep their flat pricing and zero per token rates

Details

TLDR

Problem this solves:

  • Spilled over PTU request without base_model logs model_map_value rates of 0 while spend is nonzero
  • The metadata deployment fallback from fix(logging): resolve model_map_value for proxy custom pricing #31940 resolves to the deployment's zeroed PTU cost-map entry
  • The proxy rewrites response.model to the requested alias, so the served model is gone by log time

How it solves it:

  • get_standard_logging_object_payload skips the deployment fallback when is_spilled_over_ptu_request is true
  • ResponseMetadata.set_hidden_params keeps the provider's model in _hidden_params["provider_response_model"], which select_model_name_for_cost_calc already prefers (streaming chunks had it, non stream responses did not)
  • A response that already carries provider_response_model (the decisions path sets its own) keeps it, read through a pydantic TypeAdapter so non dict hidden params are ignored
  • Both new tests fail with litellm/ at origin/main (model_map_key stays the zeroed deployment key, cost model follows the alias) and pass with the fix

User Flow

Before: a proxy admin with a PTU deployment and PTU cost attribution on sees a spilled over request billed correctly but logged at zero rates

  1. They send POST http://localhost:4000/v1/chat/completions with "model": "gpt-5.4-nano-ptu" and Azure answers with x-ms-is-spilled-over: true
  2. The response carries x-litellm-response-cost: 8.85e-06, so the request was billed per token
  3. They open the spend log row and model_map_information names the PTU deployment with input_cost_per_token: 0 and output_cost_per_token: 0, so nothing explains the 8.85e-06

After: the same request logs the served model and the rates the spend came from

  1. They send POST http://localhost:4000/v1/chat/completions with "model": "gpt-5.4-nano-ptu" and Azure answers with x-ms-is-spilled-over: true
  2. The response carries x-litellm-response-cost: 8.85e-06, unchanged
  3. The spend log row's model_map_information names the served model with input_cost_per_token: 2e-07 and output_cost_per_token: 1.25e-06, and 13 x 2e-07 + 5 x 1.25e-06 = 8.85e-06

Affected release

regression since #31940

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
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

Rung: live proxy started from the checkout (python litellm/proxy/proxy_cli.py --config config.yaml --detailed_debug --port 4000) with a real Postgres 14 database_url, real Azure OpenAI calls to a gpt-5.4-nano deployment, spend read back with psql. The QA subscription has no PTU deployment, so Azure answers x-ms-is-spilled-over: false. For the spillover case the deployment's api_base points at a local pass-through on 127.0.0.1:4100 that forwards the request to Azure unchanged and replaces that header with x-ms-is-spilled-over: true plus x-ms-spillover-from-deployment. The regular case goes straight to Azure. Both cases use the same deployment, PTU terms and token counts

Environment: LITELLM_ENABLE_PTU_COST_ATTRIBUTION=True, LITELLM_LOCAL_MODEL_COST_MAP=True

model_list:
  - model_name: gpt-5.4-nano-ptu
    litellm_params:
      model: azure/gpt-5.4-nano-lit9298
      api_base: http://127.0.0.1:4100
      api_key: os.environ/AZURE_FOUNDRY_API_KEY
      api_version: "2025-04-01-preview"
    model_info:
      id: ptu-gpt-54-nano
      ptu_count: 50
      cost_per_ptu_per_hour: 1.0
      ptu_effective_from: "2026-01-01"
      team_id: team-ptu
  - model_name: gpt-5.4-nano-ptu-direct
    litellm_params:
      model: azure/gpt-5.4-nano-lit9298
      api_base: os.environ/AZURE_FOUNDRY_API_BASE
      api_key: os.environ/AZURE_FOUNDRY_API_KEY
      api_version: "2025-04-01-preview"
    model_info:
      id: ptu-gpt-54-nano-direct
      ptu_count: 50
      cost_per_ptu_per_hour: 1.0
      ptu_effective_from: "2026-01-01"
      team_id: team-ptu
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  database_url: os.environ/DATABASE_URL

Before (5c341e7)

Spilled over PTU request

  1. curl -s http://localhost:4000/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-5.4-nano-ptu","messages":[{"role":"user","content":"Reply with the single word spillover"}],"max_tokens":20}' -D -
  2. Output
    x-litellm-call-id: 234d476f-7400-4d63-ba6a-8769f4ecafda
    x-litellm-model-id: ptu-gpt-54-nano
    x-litellm-response-cost: 8.85e-06
    {"id": "chatcmpl-EbBmWElIw6l7f0Fgsvl3mcQhZeIWw", "model": "gpt-5.4-nano-ptu", "content": "spillover", "usage": {"prompt_tokens": 13, "completion_tokens": 5}}
    

Regular PTU request (no spillover)

  1. curl -s http://localhost:4000/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-5.4-nano-ptu-direct","messages":[{"role":"user","content":"Reply with the single word spillover"}],"max_tokens":20}' -D -
  2. Output (no x-litellm-response-cost header because the cost is 0)
    x-litellm-call-id: 5c02f6f7-d0bb-4abd-9b06-d4414def3147
    x-litellm-model-id: ptu-gpt-54-nano-direct
    {"id": "chatcmpl-EbBmYPcz8ZC9KXI4GgDZbF2ngyOko", "model": "gpt-5.4-nano-ptu-direct", "content": "spillover", "usage": {"prompt_tokens": 13, "completion_tokens": 5}}
    

Spend log rows

  1. psql "$DATABASE_URL" -c "select request_id, model, spend, prompt_tokens as pt, completion_tokens as ct, metadata->'model_map_information'->>'model_map_key' as model_map_key, metadata->'model_map_information'->'model_map_value'->>'input_cost_per_token' as in_rate, metadata->'model_map_information'->'model_map_value'->>'output_cost_per_token' as out_rate from \"LiteLLM_SpendLogs\" order by \"startTime\" desc limit 2"
  2. Output, the spilled over row is billed 8.85e-06 but logged under the PTU deployment key at 0 and 0
                   request_id               |           model            |  spend   | pt | ct |       model_map_key        | in_rate | out_rate
    ----------------------------------------+----------------------------+----------+----+----+----------------------------+---------+----------
     chatcmpl-EbBmYPcz8ZC9KXI4GgDZbF2ngyOko | azure/gpt-5.4-nano-lit9298 |        0 | 13 |  5 | azure/gpt-5.4-nano-lit9298 | 0.0     | 0.0
     chatcmpl-EbBmWElIw6l7f0Fgsvl3mcQhZeIWw | azure/gpt-5.4-nano-lit9298 | 8.85e-06 | 13 |  5 | azure/gpt-5.4-nano-lit9298 | 0       | 0
    

After (d171a14)

Spilled over PTU request

  1. curl -s http://localhost:4000/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-5.4-nano-ptu","messages":[{"role":"user","content":"Reply with the single word spillover"}],"max_tokens":20}' -D -
  2. Output, same cost as before
    x-litellm-call-id: 52937d4f-7e1d-408f-93bf-9b021b882309
    x-litellm-model-id: ptu-gpt-54-nano
    x-litellm-response-cost: 8.85e-06
    {"id": "chatcmpl-EbCJeiMZs8Ow3ocsoYpyd6DdHcl5q", "model": "gpt-5.4-nano-ptu", "content": "spillover", "usage": {"prompt_tokens": 13, "completion_tokens": 5}}
    

Regular PTU request (no spillover)

  1. curl -s http://localhost:4000/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-5.4-nano-ptu-direct","messages":[{"role":"user","content":"Reply with the single word spillover"}],"max_tokens":20}' -D -
  2. Output, still 0 cost
    x-litellm-call-id: c76690ec-26d2-431f-8e0b-92fe241b291e
    x-litellm-model-id: ptu-gpt-54-nano-direct
    {"id": "chatcmpl-EbCJgXAYuk1VxF5aQkloP0oiQKlUu", "model": "gpt-5.4-nano-ptu-direct", "content": "spillover", "usage": {"prompt_tokens": 13, "completion_tokens": 5}}
    

Spend log rows

  1. Same psql query as Before
  2. Output, the spilled over row now names the served model with the rates that produce 13 x 2e-07 + 5 x 1.25e-06 = 8.85e-06, the regular PTU row is unchanged
                   request_id               |           model            |  spend   | pt | ct |       model_map_key        |  in_rate  |  out_rate
    ----------------------------------------+----------------------------+----------+----+----+----------------------------+-----------+------------
     chatcmpl-EbCJgXAYuk1VxF5aQkloP0oiQKlUu | azure/gpt-5.4-nano-lit9298 |        0 | 13 |  5 | azure/gpt-5.4-nano-lit9298 | 0.0       | 0.0
     chatcmpl-EbCJeiMZs8Ow3ocsoYpyd6DdHcl5q | azure/gpt-5.4-nano-lit9298 | 8.85e-06 | 13 |  5 | gpt-5.4-nano-2026-03-17    | 0.0000002 | 0.00000125
    

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Every non stream ModelResponse now carries _hidden_params["provider_response_model"], not only spilled over ones
    • Billing is unchanged because the cost is computed before the field is set, and at that point it equals response.model
    • StandardLoggingHiddenParams keeps its fixed key set, so the field does not reach the spend log

Low

  • Spillover was injected by the local pass-through, the QA subscription has no PTU deployment that Azure would spill
  • A spilled over streaming request was not run live, streaming chunks already carried provider_response_model so the same payload path applies

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/5bc99459e4744df4ae03daa67e69c81c
Open in Devin Desktop: https://app.devin.ai/desktop/session/5bc99459e4744df4ae03daa67e69c81c?variant=devin
Requested by: @kerrylu-berri


Note

Medium Risk
Touches spend-log model_map selection and cost attribution for Azure PTU spillover; behavior change is scoped to spillover and metadata fallback, with billing paths explicitly guarded.

Overview
Fixes spend logs for Azure PTU spillover when a deployment has no base_model: billed spend used pay-as-you-go token rates, but model_map_information still pointed at the PTU deployment’s zeroed cost-map entry.

Logging / pricing: Introduces spilled_over_ptu_for() and uses it so spilled-over requests skip PTU custom pricing and do not fall back to metadata.deployment for base_model in get_standard_logging_object_payload. custom_pricing is forced off on that path so the log’s rates match the row’s spend.

Response metadata: Non-stream responses now stash the provider’s model in _hidden_params["provider_response_model"] (same idea as streaming), so after the proxy rewrites result.model to the client alias, cost-map lookup can still resolve the served model’s rates.

Tests cover spillover logging without base_model and preserving the provider model through proxy rewrites.

Reviewed by Cursor Bugbot for commit c3a2f11. Bugbot is set up for automated code reviews on this repo. Configure here.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@klu-berri

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@kerry-berri

kerry-berri commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

litellm-stampbot: risk high

Not approved at c3a2f11. risk is high, a human reviewer is needed.

Rules (stampbot/risk_rules.yml):

  • low: no rule flagged it

Cursor Bugbot: Medium Risk at c3a2f11. Touches spend-log model_map selection and cost attribution for Azure PTU spillover; behavior change is scoped to spillover and metadata fallback, with billing paths explicitly guarded. Bugbot run requested, finished in 4 min.
Bugbot Low is low, Medium and High are high. The judge model was not called.

Pushes do not re-run this check. Comment @litellm-stampbot again to re-check the new head.

@kerry-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit c3a2f11. Configure here.

@kerry-berri kerry-berri added the risk:high Sensitive, broad, or high-risk change label Oct 20, 2026
@codspeed

codspeed Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 32 untouched benchmarks


Comparing litellm_ptu_spillover_zeroed_rates (d171a14) with main (6bc1b2b)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (730db4d) during the generation of this report, so 6bc1b2b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@klu-berri
klu-berri enabled auto-merge (squash) October 20, 2026 21:59
@klu-berri
klu-berri merged commit 6b1f4ed into main Oct 20, 2026
89 of 90 checks passed
@klu-berri
klu-berri deleted the litellm_ptu_spillover_zeroed_rates branch October 20, 2026 22:28
devin-ai-integration Bot added a commit that referenced this pull request Oct 21, 2026
…rved model's rates (#47655)

Backport of #47655 to stable/1.103.x

Cherry-picked from 6b1f4ed (main)
Cartofante pushed a commit to CartoDB/litellm that referenced this pull request Oct 26, 2026
…rved model's rates (BerriAI#47655)

* fix(logging): report pay-as-you-go rates for spilled over PTU requests without base_model

* fix(logging): keep the provider response model so spilled over PTU payloads price the served model

* fix(logging): keep a provider_response_model the response already carries

* fix(logging): read the carried provider_response_model through a typed adapter

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>

Backport of BerriAI#47655 to stable/1.106.x
Cherry-picked from 6b1f4ed (main)
yuneng-berri pushed a commit that referenced this pull request Oct 28, 2026
…rved model's rates (#47655)

* fix(logging): report pay-as-you-go rates for spilled over PTU requests without base_model

* fix(logging): keep the provider response model so spilled over PTU payloads price the served model

* fix(logging): keep a provider_response_model the response already carries

* fix(logging): read the carried provider_response_model through a typed adapter

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 6b1f4ed)
haowang1013 pushed a commit to directivegames/litellm that referenced this pull request Nov 3, 2026
…rved model's rates (BerriAI#47655)

* fix(logging): report pay-as-you-go rates for spilled over PTU requests without base_model

* fix(logging): keep the provider response model so spilled over PTU payloads price the served model

* fix(logging): keep a provider_response_model the response already carries

* fix(logging): read the carried provider_response_model through a typed adapter

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>

Backport of BerriAI#47655 to stable/1.105.x.
Cherry-picked from 6b1f4ed (main).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:high Sensitive, broad, or high-risk change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants