mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
fix(decisions): keep a token-priced row on token billing when it also sets a query price
A plain input_cost_per_query bills flat only on a row with no token price, and input_cost_per_query_decisions always bills flat, so a custom Decisions row that already bills tokens costs the same as before this PR
This commit is contained in:
parent
54dac276d3
commit
56039b9207
4 changed files with 47 additions and 13 deletions
|
|
@ -24,7 +24,11 @@ from litellm.constants import (
|
|||
)
|
||||
from litellm.litellm_core_utils.core_helpers import as_str_mapping, billed_usage_cost
|
||||
from litellm.litellm_core_utils.cost_discount import resolve_cost_discount
|
||||
from litellm.litellm_core_utils.decisions_pricing import decisions_pricing_fields, is_decisions_call_type
|
||||
from litellm.litellm_core_utils.decisions_pricing import (
|
||||
decisions_pricing_fields,
|
||||
decisions_request_price,
|
||||
is_decisions_call_type,
|
||||
)
|
||||
from litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR
|
||||
from litellm.litellm_core_utils.llm_cost_calc.tool_call_cost_tracking import (
|
||||
StandardBuiltInToolCostTracking,
|
||||
|
|
@ -604,11 +608,9 @@ def cost_per_token(
|
|||
decisions_overrides: Final = decisions_pricing_fields(
|
||||
litellm.model_cost.get(decisions_model_info["key"], {}) # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType] # model cost map is dynamically loaded
|
||||
)
|
||||
request_price: Final = decisions_overrides.get(
|
||||
"input_cost_per_query", decisions_model_info.get("input_cost_per_query")
|
||||
)
|
||||
if isinstance(request_price, (int, float)):
|
||||
return float(request_price), 0.0
|
||||
request_price: Final = decisions_request_price(decisions_model_info, decisions_overrides)
|
||||
if request_price is not None:
|
||||
return request_price, 0.0
|
||||
if decisions_overrides:
|
||||
return generic_cost_per_token(
|
||||
model=model,
|
||||
|
|
|
|||
|
|
@ -27,6 +27,15 @@ def decisions_pricing_fields(row: Mapping[str, object]) -> Mapping[str, object]:
|
|||
}
|
||||
|
||||
|
||||
def decisions_request_price(model_info: Mapping[str, object], overrides: Mapping[str, object]) -> float | None:
|
||||
rates: Final = {**model_info, **overrides}
|
||||
request_price: Final = rates.get("input_cost_per_query")
|
||||
if not isinstance(request_price, (int, float)):
|
||||
return None
|
||||
bills_tokens: Final = any(rates.get(field) for field in ("input_cost_per_token", "output_cost_per_token"))
|
||||
return None if bills_tokens and "input_cost_per_query" not in overrides else float(request_price)
|
||||
|
||||
|
||||
def apply_decisions_pricing(row: Mapping[str, object]) -> Mapping[str, object]:
|
||||
"""Return ``row`` priced for a Decisions call: ``_decisions`` values replace their plain fields."""
|
||||
plain: Final = {key: value for key, value in row.items() if not key.endswith(DECISIONS_PRICING_SUFFIX)}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,3 @@
|
|||
"""Parallel's decision model uses System One questions and the native fast mode.
|
||||
|
||||
Both LiteLLM Decisions routes share this adapter. Citations describe the whole
|
||||
request, so they stay on the response rather than being assigned to answers.
|
||||
Contract: https://docs.parallel.ai/api-reference/decisions/make-web-grounded-typed-decisions
|
||||
"""
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import Final
|
||||
|
||||
|
|
|
|||
|
|
@ -77,6 +77,36 @@ def test_flat_decisions_rate_overrides_catalog_without_changing_chat(
|
|||
) == pytest.approx((100 * 1e-07, 0.0))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("call_type", ["adecisions", "asystemone"])
|
||||
@pytest.mark.parametrize(
|
||||
("token_rates", "expected_costs"),
|
||||
[
|
||||
({"input_cost_per_token": 1e-05, "output_cost_per_token": 0.0}, (100 * 1e-05, 0.0)),
|
||||
({"input_cost_per_token": 0.0, "output_cost_per_token": 1e-05}, (0.0, 10 * 1e-05)),
|
||||
],
|
||||
)
|
||||
def test_plain_query_price_leaves_a_token_priced_row_billing_tokens(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
call_type: str,
|
||||
token_rates: dict[str, float],
|
||||
expected_costs: tuple[float, float],
|
||||
) -> None:
|
||||
model: Final = "fireworks_ai/accounts/example/models/priced-both"
|
||||
monkeypatch.setitem(
|
||||
litellm.model_cost,
|
||||
model,
|
||||
{"input_cost_per_query": 0.5, **token_rates, "litellm_provider": "fireworks_ai", "mode": "evaluation"},
|
||||
)
|
||||
|
||||
assert cost_calculator.cost_per_token(
|
||||
model=model,
|
||||
custom_llm_provider="fireworks_ai",
|
||||
prompt_tokens=100,
|
||||
completion_tokens=10,
|
||||
call_type=cast(CallTypesLiteral, call_type),
|
||||
) == pytest.approx(expected_costs)
|
||||
|
||||
|
||||
_LUNA_USAGE: Final = Usage(
|
||||
prompt_tokens=100,
|
||||
completion_tokens=10,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue