Skip to content

fix(logging): report the cost-map entry billing used in model_map_information - #47182

Merged
klu-berri merged 4 commits into
mainfrom
litellm_lit9660_model_map_from_billing
Oct 19, 2026
Merged

klu-berri merged 4 commits into
mainfrom
litellm_lit9660_model_map_from_billing

Conversation

@klu-berri

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

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • A deployment with its own input_cost_per_token / output_cost_per_token is billed at that price
  • But every callback's model_map_information and the spend log metadata show the catalog price of the model
  • A deployment priced at 0 is logged at the catalog's non-zero rates

How it solves it:

  • Billing and logging share one lookup, select_model_name_for_cost_calc, which returns the deployment entry only when given the deployment id
  • Billing passed that id, the logging payload builder never did
  • get_standard_logging_object_payload now passes logging_obj.get_router_model_id() into get_model_cost_information, which uses it only to pick the entry behind model_map_value
  • model_map_key stays the catalog name it is today (openai/gpt-6-luna), so nothing grouping on that key changes

Pseudo-diff:

 def get_model_cost_information(base_model, custom_pricing, custom_llm_provider, init_response_obj, api_base=None,
+                               router_model_id=None):
     model_cost_name = select_model_name_for_cost_calc(model=..., completion_response=..., base_model=..., custom_pricing=...)
+    pricing_model_name = select_model_name_for_cost_calc(..., router_model_id=router_model_id) if router_model_id else model_cost_name
-    _model_cost_information = litellm.get_model_info(model=model_cost_name, ...)
+    _model_cost_information = litellm.get_model_info(model=pricing_model_name or model_cost_name, ...)
     return StandardLoggingModelInformation(model_map_key=model_cost_name, model_map_value=_model_cost_information)

 # get_standard_logging_object_payload
     StandardLoggingPayloadSetup.get_model_cost_information(..., api_base=litellm_params.get("api_base"),
+                                                           router_model_id=logging_obj.get_router_model_id())

No change to model_map_key: for every deployment it stays the catalog name (openai/gpt-6-luna). Only model_map_value changes, and only for a deployment with its own price, where it is now that deployment's entry (1e-06 / 2e-06 for the priced one, 0 / 0 for the zero-priced one). Deployments without their own price are unchanged

User Flow

Before: a proxy admin prices a deployment themselves, the bill is right, but their logging destination and the spend logs disagree with it

  1. They add model_info: {id: priced-deployment, input_cost_per_token: 0.000001, output_cost_per_token: 0.000002} to a deployment of openai/gpt-6-luna and restart the proxy
  2. They send POST https://litellm-domain/v1/chat/completions with "model": "priced-alias" and get 200 with x-litellm-response-cost: 2.2e-05 for 10 input and 6 output tokens
  3. Their logging callback receives model_map_information saying openai/gpt-6-luna at 1e-07 in and 5e-07 out, which would make the bill 4e-06, not 2.2e-05
  4. https://litellm-domain/ui/?page=logs shows the same request at spend 2.2e-05 with the same catalog rates in its metadata, and a deployment priced at 0 shows those non-zero rates too

After: the same request is logged at the price it was billed at

  1. They add the same model_info prices to the deployment and restart the proxy
  2. They send the same POST https://litellm-domain/v1/chat/completions with "model": "priced-alias" and get 200 with x-litellm-response-cost: 2.2e-05
  3. Their logging callback receives model_map_information still keyed openai/gpt-6-luna but at 1e-06 in and 2e-06 out, which multiplies out to the 2.2e-05 they were charged
  4. https://litellm-domain/ui/?page=logs shows that request's metadata at the same rates, and the deployment priced at 0 shows 0 and 0

Relevant issues

Fixes #46651

Linear ticket

Resolves LIT-9660

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

Shared setup: a local proxy with a local Postgres, three deployments of the same model pointed at a local OpenAI-compatible server that answers every chat completion with usage: {prompt_tokens: 10, completion_tokens: 6}, and a 15-line CustomLogger that appends standard_logging_object.model_map_information to slo.jsonl. Expected bills: custom price 10 * 1e-06 + 6 * 2e-06 = 2.2e-05, catalog price 10 * 1e-07 + 6 * 5e-07 = 4e-06

model_list:
  - model_name: priced-alias
    litellm_params: {model: openai/gpt-6-luna, api_key: fake-key, api_base: http://127.0.0.1:4601/v1}
    model_info: {id: priced-deployment, input_cost_per_token: 0.000001, output_cost_per_token: 0.000002}
  - model_name: zero-alias
    litellm_params: {model: openai/gpt-6-luna, api_key: fake-key, api_base: http://127.0.0.1:4601/v1}
    model_info: {id: zero-deployment, input_cost_per_token: 0, output_cost_per_token: 0}
  - model_name: plain-alias
    litellm_params: {model: openai/gpt-6-luna, api_key: fake-key, api_base: http://127.0.0.1:4601/v1}
    model_info: {id: plain-deployment}
litellm_settings:
  callbacks: custom_cb.dump

Test mutation check: with litellm/litellm_core_utils/litellm_logging.py reverted to main and the new test kept, test_standard_logging_payload_reports_the_deployment_entry_the_request_was_billed_from fails in both parametrizations on model_map_information (catalog key and rates come back), and passes at the tip

Before (4bafebb)

Custom-priced deployment (priced-alias, 1e-06 in / 2e-06 out)

  1. Send the request

    $ curl -si http://localhost:4121/v1/chat/completions -H 'Authorization: Bearer $LITELLM_MASTER_KEY' -H 'Content-Type: application/json' -d '{"model": "priced-alias", "messages": [{"role": "user", "content": "hi"}]}' | grep -iE '^HTTP|x-litellm-model-id|x-litellm-response-cost:'
    HTTP/1.1 200 OK
    x-litellm-model-id: priced-deployment
    x-litellm-response-cost: 2.2e-05
    
  2. Read what the callback received in standard_logging_object.model_map_information (the callback appends it to slo.jsonl)

    $ cat slo.jsonl | grep priced-alias
    {"model_group": "priced-alias", "model_id": "priced-deployment", "response_cost": 2.2e-05, "model_map_key": "openai/gpt-6-luna", "model_map_value.input_cost_per_token": 1e-07, "model_map_value.output_cost_per_token": 5e-07}
    
  3. Read the spend log row

    $ psql "$DATABASE_URL" -c "select model_group, model_id, spend, metadata->'model_map_information'->>'model_map_key' as model_map_key, metadata->'model_map_information'->'model_map_value'->>'input_cost_per_token' as input_rate, metadata->'model_map_information'->'model_map_value'->>'output_cost_per_token' as output_rate from \"LiteLLM_SpendLogs\" where model_group = 'priced-alias'"
     model_group  |     model_id      |  spend  |   model_map_key   | input_rate | output_rate 
    --------------+-------------------+---------+-------------------+------------+-------------
     priced-alias | priced-deployment | 2.2e-05 | openai/gpt-6-luna | 0.0000001  | 0.0000005
    

Explicit zero-priced deployment (zero-alias, 0 / 0)

  1. Send the request

    $ curl -si http://localhost:4121/v1/chat/completions -H 'Authorization: Bearer $LITELLM_MASTER_KEY' -H 'Content-Type: application/json' -d '{"model": "zero-alias", "messages": [{"role": "user", "content": "hi"}]}' | grep -iE '^HTTP|x-litellm-model-id|x-litellm-response-cost:'
    HTTP/1.1 200 OK
    x-litellm-model-id: zero-deployment
    
  2. Read what the callback received in standard_logging_object.model_map_information (the callback appends it to slo.jsonl)

    $ cat slo.jsonl | grep zero-alias
    {"model_group": "zero-alias", "model_id": "zero-deployment", "response_cost": 0.0, "model_map_key": "openai/gpt-6-luna", "model_map_value.input_cost_per_token": 1e-07, "model_map_value.output_cost_per_token": 5e-07}
    
  3. Read the spend log row

    $ psql "$DATABASE_URL" -c "select model_group, model_id, spend, metadata->'model_map_information'->>'model_map_key' as model_map_key, metadata->'model_map_information'->'model_map_value'->>'input_cost_per_token' as input_rate, metadata->'model_map_information'->'model_map_value'->>'output_cost_per_token' as output_rate from \"LiteLLM_SpendLogs\" where model_group = 'zero-alias'"
     model_group  |     model_id      |  spend  |   model_map_key   | input_rate | output_rate 
    --------------+-------------------+---------+-------------------+------------+-------------
     zero-alias   | zero-deployment   |       0 | openai/gpt-6-luna | 0.0000001  | 0.0000005
    

Deployment with no price of its own (plain-alias)

  1. Send the request

    $ curl -si http://localhost:4121/v1/chat/completions -H 'Authorization: Bearer $LITELLM_MASTER_KEY' -H 'Content-Type: application/json' -d '{"model": "plain-alias", "messages": [{"role": "user", "content": "hi"}]}' | grep -iE '^HTTP|x-litellm-model-id|x-litellm-response-cost:'
    HTTP/1.1 200 OK
    x-litellm-model-id: plain-deployment
    x-litellm-response-cost: 4e-06
    
  2. Read what the callback received in standard_logging_object.model_map_information (the callback appends it to slo.jsonl)

    $ cat slo.jsonl | grep plain-alias
    {"model_group": "plain-alias", "model_id": "plain-deployment", "response_cost": 4e-06, "model_map_key": "openai/gpt-6-luna", "model_map_value.input_cost_per_token": 1e-07, "model_map_value.output_cost_per_token": 5e-07}
    
  3. Read the spend log row

    $ psql "$DATABASE_URL" -c "select model_group, model_id, spend, metadata->'model_map_information'->>'model_map_key' as model_map_key, metadata->'model_map_information'->'model_map_value'->>'input_cost_per_token' as input_rate, metadata->'model_map_information'->'model_map_value'->>'output_cost_per_token' as output_rate from \"LiteLLM_SpendLogs\" where model_group = 'plain-alias'"
     model_group  |     model_id      |  spend  |   model_map_key   | input_rate | output_rate 
    --------------+-------------------+---------+-------------------+------------+-------------
     plain-alias  | plain-deployment  |   4e-06 | openai/gpt-6-luna | 0.0000001  | 0.0000005
    

After (1554428)

Custom-priced deployment (priced-alias, 1e-06 in / 2e-06 out)

  1. Send the request

    $ curl -si http://localhost:4124/v1/chat/completions -H 'Authorization: Bearer $LITELLM_MASTER_KEY' -H 'Content-Type: application/json' -d '{"model": "priced-alias", "messages": [{"role": "user", "content": "hi"}]}' | grep -iE '^HTTP|x-litellm-model-id|x-litellm-response-cost:'
    HTTP/1.1 200 OK
    x-litellm-model-id: priced-deployment
    x-litellm-response-cost: 2.2e-05
    
  2. Read what the callback received in standard_logging_object.model_map_information (the callback appends it to slo.jsonl)

    $ cat slo.jsonl | grep priced-alias
    {"model_group": "priced-alias", "model_id": "priced-deployment", "response_cost": 2.2e-05, "model_map_key": "openai/gpt-6-luna", "model_map_value.input_cost_per_token": 1e-06, "model_map_value.output_cost_per_token": 2e-06}
    
  3. Read the spend log row

    $ psql "$DATABASE_URL" -c "select model_group, model_id, spend, metadata->'model_map_information'->>'model_map_key' as model_map_key, metadata->'model_map_information'->'model_map_value'->>'input_cost_per_token' as input_rate, metadata->'model_map_information'->'model_map_value'->>'output_cost_per_token' as output_rate from \"LiteLLM_SpendLogs\" where model_group = 'priced-alias'"
     model_group  |     model_id      |  spend  |   model_map_key   | input_rate | output_rate 
    --------------+-------------------+---------+-------------------+------------+-------------
     priced-alias | priced-deployment | 2.2e-05 | openai/gpt-6-luna | 0.000001   | 0.000002
    

Explicit zero-priced deployment (zero-alias, 0 / 0)

  1. Send the request

    $ curl -si http://localhost:4124/v1/chat/completions -H 'Authorization: Bearer $LITELLM_MASTER_KEY' -H 'Content-Type: application/json' -d '{"model": "zero-alias", "messages": [{"role": "user", "content": "hi"}]}' | grep -iE '^HTTP|x-litellm-model-id|x-litellm-response-cost:'
    HTTP/1.1 200 OK
    x-litellm-model-id: zero-deployment
    
  2. Read what the callback received in standard_logging_object.model_map_information (the callback appends it to slo.jsonl)

    $ cat slo.jsonl | grep zero-alias
    {"model_group": "zero-alias", "model_id": "zero-deployment", "response_cost": 0.0, "model_map_key": "openai/gpt-6-luna", "model_map_value.input_cost_per_token": 0, "model_map_value.output_cost_per_token": 0}
    
  3. Read the spend log row

    $ psql "$DATABASE_URL" -c "select model_group, model_id, spend, metadata->'model_map_information'->>'model_map_key' as model_map_key, metadata->'model_map_information'->'model_map_value'->>'input_cost_per_token' as input_rate, metadata->'model_map_information'->'model_map_value'->>'output_cost_per_token' as output_rate from \"LiteLLM_SpendLogs\" where model_group = 'zero-alias'"
     model_group  |     model_id      |  spend  |   model_map_key   | input_rate | output_rate 
    --------------+-------------------+---------+-------------------+------------+-------------
     zero-alias   | zero-deployment   |       0 | openai/gpt-6-luna | 0          | 0
    

Deployment with no price of its own (plain-alias)

  1. Send the request

    $ curl -si http://localhost:4124/v1/chat/completions -H 'Authorization: Bearer $LITELLM_MASTER_KEY' -H 'Content-Type: application/json' -d '{"model": "plain-alias", "messages": [{"role": "user", "content": "hi"}]}' | grep -iE '^HTTP|x-litellm-model-id|x-litellm-response-cost:'
    HTTP/1.1 200 OK
    x-litellm-model-id: plain-deployment
    x-litellm-response-cost: 4e-06
    
  2. Read what the callback received in standard_logging_object.model_map_information (the callback appends it to slo.jsonl)

    $ cat slo.jsonl | grep plain-alias
    {"model_group": "plain-alias", "model_id": "plain-deployment", "response_cost": 4e-06, "model_map_key": "openai/gpt-6-luna", "model_map_value.input_cost_per_token": 1e-07, "model_map_value.output_cost_per_token": 5e-07}
    
  3. Read the spend log row

    $ psql "$DATABASE_URL" -c "select model_group, model_id, spend, metadata->'model_map_information'->>'model_map_key' as model_map_key, metadata->'model_map_information'->'model_map_value'->>'input_cost_per_token' as input_rate, metadata->'model_map_information'->'model_map_value'->>'output_cost_per_token' as output_rate from \"LiteLLM_SpendLogs\" where model_group = 'plain-alias'"
     model_group  |     model_id      |  spend  |   model_map_key   | input_rate | output_rate 
    --------------+-------------------+---------+-------------------+------------+-------------
     plain-alias  | plain-deployment  |   4e-06 | openai/gpt-6-luna | 0.0000001  | 0.0000005
    

Type

🐛 Bug Fix

Caveats (if any)

Low

  • model_map_value for a custom-priced deployment now carries that deployment's entry instead of the catalog entry, so fields other than the two rates (max_input_tokens, supported_openai_params, and so on) come from the deployment's registered entry. model_map_key is unchanged
  • Proof uses a local OpenAI-compatible stub with fixed usage counts, no real provider call. The code path only reads the usage counts and the cost map, so a real provider changes the numbers, not the behavior

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/a2d63811dfbf4b969587ba4e1eae1f65
Open in Devin Desktop: https://app.devin.ai/desktop/session/a2d63811dfbf4b969587ba4e1eae1f65?variant=devin
Requested by: @kerrylu-berri


Note

Low Risk
Logging metadata only; billing logic is unchanged and model_map_key behavior is preserved.

Overview
Standard logging payloads were showing catalog token rates in model_map_information even when a router deployment billed at custom or zero prices, so spend logs and callbacks disagreed with response_cost.

get_model_cost_information now accepts router_model_id and, when present, resolves model_map_value via the same select_model_name_for_cost_calc path billing already uses (deployment id in the cost map). model_map_key is unchanged (still the catalog model name for grouping).

get_standard_logging_object_payload passes logging_obj.get_router_model_id() into that helper. A parametrized unit test covers custom and explicit zero deployment pricing in the payload.

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

…ormation

Billing resolved the deployment's own cost-map entry and logging ran a second,
name-only lookup, so callbacks and spend logs showed the catalog rates for a
custom-priced deployment. completion_cost now stores the entry it billed from on
the logging object and the standard logging payload reads it, falling back to the
old lookup only when billing never ran.
@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

Drop the logging-object plumbing and keep the lookup where it was:
get_standard_logging_object_payload now hands the deployment id to
get_model_cost_information as router_model_id, the same id billing uses, so
the deployment's own cost-map entry is reported instead of the catalog entry.
@codspeed

codspeed Bot commented Oct 17, 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

✅ 32 untouched benchmarks


Comparing litellm_lit9660_model_map_from_billing (1554428) with main (2e427c6)

Open in CodSpeed

@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.

@codecov

codecov Bot commented Oct 17, 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

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@kerry-berri

kerry-berri commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

litellm-stampbot: risk low

Approved at 1554428.

Rules (stampbot/risk_rules.yml):

  • low: no rule flagged it

Cursor Bugbot: Low Risk at 1554428. Logging metadata only; billing logic is unchanged and model_map_key behavior is preserved. 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.

Stale Bugbot comment from a previous run.

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

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@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 1554428. Configure here.

@kerry-berri kerry-berri added risk:low Small, isolated, low-risk change and removed risk:high Sensitive, broad, or high-risk change labels Oct 19, 2026

@kerry-berri kerry-berri 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.

Low risk by rule and by judgement at 1554428

@klu-berri
klu-berri merged commit 1212260 into main Oct 19, 2026
98 of 99 checks passed
@klu-berri
klu-berri deleted the litellm_lit9660_model_map_from_billing branch October 19, 2026 20:38
pamanseau pushed a commit to KubeTEE-AI/litellm that referenced this pull request Oct 25, 2026
…ormation (BerriAI#47182)

Backport of BerriAI#47182 to stable/1.103.x.
Cherry-picked from 1212260 (main).
Test-file conflict resolved by keeping the line's file and appending this PR's tests.
Calls the line's _select_model_name_for_cost_calc, which is the private name on this line.
Cartofante pushed a commit to CartoDB/litellm that referenced this pull request Oct 26, 2026
…ormation (BerriAI#47182)

Backport of BerriAI#47182 to stable/1.106.x.
Cherry-picked from 1212260 (main).
Test conflict resolved by keeping the line's file plus this PR's own tests.
yuneng-berri pushed a commit that referenced this pull request Oct 28, 2026
…ormation (#47182)

* fix(logging): report the cost-map entry billing used in model_map_information

Billing resolved the deployment's own cost-map entry and logging ran a second,
name-only lookup, so callbacks and spend logs showed the catalog rates for a
custom-priced deployment. completion_cost now stores the entry it billed from on
the logging object and the standard logging payload reads it, falling back to the
old lookup only when billing never ran.

* fix(logging): pass the deployment id into the logging cost-map lookup

Drop the logging-object plumbing and keep the lookup where it was:
get_standard_logging_object_payload now hands the deployment id to
get_model_cost_information as router_model_id, the same id billing uses, so
the deployment's own cost-map entry is reported instead of the catalog entry.

* test(logging): mirror the proxy metadata in the deployment-price regression test

* fix(logging): keep the catalog model_map_key, report only the deployment's own rates

---------

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

Backport of BerriAI#47182 to stable/1.105.x.
Cherry-picked from 1212260 (main).
Adapted to the line: calls the line's _select_model_name_for_cost_calc (public on main) and the test uses the line's _response_cost_calculator and a local payload import; the test is appended since its main-side neighbours are not on the line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:low Small, isolated, low-risk change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Deployment custom pricing is billed correctly but callback and spend-log model-map rates use backend prices

3 participants