litellm/tests/unit/llms/dashscope
maciej-berri 0ba2d7933c
refactor(cost): price generic_cost_per_token with the native calculate (#52816)
* refactor(cost): price generic_cost_per_token with the native calculate

litellm-cost gains Schedule::is_open, Quote and calculate, and generic_cost_per_token now lowers the row into NativePrices and prices the billed quantities there. Quote is the single rate rule: band pick, service tier fallback, tiered_pricing, off-peak, cache read and write, modality and reasoning rates, unit meters. calculate multiplies usage over it, sums each side in the order Python did and applies the residency, FedRAMP and regional endpoint uplifts

Python keeps reading provider usage into counts (billed_quantities), the provider wrappers, custom pricing, the Anthropic and Bedrock geo and speed multipliers and the batch path. get_billed_token_rates, calculate_prompt_caching_savings and the Perplexity and Dashscope cost files read rates from quote. The Python rate functions, the Fireworks cache-read default module and the price lookup reference fixture are deleted

model-catalog adds billed_prices, a lowering for billing that never fails, because a deployment that overrides a flat-priced row with tiered_pricing holds both spellings and Prices::try_from rejects it. Prices::try_from stays strict for the catalog guard

* fix(cost): keep main's billing for tiered and malformed rows

The first cut of the native pricing path changed the bill for a few row shapes no catalog row has. Each one now has a test that failed first and passes on main

billed_prices returns BilledPrices, Flat or Tiered. A row whose token tiers price input bills an empty prompt from its flat prices, since main picks no tier for it, so a tier-only row bills it nothing. A tier's missing cache and reasoning rates are that tier's input and output rate, filled before any off-peak swap, which also stops a Fireworks row with tiers from reading its cache at half price. A token tier starts where the one before it ends, so a ladder written with a one token gap bills the next tier from its first token. Result tiers are left to the search calculator. A modality input token price that does not parse is 0 again

The off-peak reader reports a malformed window or weekday and keeps the well-formed ones, where it used to drop the whole block

A batch or decisions call, or one with custom pricing, gets a quote with no rates instead of online rates, and the speed and geo uplifts apply when the row carries them

NativeTokenUsage names its quantities input_*, cached_*, cache_write_* and output_*, and billed_cache_read_rate is the one place that picks the explicit or implicit cache-read rate

* fix(cost): borrow the call tier's image token rate for image-only output

Main now bills an image-only row's output at output_cost_per_image_token for the request's service tier, falling back to the standard image rate. Quote::text_output read only the standard image rate, so a priority call kept billing the standard price after the rebase

* docs(cost): plainer billed_quantities docstring, drop 'main' from calculate's doc

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: narmstrong-berri <narmstrong@berri.ai>
2026-11-23 22:00:27 +00:00
..
__init__.py
test_dashscope_chat_transformation.py
test_dashscope_common_utils.py
test_dashscope_cost_calculator.py refactor(cost): price generic_cost_per_token with the native calculate (#52816) 2026-11-23 22:00:27 +00:00
test_dashscope_embedding_transformation.py
test_dashscope_image_edit_transformation.py
test_dashscope_messages_transformation.py
test_dashscope_rerank_transformation.py
test_dashscope_text_to_speech_transformation.py
test_dashscope_video_transformation.py feat(videos): add first-class OpenRouter video request fields (#50319) 2026-11-06 16:50:48 -08:00
test_qwen_brand_aliases.py