From 4efea1f7d8d89115db9e983b957aa8a06534f64b Mon Sep 17 00:00:00 2001 From: Anuj7411 Date: Tue, 18 Aug 2026 20:20:37 +0530 Subject: [PATCH] fix(cost): treat an explicit zero input_cost_per_token as priced, not missing --- litellm/litellm_core_utils/llm_cost_calc/utils.py | 2 +- .../llm_cost_calc/test_llm_cost_calc_utils.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py index 9d6ad8b6e39..09b2128bb65 100644 --- a/litellm/litellm_core_utils/llm_cost_calc/utils.py +++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py @@ -112,7 +112,7 @@ def select_cost_metric_for_model( """ if model_info.get("input_cost_per_character"): return "cost_per_character" - elif model_info.get("input_cost_per_token"): + elif model_info.get("input_cost_per_token") is not None: return "cost_per_token" else: raise ValueError( diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py index 1402e056b72..32ebc1c6319 100644 --- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py +++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py @@ -3345,3 +3345,18 @@ def test_generic_cost_per_token_grok_46_long_context(_local_model_cost_map): ) assert prompt_cost == pytest.approx(200_000 * 4e-06 + 50_000 * 1e-06) assert completion_cost == pytest.approx(1_000 * 1.2e-05) + + +def test_select_cost_metric_for_model_treats_zero_token_cost_as_priced(): + from litellm.litellm_core_utils.llm_cost_calc.utils import select_cost_metric_for_model + + free_model = {"key": "free-model", "input_cost_per_token": 0.0} + assert select_cost_metric_for_model(free_model) == "cost_per_token" + + char_model = {"key": "char-model", "input_cost_per_character": 1e-6, "input_cost_per_token": 0.0} + assert select_cost_metric_for_model(char_model) == "cost_per_character" + + import pytest + + with pytest.raises(ValueError): + select_cost_metric_for_model({"key": "no-pricing"})