mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(cost_calculator): keep deployment provider when response hidden provider differs
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
4b368bf066
commit
40d3d1cca2
2 changed files with 54 additions and 12 deletions
|
|
@ -773,6 +773,23 @@ def _get_hidden_str_for_cost_calc(hidden_params: object, key: str) -> str | None
|
|||
return value if isinstance(value, str) and value else None
|
||||
|
||||
|
||||
def _router_model_id_has_pricing(router_model_id: str | None) -> bool:
|
||||
if router_model_id is None:
|
||||
return False
|
||||
entry: Final = litellm.model_cost.get(router_model_id)
|
||||
if entry is None:
|
||||
return False
|
||||
return any(
|
||||
entry.get(cost_key) is not None
|
||||
for cost_key in (
|
||||
"input_cost_per_token",
|
||||
"input_cost_per_second",
|
||||
"input_cost_per_query",
|
||||
"tiered_pricing",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _select_model_name_for_cost_calc(
|
||||
model: str | None,
|
||||
completion_response: object | None,
|
||||
|
|
@ -809,17 +826,8 @@ def _select_model_name_for_cost_calc(
|
|||
)
|
||||
|
||||
if custom_pricing is True:
|
||||
if router_model_id is not None and router_model_id in litellm.model_cost:
|
||||
entry: Final = litellm.model_cost[router_model_id]
|
||||
if (
|
||||
entry.get("input_cost_per_token") is not None
|
||||
or entry.get("input_cost_per_second") is not None
|
||||
or entry.get("input_cost_per_query") is not None
|
||||
or entry.get("tiered_pricing") is not None
|
||||
):
|
||||
return_model = router_model_id
|
||||
else:
|
||||
return_model = model
|
||||
if _router_model_id_has_pricing(router_model_id):
|
||||
return_model = router_model_id
|
||||
else:
|
||||
return_model = model
|
||||
|
||||
|
|
@ -1446,7 +1454,8 @@ def completion_cost(
|
|||
|
||||
hidden_params = getattr(completion_response, "_hidden_params", None)
|
||||
if hidden_params is not None:
|
||||
custom_llm_provider = hidden_params.get("custom_llm_provider", custom_llm_provider or None)
|
||||
if not (custom_pricing is True and _router_model_id_has_pricing(router_model_id)):
|
||||
custom_llm_provider = hidden_params.get("custom_llm_provider", custom_llm_provider or None)
|
||||
region_name = hidden_params.get("region_name", region_name)
|
||||
|
||||
# For Gemini/Vertex AI responses, trafficType is stored in
|
||||
|
|
|
|||
|
|
@ -834,6 +834,39 @@ def test_custom_pricing_cost_calc_uses_router_model_id_from_litellm_metadata():
|
|||
assert custom_model_id not in (selected_model_no_custom or "")
|
||||
|
||||
|
||||
def test_completion_cost_keeps_custom_pricing_provider_when_hidden_provider_differs(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
):
|
||||
deployment_id = "azure-ai-custom-pricing-provider-test"
|
||||
input_rate = 0.0004
|
||||
output_rate = 0.0011
|
||||
monkeypatch.setitem(
|
||||
litellm.model_cost,
|
||||
deployment_id,
|
||||
{
|
||||
"input_cost_per_token": input_rate,
|
||||
"output_cost_per_token": output_rate,
|
||||
"litellm_provider": "azure_ai",
|
||||
},
|
||||
)
|
||||
|
||||
response = ModelResponse(
|
||||
model="azure_ai/neutral-test-model",
|
||||
usage=Usage(prompt_tokens=2, completion_tokens=3, total_tokens=5),
|
||||
)
|
||||
response._hidden_params = {"custom_llm_provider": "openai"}
|
||||
|
||||
cost = completion_cost(
|
||||
completion_response=response,
|
||||
model="azure_ai/neutral-test-model",
|
||||
custom_llm_provider="azure_ai",
|
||||
custom_pricing=True,
|
||||
router_model_id=deployment_id,
|
||||
)
|
||||
|
||||
assert cost == 2 * input_rate + 3 * output_rate
|
||||
|
||||
|
||||
def test_per_request_custom_pricing_with_router():
|
||||
"""When custom pricing is passed as per-request kwargs (not in model_list),
|
||||
_select_model_name_for_cost_calc should fall back to the model name
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue