mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-22 00:31:44 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
parent
0af33fbe70
commit
b3fc2aa4d4
2 changed files with 20 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue