litellm/tests/unit/rust_bridge
maciej-berri aeccda17c1
feat(cost): price native /v1/messages calls in the Rust route and hand the receipt to logging (#52624)
* feat(cost): price native /v1/messages calls in the Rust route and hand the receipt to logging

The Messages route now reads Anthropic wire usage in Rust (a port of
calculate_usage plus billed_usage, captured once against main for every
P1 provider body and stream) and prices the finished call through a
Pricing dependency the Python bridge backs with the model-catalog
snapshot. A message body settles Cost::Priced at result_ready. A stream
gets Cost::Metered and the route settles its StreamMeter at message_stop
from message_start merged with the final message_delta, and injects each
usage event's cost itself when the caller asked for it.

The proxy hands the route the deployment facts Logging prices with
(deployment id, base model, Mantle region, Vertex location, residency)
through the existing settings projection, and sends none for custom
pricing, a pricing provider override or a Bedrock model_id, which keep
today's Python path.

The legacy adapter writes the receipt onto Logging through the new
record_route_cost contract function instead of popping response_cost, and
Logging, the pass-through stream handler and the proxy's stream cost
injection read it instead of pricing the call again.

* refactor(cost): keep the lint gates flat for the Rust-priced Messages hand-off

* fix(cost): test the Anthropic batch request list by truthiness instead of an untyped len

* docs(python-bridge): note RoutePricing reads its snapshot through one CostLookup per call

* docs(cost): describe price_call as the shared /v1/messages pricing body

* fix(cost): run the Claude subscription check before a Rust route's price in non-stream logging

A native /v1/messages call priced by the Rust route records its cost on the logging object early. The
non-stream success path then kept that cost as already calculated, so a call covered by a Claude
subscription logged the token price instead of 0. Route-priced calls now go through
response_cost_calculator, which checks the subscription first and then returns the route's price.

* refactor(cost): name the keep-a-precomputed-cost check so the success branch stays under PLR0916

* refactor(cost): fold the route-priced check into the precomputed-cost condition to keep the lint and type gates flat

* fix(cost): resolve a bedrock mantle/ route's pricing region from the requested model like logging does

The Rust route looked up the Mantle pricing region without the model, so a bedrock/mantle/ call dropped
its configured region and could be priced at non-regional rates. Pass the requested model, matching the
logging path.

* fix(cost): price a native Vertex call at the routed deployment's location

On the proxy the logger is created before a deployment is picked, so the route read no location from it
and priced a regional Vertex deployment at the global default. Resolve the location from the routed request
first, as dispatch does, then fall back to the logger's params.

* perf(cost): decode each stream event once when injecting the cost

With stream cost injection on, every chunk was decoded twice: the meter parsed each SSE event's JSON, then
with_injected_cost split the chunk again and reparsed each data line to find the message_delta. observe now
parses each event of a whole-frame chunk into one Value that the meter reads and the injection prices, and
with_injected_cost only matches the line text. Chunks that cannot carry an injected cost, and every chunk when
injection is off, still read the typed event straight from the text. The meter test now also runs with
injection on over a stream cut mid-event, which caught an extra pricing call per split delta in a first draft.
2026-11-23 16:27:46 -08:00
..
host feat(cost): price native /v1/messages calls in the Rust route and hand the receipt to logging (#52624) 2026-11-23 16:27:46 -08:00
messages feat(messages): serve bedrock invoke messages on rust only (#51330) 2026-11-15 16:58:31 -08:00
ocr refactor(rust-bridge): require the native extension (#48852) 2026-10-28 13:16:56 -07:00
trace refactor(lens): connect LiteLLM to the independent Lens service (#45529) 2026-10-09 16:17:05 -07:00
__init__.py
AGENTS.md refactor(rust-bridge): decide native routing in the catalog, drop the bypass predicates (#46750) 2026-10-20 15:06:24 -07:00
native_route_wheel_test.py fix(bedrock): omit system prompt from audio transcription and report its token usage (#41565) 2026-10-28 19:23:15 -07:00
stubtest.ini test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_bindings.py refactor(rust-bridge): require the native extension (#48852) 2026-10-28 13:16:56 -07:00
test_catalog.py feat(messages): serve bedrock claude_platform messages on rust only (#52681) 2026-11-19 14:34:02 -08:00
test_clickhouse.py refactor(lens): connect LiteLLM to the independent Lens service (#45529) 2026-10-09 16:17:05 -07:00
test_configuration.py ci(unit): cut unit check time with cheaper per-test resets and fewer non-unit tests (#51864) 2026-11-14 15:41:30 -08:00
test_detectors.py feat(messages): serve anthropic messages on rust only (#51119) 2026-11-12 00:07:20 +00:00
test_execution.py refactor(rust-bridge): return an unstarted Execution from native entrypoints and drive it in Python (#46299) 2026-10-14 17:50:44 -07:00
test_failures.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_fork_guard.py refactor(rust-bridge): require the native extension (#48852) 2026-10-28 13:16:56 -07:00
test_logger.py refactor(rust_bridge): send every Rust log line through one subscriber (#53738) 2026-11-23 17:53:54 +09:00
test_public_call.py fix(rust): run native async calls through python's async wrapper steps (#50423) 2026-11-10 00:24:20 +00:00
test_runtime.py feat(messages): serve anthropic messages on rust only (#51119) 2026-11-12 00:07:20 +00:00
test_token_counter.py fix(proxy): count the request's tool_choice on the token count routes (#51133) 2026-11-14 07:53:27 -05:00
test_tokenizer.py refactor(sdk): separate core AWS and tokenizer dependencies (#44447) 2026-10-08 19:07:57 -07:00
test_verify_linux_native_wheel.py chore: move scripts and runtime sources under litellm (#51006) 2026-11-12 04:19:57 +00:00