Skip to content

feat(model-catalog): candidate rows and pricing snapshot for /v1/messages Claude providers - #52472

Merged
narmstrong-berri merged 6 commits into
litellm_cost_generic_on_mainfrom
litellm_cost_model_catalog_rows
Nov 23, 2026
Merged

narmstrong-berri merged 6 commits into
litellm_cost_generic_on_mainfrom
litellm_cost_model_catalog_rows

Conversation

@maciej-berri

@maciej-berri maciej-berri commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

[insert handwritten description here]

Details

TLDR

This is layer 2 of a stack under the Rust cost port. Layer 1 (litellm_cost_generic_on_main, the base of this PR) carries #51566 rebased onto main and has its own draft PR #52816. Layer 3 (the pricing core, bridge and Python adapter) builds on this one.

Problem this solves:

  • Rust pricing needs main's row choice for /v1/messages Claude calls
  • It needs deployment rows by id, as the router stores them
  • It needs a cheap per-call view of litellm.model_cost that never bills a stale price

How it solves it:

  • Ports main's candidate row order for five Claude providers into litellm_cost::candidates
  • Defers every call main would price another way
  • Model Catalog keeps only general table lookups behind a small read trait
  • python-bridge owns everything that exists because the table is a Python dict
  • Lowers provider_specific_entry geo and speed uplifts

How the pieces split

Model Catalog answers table questions only. CatalogView is the read interface a host implements (exact key, folded key, a row's fields, its registered_fields, and lowered, which by default lowers the row each time), and PricingContext is the immutable rest (capability rules, provider names, renamed models). resolve/ keeps the get_model_info name walk (names.rs, lookup in mod.rs) and the Bedrock id spellings. Catalog implements CatalogView, so the Rust host resolves through the same code with no Python.

completion_cost's candidate walk (which names to try, in which order, deployment rows by id, the provider to bill as, when to defer) moved to litellm-cost as candidates/. It reads a catalog only through litellm-cost's CandidateTable trait, which Model Catalog implements over CostLookup, so litellm-cost never depends on Model Catalog. The parity tests against cost_candidates.json stay in Model Catalog (resolve/candidate_walk.rs) because that is the only crate that sees both, and the fixture is unchanged.

python-bridge keeps the Python-shaped parts: the generation read from Python, the snapshot that is swapped when it moves, and the row cache. The cache used to key converted rows by Python object identity, so litellm.model_cost["x"]["input_cost_per_token"] = 2e-6 after a call was priced kept billing the old rate. It now keeps each lowering with the fields it came from and reuses it only while the row reads the same fields, and each pricing call reads every row once so it sees one version of it.

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

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

E2E exception at a077291. Nothing calls the new code yet. The pricing core and the native Messages hand-off that use it land in the next layer of the stack, so there is no user flow to run before and after, and no live provider keys were available for this work. The proof is parity against main's own answers instead.

Main's choices were captured once from litellm.completion_cost(call_type="anthropic_messages") at main 532df6e plus #51566, with LITELLM_LOCAL_MODEL_COST_MAP=True and NativePrices spied, for 48 realistic calls across native Anthropic, Bedrock invoke (cross-region profiles, invoke/, claude_platform/, region paths, bedrock/mantle/), Bedrock Mantle with and without a region, Azure AI with and without base_model, and Vertex AI with @version and @default. Each case records the row main bills and, with every pricing attempt forced to fail, every row main falls through in order. The Rust test asserts the same rows in the same order, then whether main would go on to a path that is left to Python. All 48 match. A second capture registers a register_model price on a Bedrock row a cross-region profile reaches, and the Rust rows match main there too, with and without custom pricing. A new test resolves every fixture call over the Rust host's Catalog and over the Python-shaped table of the same document and asserts the same rows.

Commands and results
cd litellm/rust
CARGO_INCREMENTAL=0 CARGO_PROFILE_DEV_DEBUG=0 cargo test -p litellm-model-catalog -p litellm-llms
# model-catalog lib 367 passed (48 fixture cases among them), catalog 40, cost_map 13, llms lib 2014 passed
PYO3_PYTHON=<venv>/bin/python cargo test -p litellm-python-bridge --lib
# 494 passed
cargo clippy -p litellm-model-catalog -p litellm-llms -p litellm-python-bridge --all-targets -- -D warnings
# clean
LITELLM_LOCAL_MODEL_COST_MAP=True python -m pytest -q tests/unit/rust_bridge/host/test_model_cost.py
# 8 passed
cargo check --workspace --all-targets
# clean
ruff_strict_gate, type_discipline_gate, test_quality_gate, openapi_docs_gate --base origin/main
# OK, no rule grew

Mutation checks, each applied to the source, run against cargo test -p litellm-model-catalog --lib, then reverted. All nine were caught. Keeping main's candidate order without moving rule-only names last fails 4 fixture cases. Skipping the Mantle region key fails mantle_gov_region_row. Dropping the Bedrock cross-region profile rule fails bedrock_profile_over_served_base_id. Pricing a Bedrock register_model price through the normal lookup fails the registration case. Ignoring the row provider match fails 20 cases. Dropping the served-model preference fails 4 cases. Stripping only one repeated provider prefix fails anthropic_thrice_repeated_prefix. Keeping the Mantle region in the split name fails the names test, and dropping the geo lowering fails 4 lowering and snapshot tests.

Restack

On 2026-11-19 the whole stack was rebased onto main, and layer 1 now has its own draft PR #52816.

On 2026-11-20 the snapshot and the candidate walk were split as described in "How the pieces split", and this PR's new tip is f6d635ee49. At that tip and at the two PRs above it, the cost, model-catalog, inference-messages and python-bridge Rust tests pass (python-bridge's logger test needs the venv's Python, as before), clippy and fmt are clean, the cost_candidates.json parity cases pass unchanged, and the in-place edit regression test in #52484 fails on the old tip and passes now. A rerun of the native /v1/messages SDK A/B set for Anthropic, Bedrock invoke and Azure AI (1080 calls) gives costs identical to the earlier run.

Type

🆕 New Feature
🧹 Refactoring

Caveats (if any)

Medium

  • Calls the port does not cover come back Deferred, so Python keeps pricing them
    • other providers, Model Router and Together or Replicate names, Bedrock inference profiles, hidden response facts
  • Main's quirks are copied and listed in the model-catalog AGENTS.md
    • seven deployment price views disagree, only the stored id row is exposed
  • Mantle region is a fact the caller passes in, since main reads env and secrets for it
  • The candidate walk's parity tests live in Model Catalog, not beside the walk in litellm-cost, because litellm-cost cannot depend on the catalog

Low

  • Bedrock id helpers moved from litellm-llms into model-catalog, llms re-exports them. The two the billing walk needs (routing prefix strip and the cross-region profile check) live in litellm_cost::candidates::bedrock and Model Catalog re-exports them, since the rest needs litellm-auth-aws
  • make check could not install litellm-admin-mcp (403) or run the dashboard API sync without node_modules
    • the lint gates ran directly, and type_check_gate could not provision its environment for the same reason

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

@CLAassistant

CLAassistant commented Nov 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@veria-ai

veria-ai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

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

LGTM overall. Some minor changes

@narmstrong-berri
narmstrong-berri force-pushed the litellm_cost_model_catalog_rows branch from 35c7f0a to 3f32986 Compare November 23, 2026 20:37
@narmstrong-berri
narmstrong-berri enabled stacked auto-merge (squash) November 23, 2026 20:47
maciej-berri and others added 6 commits November 23, 2026 13:44
Adjustments now carry the geo multipliers and the speed: fast multiplier
main reads out of provider_specific_entry, so the Rust pricing core can
apply them from the lowered row.
…ages Claude providers

Snapshot::candidates ports main's row choice for a finished call
(select_model_name_for_cost_calc, the completion_cost candidate loop,
cost_per_token key normalisation and the get_model_info walk) for
anthropic, bedrock, bedrock_mantle, azure_ai and vertex_ai. Every call
main would price by another path comes back Deferred so Python keeps it.

A Snapshot reads a host's live table through LiveTable, keyed on the
model cost version and the cost discount and margin config identities,
and lowers rows lazily with a cache keyed by row object. Deployment rows
are exposed by id as RowRef with Registered provenance.

The Bedrock model id helpers move here from litellm-llms, which
re-exports them, so routes and lookups share one copy. Parity is pinned
by 48 calls whose rows main billed and tried were captured once.
NativeModelCostMap::cost_snapshot hands out the model-catalog Snapshot of
the published table for its current generation. LiveRow is the snapshot's
row, identified by the row object, and invalidate() also forgets cached
rows so changed_in_place() and registration passes reread them. The host
module gains generation() and known_names(), the two facts only Python
has. The row-to-fields reader is shared with NativePrices.
…the Python snapshot into python-bridge

The shared core now reads a host table through CatalogView (key, folded key, fields,
registered_fields, lowered) plus an immutable PricingContext, and resolve/ runs on a
borrowed CostLookup. Generation, the swap-on-change manager and the identity-keyed row
cache move to python-bridge, which keeps lowerings by row contents so a price edited in
place in litellm.model_cost is billed on the next call. Catalog implements the view, so
the Rust host resolves through the same path.
…log into litellm-cost

model-catalog keeps the general lookups (exact key, folded key, the get_model_info walk,
lowered rows) and implements litellm-cost's new CandidateTable with row facts only.
The walk, its row and deferral types and the Bedrock profile rule now live in
litellm_cost::candidates, so the catalog holds no completion_cost billing rules. The
parity tests stay in model-catalog because litellm-cost cannot see a catalog.
…efusal, move the walk parity tests under resolve/tests
@narmstrong-berri
narmstrong-berri force-pushed the litellm_cost_model_catalog_rows branch from 3f32986 to 13b06ea Compare November 23, 2026 21:47
@narmstrong-berri
narmstrong-berri merged commit 6c093a3 into main Nov 23, 2026
69 of 116 checks passed
@narmstrong-berri
narmstrong-berri deleted the litellm_cost_model_catalog_rows branch November 23, 2026 22:00
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.

3 participants