From b3fc2aa4d443732ace67ddb242e5fbade8da7587 Mon Sep 17 00:00:00 2001 From: Parinith Date: Sun, 10 May 2026 20:16:45 +0530 Subject: [PATCH] fix(cost_calculator): return (0.0, 0.0) for unrecognized models instead of raising cost_per_token() now catches exceptions from get_llm_provider() (unknown provider) and _cached_get_model_info_helper() (model not in cost map) and returns (0.0, 0.0) gracefully. completion_cost() is transitively fixed since it delegates to cost_per_token(). Closes #27581 Co-Authored-By: Claude Sonnet 4.6 --- litellm/cost_calculator.py | 14 ++++++++++---- tests/test_litellm/test_cost_calculator.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index 9b4dd80265c..0a8b960a9f3 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -350,7 +350,10 @@ def cost_per_token( # noqa: PLR0915 ): # use region based pricing, if it's available model_with_provider = model_with_provider_and_region else: - _, custom_llm_provider, _, _ = litellm.get_llm_provider(model=model) + try: + _, custom_llm_provider, _, _ = litellm.get_llm_provider(model=model) + except Exception: + return 0.0, 0.0 model_without_prefix = model model_parts = model.split("/", 1) if len(model_parts) > 1: @@ -545,9 +548,12 @@ def cost_per_token( # noqa: PLR0915 service_tier=service_tier, ) else: - model_info = _cached_get_model_info_helper( - model=model, custom_llm_provider=custom_llm_provider - ) + try: + model_info = _cached_get_model_info_helper( + model=model, custom_llm_provider=custom_llm_provider + ) + except Exception: + return 0.0, 0.0 if (model_info.get("input_cost_per_token") or 0.0) > 0 or ( model_info.get("output_cost_per_token") or 0.0 diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py index e53484dd287..41b7d1395a6 100644 --- a/tests/test_litellm/test_cost_calculator.py +++ b/tests/test_litellm/test_cost_calculator.py @@ -13,6 +13,7 @@ from pydantic import BaseModel import litellm from litellm.cost_calculator import ( completion_cost, + cost_per_token, handle_realtime_stream_cost_calculation, response_cost_calculator, ) @@ -2057,3 +2058,12 @@ def test_openrouter_gemini_3_1_flash_lite_preview_pricing(): assert model_info["output_cost_per_token"] == 1.5e-06 assert model_info["max_input_tokens"] == 1048576 assert model_info["max_output_tokens"] == 65536 + + +def test_cost_per_token_returns_zero_for_unknown_model(): + """ + cost_per_token() must return (0.0, 0.0) for a model that is not in the + cost map instead of raising an exception (GitHub issue #27581). + """ + result = cost_per_token(model="fake-model-xyz-123") + assert result == (0.0, 0.0)