From 3201e85d65cfd1df64db05fc11ec7b32bb466ad1 Mon Sep 17 00:00:00 2001 From: er-s-an <92594860+er-s-an@users.noreply.github.com> Date: Mon, 12 Oct 2026 09:21:03 +0800 Subject: [PATCH 1/2] fix(huggingface): respect registered model_cost for TEI rerank pricing Skip the synthetic zero-cost huggingface ModelInfo fallback when the model is already present in litellm.model_cost, so input_cost_per_query on a TEI rerank deployment is billed instead of always returning $0. Fixes #45975 --- litellm/utils.py | 2 +- tests/unit/test_cost_calculator.py | 31 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/litellm/utils.py b/litellm/utils.py index 44dc75f45d7..e87e1e1fddc 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -6097,7 +6097,7 @@ def get_model_info_helper( e, ) - if custom_llm_provider == "huggingface": + if custom_llm_provider == "huggingface" and model not in litellm.model_cost: max_tokens: Final = _get_max_position_embeddings(model_name=model) return ModelInfoBase( key=model, diff --git a/tests/unit/test_cost_calculator.py b/tests/unit/test_cost_calculator.py index 8dbef772472..d238a953c74 100644 --- a/tests/unit/test_cost_calculator.py +++ b/tests/unit/test_cost_calculator.py @@ -1049,6 +1049,37 @@ def test_tiered_pricing_only_deployment_completion_cost_is_nonzero(): assert cost > 0 +def test_huggingface_rerank_uses_registered_input_cost_per_query(): + """Registered per-query price must apply for huggingface/TEI rerank (#45975). + + ``_get_model_info_helper`` used to return a synthetic zero-cost huggingface + description before consulting ``litellm.model_cost``, so a deployment priced + only via ``input_cost_per_query`` always billed $0 under that provider. + """ + from litellm.cost_calculator import rerank_cost + from litellm.utils import cached_get_model_info_helper + + cached_get_model_info_helper.cache_clear() + litellm.model_cost.pop("hf-tei-rerank-deployment", None) + litellm.register_model({"hf-tei-rerank-deployment": {"input_cost_per_query": 0.001}}) + + cost = rerank_cost( + model="hf-tei-rerank-deployment", + custom_llm_provider="huggingface", + billed_units={"search_units": 1}, + ) + assert cost == (0.001, 0.0) + + # Unregistered huggingface models still get the synthetic zero-cost fallback. + cached_get_model_info_helper.cache_clear() + info = litellm.get_model_info( + model="unregistered-hf-tei-model-xyz", + custom_llm_provider="huggingface", + ) + assert info["input_cost_per_token"] == 0 + assert info["output_cost_per_token"] == 0 + + def test_per_query_priced_rerank_deployment_completion_cost_is_nonzero(): """A rerank deployment priced only via ``input_cost_per_query`` must resolve cost against its ``router_model_id`` entry: the shared backend alias has From a6f9e518813aec7d6d4f65d547ac4cfa225edbdc Mon Sep 17 00:00:00 2001 From: er-s-an <92594860+er-s-an@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:16:10 +0800 Subject: [PATCH 2/2] fix(huggingface): use typed registered-model lookup --- litellm/utils.py | 2 +- tests/unit/test_cost_calculator.py | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index e87e1e1fddc..b2c62639363 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -6097,7 +6097,7 @@ def get_model_info_helper( e, ) - if custom_llm_provider == "huggingface" and model not in litellm.model_cost: + if custom_llm_provider == "huggingface" and get_model_cost_key(model) is None: max_tokens: Final = _get_max_position_embeddings(model_name=model) return ModelInfoBase( key=model, diff --git a/tests/unit/test_cost_calculator.py b/tests/unit/test_cost_calculator.py index d238a953c74..f5c0949da94 100644 --- a/tests/unit/test_cost_calculator.py +++ b/tests/unit/test_cost_calculator.py @@ -1049,7 +1049,18 @@ def test_tiered_pricing_only_deployment_completion_cost_is_nonzero(): assert cost > 0 -def test_huggingface_rerank_uses_registered_input_cost_per_query(): +@pytest.mark.parametrize( + ("registered_model", "model"), + [ + ("hf-tei-rerank-deployment", "hf-tei-rerank-deployment"), + ("huggingface/hf-tei-rerank-deployment", "huggingface/hf-tei-rerank-deployment"), + ("hf-tei-rerank-deployment", "HF-TEI-RERANK-DEPLOYMENT"), + ], +) +@pytest.mark.usefixtures("local_model_cost_map") +def test_huggingface_rerank_uses_registered_input_cost_per_query( + registered_model: str, model: str, request: pytest.FixtureRequest +): """Registered per-query price must apply for huggingface/TEI rerank (#45975). ``_get_model_info_helper`` used to return a synthetic zero-cost huggingface @@ -1060,11 +1071,12 @@ def test_huggingface_rerank_uses_registered_input_cost_per_query(): from litellm.utils import cached_get_model_info_helper cached_get_model_info_helper.cache_clear() - litellm.model_cost.pop("hf-tei-rerank-deployment", None) - litellm.register_model({"hf-tei-rerank-deployment": {"input_cost_per_query": 0.001}}) + request.addfinalizer(cached_get_model_info_helper.cache_clear) + litellm.model_cost.pop(registered_model, None) + litellm.register_model({registered_model: {"input_cost_per_query": 0.001}}) cost = rerank_cost( - model="hf-tei-rerank-deployment", + model=model, custom_llm_provider="huggingface", billed_units={"search_units": 1}, )