Repository navigation
fix(cost): treat rate-less cost map rows as rule-only when ranking cost candidates - #44322
devin-ai-integration[bot] wants to merge 3 commits into
Conversation
…able response models Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
bugbot run |
|
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>
|
bugbot run |
There was a problem hiding this comment.
✅ 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>
|
bugbot run |
| 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}"): |
There was a problem hiding this comment.
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.
| 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}"): |
There was a problem hiding this comment.
when does this happen lol
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
TLDR
Problem this solves:
base_modelstill bills $0 when a sibling deployment serves that nameHow it solves it:
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 namehaiku-base-rule(anthropic/claude-haiku-4-5,base_model: claude-opus-9) and a second deployment onanthropic/claude-opus-9"model": "haiku-base-rule"x-litellm-response-cost-original: 0.0, so budgets and spend logs count nothingAfter: the same request is billed at the deployment's own rate
x-litellm-response-cost: 5.9e-05, which is claude-haiku-4-5's rate for 9 input and 10 output tokensPre-Submission checklist
uv run pytest tests/unit/<your_test_file>.py -vScreenshots / Proof of Fix
Shared setup: a proxy on each commit with this config, no DB, real Anthropic key
Before (f9a32ff)
x-litellm-response-cost-original: 0.0,x-litellm-response-cost-input: 0.0,x-litellm-response-cost-output: 0.0, nox-litellm-response-costheader, usage{"prompt_tokens":9,"completion_tokens":10}After (67f3346)
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_modelwhile 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_keyand treats a map row as priced only when it has numeric*cost*fields—not merely when the key exists._prices_only_via_capability_rulenow uses that check (including provider-prefixed names) and evaluatesmatch_capability_generalizationsacross 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_modellookup.Reviewed by Cursor Bugbot for commit 549d7f6. Bugbot is set up for automated code reviews on this repo. Configure here.