From 8abcd13a9d1be9f79907eee269edafa9a40639bd Mon Sep 17 00:00:00 2001 From: Kerry Lu Date: Sun, 15 Nov 2026 12:04:41 -0800 Subject: [PATCH] fix(cost): keep a deployment's per-second video price over the catalog row's flat price (#51688) * fix(cost): keep a deployment's per-second video price over the catalog row's flat price * fix(cost): drop the superseded flat video price from the registration kept for reloads * test(cost): derive the per-second rate from the catalog row so the regression holds at any vendor price * fix(cost): keep the flat video price when only a resolution tier is priced per second * test(integration): assert the body sent to fal in the per-second price test --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Nate Armstrong --- litellm/router_rust/surface/registry.py | 4 +- litellm/rust_bridge/host/model_cost.py | 9 +- litellm/utils.py | 17 +++- .../providers/test_fal_ai_video_wire.py | 53 +++++++++++ .../unit/rust_bridge/host/test_model_cost.py | 16 +++- tests/unit/test_video_generation.py | 87 +++++++++++++++++++ 6 files changed, 179 insertions(+), 7 deletions(-) diff --git a/litellm/router_rust/surface/registry.py b/litellm/router_rust/surface/registry.py index 48387c5e605..946820f0100 100644 --- a/litellm/router_rust/surface/registry.py +++ b/litellm/router_rust/surface/registry.py @@ -133,6 +133,7 @@ from litellm.types.utils import ( ) from litellm.utils import ( ModelInfoLookupMiss, + base_fields_replaced_by, catalog_with_runtime_registrations, get_potential_model_names, get_secret, @@ -340,10 +341,11 @@ def _inherited_off_peak_base_rates( backend_entry: Final = _backend_cost_entry(host, backend_model, provider) if backend_entry is None: return {} + superseded: Final = base_fields_replaced_by(model_info) return { field: copy.deepcopy(value) for field, value in backend_entry.items() - if model_info.get(field) is None and value is not None + if model_info.get(field) is None and value is not None and field not in superseded } diff --git a/litellm/rust_bridge/host/model_cost.py b/litellm/rust_bridge/host/model_cost.py index cfedbb64592..ad743c63498 100644 --- a/litellm/rust_bridge/host/model_cost.py +++ b/litellm/rust_bridge/host/model_cost.py @@ -46,6 +46,11 @@ def merged( def kept(previous: dict[str, object] | None, registration: Mapping[str, object]) -> dict[str, object]: """The registration ``register_model(persist_across_reloads=True)`` keeps for a key: ``registration`` merged into the one kept before it.""" - from litellm.utils import update_dictionary + from litellm.utils import base_fields_replaced_by, update_dictionary - return update_dictionary(dict(previous or {}), registration) + replaced: Final = base_fields_replaced_by(registration) + return { + name: value + for name, value in update_dictionary(dict(previous or {}), registration).items() + if name not in replaced + } diff --git a/litellm/utils.py b/litellm/utils.py index 2c0dc03563c..03adc329f04 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -4319,13 +4319,24 @@ def native_structured_output_support(model_info: Mapping[str, object]) -> bool | return declared if isinstance(declared, bool) else None -def _base_fields_replaced_by(value: Mapping[str, object]) -> frozenset[str]: - return ( +def _sets_video_price_per_second(value: Mapping[str, object]) -> bool: + return any(value.get(name) is not None for name in ("output_cost_per_second", "output_cost_per_video_per_second")) + + +def base_fields_replaced_by(value: Mapping[str, object]) -> frozenset[str]: + """Base-row fields a registration supersedes without naming them.""" + explicit_cache_read: Final[frozenset[str]] = ( frozenset({"explicit_cache_read_input_token_cost"}) if value.get("cache_read_input_token_cost") is not None and value.get("explicit_cache_read_input_token_cost") is None else frozenset() ) + flat_video_price: Final[frozenset[str]] = ( + frozenset({"output_cost_per_video"}) + if value.get("output_cost_per_video") is None and _sets_video_price_per_second(value) + else frozenset() + ) + return explicit_cache_read | flat_video_price def model_cost_registration_write( @@ -4342,7 +4353,7 @@ def model_cost_registration_write( } entry: Final = { **update_dictionary(existing_model, registration), - **dict.fromkeys(_base_fields_replaced_by(registration)), + **dict.fromkeys(base_fields_replaced_by(registration)), } names: Final = current.names if isinstance(current, AliasedCostEntry) else () previous_fields: Final[frozenset[str]] = ( diff --git a/tests/integration/providers/test_fal_ai_video_wire.py b/tests/integration/providers/test_fal_ai_video_wire.py index 40a5621226c..cf6646e2410 100644 --- a/tests/integration/providers/test_fal_ai_video_wire.py +++ b/tests/integration/providers/test_fal_ai_video_wire.py @@ -3,6 +3,7 @@ import ipaddress import json import ssl import uuid +from collections.abc import Mapping from pathlib import Path from typing import Final @@ -14,6 +15,7 @@ from cryptography.x509.oid import NameOID from integration._support.client import JSON_OBJECT, Gateway, object_value, string_value from integration._support.wire import Reply, Request, wire_server from integration.providers._dashscope_video_support import rate, settled_spend +from pydantic import JsonValue _MODEL: Final = "bytedance/seedance-2.5/text-to-video" _H3_MODEL: Final = "minimax/h3/text-to-video" @@ -435,6 +437,57 @@ def test_fal_h3_oversized_size_uses_top_resolution_tier_and_queues(gateway: Gate assert [(request.method, request.target) for request in wire.drain()] == [("POST", f"/{_H3_MODEL}")] +_FLAT_PRICED_MODEL: Final = "fal-ai/ltx-video" +_FLAT_PRICED_MODEL_AS_DEPLOYMENT_ID: Final = "fal-ai/ovi" +_OFF_PEAK_NEVER_OPEN: Final[JsonValue] = {"hours_utc": "00:00-00:00", "output_cost_per_second": 0.05} + + +@pytest.mark.parametrize( + ("fal_model", "model_info"), + [ + pytest.param( + _FLAT_PRICED_MODEL, + {"off_peak_pricing": _OFF_PEAK_NEVER_OPEN}, + id="deployment_also_sets_off_peak_pricing", + ), + pytest.param( + _FLAT_PRICED_MODEL_AS_DEPLOYMENT_ID, + {"id": f"fal_ai/{_FLAT_PRICED_MODEL_AS_DEPLOYMENT_ID}"}, + id="deployment_id_equals_the_catalog_key", + ), + ], +) +def test_deployment_per_second_price_bills_a_fal_model_the_catalog_prices_per_video( + gateway: Gateway, fal_model: str, model_info: Mapping[str, JsonValue] +) -> None: + request_id: Final = "fal-flat-req-" + uuid.uuid4().hex + per_second_rate: Final = rate("fal_ai", "output_cost_per_video", fal_model) + + def respond(request: Request) -> Reply: + assert request.method == "POST" + assert request.target == f"/{fal_model}" + assert json.loads(request.body) == {"prompt": "a cat playing volleyball on a beach", "duration": "5"} + return Reply(body=json.dumps({"status": "IN_QUEUE", "request_id": request_id, "queue_position": 0}).encode()) + + with wire_server(respond) as wire, gateway.scenario() as scenario: + model: Final = scenario.model( + model=f"fal_ai/{fal_model}", + api_base=wire.url, + api_key="synthetic-fal-key", + output_cost_per_second=per_second_rate, + model_info=model_info, + ) + key: Final = scenario.key(models=[model]) + created: Final = gateway.request( + "POST", + "/v1/videos", + {"model": model, "prompt": "a cat playing volleyball on a beach", "seconds": "5"}, + key=key, + ) + assert created.status_code == 200, created.text + settled_spend(key, 5 * per_second_rate) + + def test_fal_video_without_a_resolution_bills_the_rows_default_rate(gateway: Gateway) -> None: request_id: Final = "fal-upscale-req-" + uuid.uuid4().hex source_video: Final = "https://example.com/source.mp4" diff --git a/tests/unit/rust_bridge/host/test_model_cost.py b/tests/unit/rust_bridge/host/test_model_cost.py index 66f411da83e..0a2879f1e16 100644 --- a/tests/unit/rust_bridge/host/test_model_cost.py +++ b/tests/unit/rust_bridge/host/test_model_cost.py @@ -4,7 +4,7 @@ import pytest import litellm from litellm.litellm_core_utils.model_cost_table import AliasedCostEntry -from litellm.rust_bridge.host.model_cost import merged, resolve +from litellm.rust_bridge.host.model_cost import kept, merged, resolve pytestmark = pytest.mark.usefixtures("local_model_cost_map") @@ -23,3 +23,17 @@ def test_merged_keeps_the_alias_names_and_adds_only_the_fields_a_registration_se assert row == {"mode": "chat", "x": 2, "litellm_provider": "openai"} assert (row.names, row.registered_fields) == (("owner", "alias"), frozenset({"mode", "litellm_provider"})) + + +def test_kept_registration_drops_the_flat_video_price_a_later_per_second_price_replaced() -> None: + replayed: Final = kept({"mode": "video_generation", "output_cost_per_video": 0.37}, {"output_cost_per_second": 0.1}) + + assert replayed == {"mode": "video_generation", "output_cost_per_second": 0.1} + + +def test_kept_registration_keeps_a_flat_video_price_registered_beside_a_per_second_price() -> None: + replayed: Final = kept( + {"output_cost_per_video": 0.37}, {"output_cost_per_video": 0.4, "output_cost_per_second": 0.1} + ) + + assert replayed == {"output_cost_per_video": 0.4, "output_cost_per_second": 0.1} diff --git a/tests/unit/test_video_generation.py b/tests/unit/test_video_generation.py index 0fc7dc467d6..c2f851945de 100644 --- a/tests/unit/test_video_generation.py +++ b/tests/unit/test_video_generation.py @@ -74,6 +74,93 @@ def test_completion_cost_flat_video_deployment_override_counts_videos( assert cost == pytest.approx(0.37 * count) +_OFF_PEAK: Final = {"hours_utc": "00:00-00:00", "output_cost_per_second": 0.05} + + +@pytest.mark.parametrize( + "model_info", + [ + pytest.param({"output_cost_per_second": 0.1, "off_peak_pricing": _OFF_PEAK}, id="with_off_peak_pricing"), + pytest.param({"output_cost_per_second": 0.1, "id": "openai/test-flat-video-row"}, id="id_is_the_catalog_key"), + pytest.param( + {"output_cost_per_video_per_second": 0.1, "id": "openai/test-flat-video-row"}, + id="video_per_second_key_on_the_catalog_key", + ), + ], +) +def test_deployment_per_second_video_price_beats_the_catalog_rows_flat_price(model_info: dict[str, object]) -> None: + catalog_key: Final = "openai/test-flat-video-row" + litellm.register_model( + {catalog_key: {"litellm_provider": "openai", "mode": "video_generation", "output_cost_per_video": 0.91}} + ) + try: + router: Final = litellm.Router( + model_list=[ + { + "model_name": "flat-row-per-second", + "litellm_params": {"model": catalog_key, "api_key": "synthetic"}, + "model_info": model_info, + } + ] + ) + deployment: Final = router.get_deployment_by_model_group_name("flat-row-per-second") + assert deployment is not None + logging_obj: Final = LitellmLogging( + model=catalog_key, + messages=[], + stream=False, + call_type="create_video", + start_time=datetime(2026, 1, 1), + litellm_call_id="flat-row-per-second", + function_id="flat-row-per-second", + ) + logging_obj.litellm_params = {"metadata": {"model_info": deployment.model_info.model_dump()}} + cost: Final = litellm.completion_cost( + completion_response=VideoObject( + id="vid", + object="video", + status="completed", + usage={"duration_seconds": 5.0, "video_resolution": "720p"}, + ), + model=catalog_key, + call_type="create_video", + custom_llm_provider="openai", + custom_pricing=True, + litellm_logging_obj=logging_obj, + router_model_id=deployment.model_info.id, + ) + assert cost == pytest.approx(0.5) + finally: + litellm.model_cost.pop(catalog_key, None) + + +def test_resolution_only_per_second_price_keeps_the_flat_price_for_other_resolutions() -> None: + catalog_key: Final = "openai/test-flat-video-tiered" + litellm.register_model( + {catalog_key: {"litellm_provider": "openai", "mode": "video_generation", "output_cost_per_video": 0.91}} + ) + try: + litellm.register_model({catalog_key: {"output_cost_per_second_720p": 0.1}}) + cost: Final = default_video_cost_calculator( + catalog_key, 5.0, custom_llm_provider="openai", video_resolution="1080p" + ) + assert cost == pytest.approx(0.91) + finally: + litellm.model_cost.pop(catalog_key, None) + + +def test_deployment_flat_video_price_still_wins_beside_its_own_per_second_price() -> None: + catalog_key: Final = "openai/test-flat-video-kept" + litellm.register_model( + {catalog_key: {"litellm_provider": "openai", "mode": "video_generation", "output_cost_per_video": 0.91}} + ) + try: + litellm.register_model({catalog_key: {"output_cost_per_video": 0.37, "output_cost_per_second": 0.1}}) + assert default_video_cost_calculator(catalog_key, 5.0, custom_llm_provider="openai") == pytest.approx(0.37) + finally: + litellm.model_cost.pop(catalog_key, None) + + class TestVideoGeneration: """Test suite for video generation functionality."""