From ed43c74bfa9ad8367e274fc5ee1b150d9219c77e Mon Sep 17 00:00:00 2001 From: Priyanka Sadam Date: Mon, 10 Aug 2026 04:20:36 +0000 Subject: [PATCH] fix(cost_calculator): return (0.0, 0.0) for unknown openai model instead of crashing When cost_per_token() is called with custom_llm_provider='openai' and an unrecognized model name, openai_cost_per_token() raises an Exception instead of gracefully returning (0.0, 0.0). Wraps the openai_cost_per_token() call in a try/except so unknown models fall back to (0.0, 0.0), consistent with how other branches handle this. Adds a regression test to confirm the behavior. Ref: https://github.com/BerriAI/litellm/pull/27587 --- litellm/cost_calculator.py | 19 +++++++++++++------ tests/test_litellm/test_cost_calculator.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py index 6b6653c5646..1ebfe4b8bdf 100644 --- a/litellm/cost_calculator.py +++ b/litellm/cost_calculator.py @@ -599,12 +599,19 @@ def cost_per_token( elif custom_llm_provider == "bedrock": return bedrock_cost_per_token(model=model, usage=usage_block, service_tier=service_tier) elif custom_llm_provider == "openai": - return openai_cost_per_token( - model=model, - usage=usage_block, - service_tier=service_tier, - data_residency=data_residency, - ) + try: + return openai_cost_per_token( + model=model, + usage=usage_block, + service_tier=service_tier, + data_residency=data_residency, + ) + except Exception: + verbose_logger.debug( + "cost_per_token: unknown model=%s, custom_llm_provider=openai. Returning 0.0, 0.0", + model, + ) + return 0.0, 0.0 elif custom_llm_provider == "databricks": return databricks_cost_per_token(model=model, usage=usage_block) elif custom_llm_provider == "fireworks_ai": diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py index 3f024e2fd03..6eb3b6ea9ed 100644 --- a/tests/test_litellm/test_cost_calculator.py +++ b/tests/test_litellm/test_cost_calculator.py @@ -3530,3 +3530,22 @@ def test_completion_cost_prices_anthropic_shaped_cache_read_tokens(): ) assert cost == pytest.approx(3 * 5e-6 + 4014 * 5e-7 + 5 * 3e-5, rel=1e-9) +def test_cost_per_token_unknown_model_with_explicit_provider(): + """ + Test: cost_per_token() with a known provider but unknown model. + This is the second guard point flagged in PR #27587 review. + A fake model name with explicit custom_llm_provider="openai" + should not crash — it should return (0.0, 0.0). + + Ref: https://github.com/BerriAI/litellm/pull/27587 + """ + import litellm + from litellm import cost_per_token + + result = cost_per_token( + model="fake-model-xyz-123", + custom_llm_provider="openai", + prompt_tokens=10, + completion_tokens=5, + ) + assert result == (0.0, 0.0), f"Expected (0.0, 0.0) for unknown model, got {result}"