Skip to content

fix(cost): treat rate-less cost map rows as rule-only when ranking cost candidates - #44322

Closed
devin-ai-integration[bot] wants to merge 3 commits into
mainfrom
litellm_rule_only_cost_registered_row
Closed

devin-ai-integration[bot] wants to merge 3 commits into
mainfrom
litellm_rule_only_cost_registered_row

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • A rule-only base_model still bills $0 when a sibling deployment serves that name

How it solves it:

  • Cost map rows with no rate fields now count as unpriced when ordering candidates
  • The rate check resolves keys case-insensitively, like the price lookup does

User Flow

Before: a proxy admin prices a deployment through a capability-rule-only base_model, and spend reads $0 once another deployment uses that name

  1. The admin's config has haiku-base-rule (anthropic/claude-haiku-4-5, base_model: claude-opus-9) and a second deployment on anthropic/claude-opus-9
  2. A developer sends POST http://localhost:4000/v1/chat/completions with "model": "haiku-base-rule"
  3. The response comes back 200 with x-litellm-response-cost-original: 0.0, so budgets and spend logs count nothing

After: the same request is billed at the deployment's own rate

  1. Same config with both deployments
  2. The developer sends the same POST http://localhost:4000/v1/chat/completions
  3. The response carries x-litellm-response-cost: 5.9e-05, which is claude-haiku-4-5's rate for 9 input and 10 output tokens

Pre-Submission checklist

  • 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
  • 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

Screenshots / Proof of Fix

Shared setup: a proxy on each commit with this config, no DB, real Anthropic key

model_list:
  - model_name: haiku-base-rule
    litellm_params:
      model: anthropic/claude-haiku-4-5
      api_key: os.environ/ANTHROPIC_API_KEY
    model_info:
      base_model: claude-opus-9
  - model_name: opus-9-sibling
    litellm_params:
      model: anthropic/claude-opus-9
      api_key: os.environ/ANTHROPIC_API_KEY
curl -s -D - -o /tmp/body.json http://localhost:4000/v1/chat/completions -H "Content-Type: application/json" \
  -d '{"model": "haiku-base-rule", "messages": [{"role": "user", "content": "Say hi"}], "max_tokens": 10}' | grep -i "x-litellm-response-cost"
jq -c '.usage | {prompt_tokens, completion_tokens}' /tmp/body.json

Before (f9a32ff)

  1. Ran the curl above
  2. Observed x-litellm-response-cost-original: 0.0, x-litellm-response-cost-input: 0.0, x-litellm-response-cost-output: 0.0, no x-litellm-response-cost header, usage {"prompt_tokens":9,"completion_tokens":10}

After (67f3346)

  1. Ran the curl above
  2. Observed x-litellm-response-cost: 5.9000000000000004e-05, usage {"prompt_tokens":9,"completion_tokens":10}

Type

🐛 Bug Fix
✅ Test

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


Note

Medium Risk
Changes how model candidates are ordered for spend calculation when the cost map contains metadata-only entries; incorrect logic would mis-bill proxy usage and budgets.

Overview
Fixes $0 spend when a deployment uses a capability-rule-only base_model while another deployment registers the same underlying name in the cost map without token rates.

Cost calculator: Adds _has_cost_map_rates, which resolves keys via _get_model_cost_key and treats a map row as priced only when it has numeric *cost* fields—not merely when the key exists. _prices_only_via_capability_rule now uses that check (including provider-prefixed names) and evaluates match_capability_generalizations across the usual model-name variants so rate-less router registrations no longer block billing at the deployment’s real rates.

Tests: Integration coverage for sibling deployments sharing a rule-only name (stream and non-stream), plus unit tests for rate-less registered base models and mixed-case base_model lookup.

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

…able response models

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

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

CLAassistant commented Oct 3, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@kerry-berri

Copy link
Copy Markdown
Contributor

@greptileai

@kerry-berri

Copy link
Copy Markdown
Contributor

bugbot run

@greptile-apps

greptile-apps Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Adjusts cost calculation logic for unpriced model registrations.

The PR is not yet safe to merge because an unhashable response model can interrupt billing.

Findings

  1. P1 Unhashable response model breaks billing ▶

Summary

The PR changes cost-candidate ordering so rate-less cost-map rows do not take precedence over priced deployments, and adds billing coverage for sibling deployments and mixed-case base models.

  • The final revision removes the guard that skipped non-string candidates, reintroducing a billing exception for unhashable response models.

Reviews (3) · Last reviewed commit: "fix(cost): drop the non-string response ..."

Comment thread litellm/cost_calculator.py Resolved

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

@codspeed

codspeed Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_rule_only_cost_registered_row (549d7f6) with main (8efb4a2)

Open in CodSpeed

@codecov

codecov Bot commented Oct 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…-only candidates

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@kerry-berri

Copy link
Copy Markdown
Contributor

@greptileai

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

…y check

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@kerry-berri

Copy link
Copy Markdown
Contributor

@greptileai

@kerry-berri

Copy link
Copy Markdown
Contributor

bugbot run

@devin-ai-integration devin-ai-integration Bot changed the title fix(cost): treat rate-less cost map rows as rule-only and skip unhashable response models fix(cost): treat rate-less cost map rows as rule-only when ranking cost candidates Oct 3, 2026
if model is None or model in litellm.model_cost or f"{custom_llm_provider}/{model}" in litellm.model_cost:
# The router registers every deployment model in the cost map, rates or not, so an
# unpriced exact key still counts as rule-only when a capability rule matches it.
if model is None or _has_cost_map_rates(model) or _has_cost_map_rates(f"{custom_llm_provider}/{model}"):

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.

P1 Unhashable response model breaks billing
When a completion response has a list or dict in its model field, candidate sorting passes it to _has_cost_map_rates. The cost-key lookup hashes that value before the try block, raising TypeError instead of skipping it and using a valid model candidate. Billing therefore fails for that response.

Suggested change
if model is None or _has_cost_map_rates(model) or _has_cost_map_rates(f"{custom_llm_provider}/{model}"):
if not isinstance(model, str) or _has_cost_map_rates(model) or _has_cost_map_rates(f"{custom_llm_provider}/{model}"):

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.

when does this happen lol

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It doesn't in practice. Every provider path sets model to a string, and the list case only came from a hand-built gauntlet input, so the guard was dropped on purpose

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.

Pretty rare—normal OpenAI-compatible responses always have a string model, so this would only occur if a provider adapter or custom callback returned a malformed response such as model: [] or model: {...}.

_get_response_model() accepts that field, and candidate ranking then passes it to _has_cost_map_rates() → _get_model_cost_key(), which assumes the key is hashable and can raise TypeError before valid fallback candidates are considered.

So this is defensive hardening rather than part of the normal rule-only flow: it prevents one malformed provider response from breaking cost calculation.

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.

That makes sense. Given the provider paths guarantee that model is a string, a list/dict here is not a supported runtime state; my concern came from an artificial hand-built input. I withdraw the finding—the guard is not needed for this change.

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