Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(ptu): clear a PTU deployment's tiered_pricing instead of zeroing it

mateo-berri5970754a8583
diff --git a/litellm/proxy/management_endpoints/model_management_endpoints.py b/litellm/proxy/management_endpoints/model_management_endpoints.py
index 912e18150b3..45a15d0d1f1 100644
--- a/litellm/proxy/management_endpoints/model_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/model_management_endpoints.py
@@ -342,14 +342,17 @@ def _validate_ptu_model_info(model_info: Mapping[str, object]) -> None:
)
-# The six mirrored pricing fields plus the three remaining fields
+# The mirrored per-token pricing fields plus the three remaining fields
# Router._inherit_builtin_cache_pricing back-fills from the public cost map. An unset field is
# what that back-fill targets, so a field left out here is one a PTU deployment still bills.
-_PTU_ZEROED_PRICING_FIELDS: Final = SPECIAL_MODEL_INFO_PARAMS + (
+# tiered_pricing is the one mirrored field that is a list, not a rate, so it is dropped from a
+# PTU deployment (see _PTU_CLEARED_PRICING_FIELDS) rather than stored as zero.
+_PTU_ZEROED_PRICING_FIELDS: Final = tuple(f for f in SPECIAL_MODEL_INFO_PARAMS if f != "tiered_pricing") + (
"cache_creation_input_token_cost_above_1hr",
"cache_creation_input_token_cost_above_200k_tokens",
"cache_read_input_token_cost_above_200k_tokens",
)
+_PTU_CLEARED_PRICING_FIELDS: Final = frozenset({"tiered_pricing"})
_PTU_ZEROED_PRICING: Final[Mapping[str, float]] = MappingProxyType(dict.fromkeys(_PTU_ZEROED_PRICING_FIELDS, 0.0))
_NO_PRICING_OVERRIDE: Final[Mapping[str, float]] = MappingProxyType({})
_EMPTY_MODEL_INFO: Final[Mapping[str, object]] = _NO_PRICING_OVERRIDE
@@ -378,7 +381,12 @@ def _raise_if_ptu_deployment_is_priced(*, model_info: Mapping[str, object], supp
return
if model_info.get("ptu_count") is None or model_info.get("cost_per_ptu_per_hour") is None:
return
- priced: Final = tuple(sorted(field for field in _CUSTOM_PRICING_FIELDS if _is_nonzero_price(supplied.get(field))))
+ priced: Final = tuple(
+ sorted(
+ tuple(field for field in _CUSTOM_PRICING_FIELDS if _is_nonzero_price(supplied.get(field)))
+ + tuple(field for field in _PTU_CLEARED_PRICING_FIELDS if supplied.get(field))
+ )
+ )
if not priced:
return
raise HTTPException(
@@ -448,7 +456,7 @@ def _ptu_pricing_delta(
supplied: Final = patch.litellm_params.model_dump(exclude_none=True) if patch.litellm_params else _EMPTY_MODEL_INFO
zeroed: Final = _ptu_zeroed_pricing(model_info=model_info, litellm_params=litellm_params, supplied=supplied)
if zeroed:
- return zeroed, frozenset()
+ return zeroed, _PTU_CLEARED_PRICING_FIELDS
was_ptu: Final = any(stored_model_info.get(field) is not None for field in _PTU_PRICED_PAIR)
if not was_ptu or not _explicitly_cleared_ptu_fields(patch.model_info) & _PTU_PRICED_PAIR:
return _NO_PRICING_OVERRIDE, frozenset()
@@ -466,11 +474,13 @@ def _ptu_priced_deployment(model_params: Deployment) -> Deployment:
override: Final = _ptu_zeroed_pricing(model_info=model_info, litellm_params=litellm_params, supplied=litellm_params)
if not override:
return model_params
+ cleared: Final = dict.fromkeys(_PTU_CLEARED_PRICING_FIELDS, None)
+ pricing_update: Final = MappingProxyType(dict(override, **cleared))
return model_params.model_copy(
update=MappingProxyType(
{
- "litellm_params": model_params.litellm_params.model_copy(update=override),
- "model_info": model_params.model_info.model_copy(update=override),
+ "litellm_params": model_params.litellm_params.model_copy(update=pricing_update),
+ "model_info": model_params.model_info.model_copy(update=pricing_update),
}
)
)
diff --git a/tests/test_litellm/proxy/management_endpoints/test_ptu_model_settings.py b/tests/test_litellm/proxy/management_endpoints/test_ptu_model_settings.py
index 6e670e48b6a..5af880038eb 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_ptu_model_settings.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_ptu_model_settings.py
@@ -777,6 +777,53 @@ class TestPtuDeploymentsAreNotBilledPerToken:
assert exc.value.status_code == 400
assert field in str(exc.value.detail)
+ def test_a_tiered_price_the_caller_supplies_is_refused(self):
+ """Tier rates bill the traffic per token just as surely as a flat rate does."""
+ with pytest.raises(HTTPException) as exc:
+ self._zeroed(model_info=self.PTU, supplied={"tiered_pricing": [{"range": [0, 100], "input_cost_per_token": 1e-06}]})
+ assert exc.value.status_code == 400
+ assert "tiered_pricing" in str(exc.value.detail)
+
+ def test_tiered_pricing_already_on_the_row_is_cleared_not_zeroed(self):
+ """tiered_pricing is a list, so the zero the other fields store would not even validate.
+ Left in place it would keep billing per token at the tier rates."""
+ tiers = [{"range": [0, 128000], "input_cost_per_token": 3e-06}]
+ priced = _ptu_priced_deployment(
+ Deployment(
+ model_name="tiered",
+ litellm_params=LiteLLM_Params(model="openai/gpt-4o"),
+ model_info=ModelInfo(
+ id="dep-tiered",
+ team_id="t",
+ tiered_pricing=tiers,
+ ptu_effective_from=datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc),
+ **self.PTU,
+ ),
+ )
+ )
+ assert priced.litellm_params.tiered_pricing is None
+ assert priced.model_info.tiered_pricing is None
+
+ written = update_db_model(
+ db_model=Deployment(
+ model_name="tiered",
+ litellm_params=LiteLLM_Params(model="openai/gpt-4o", tiered_pricing=tiers),
+ model_info=ModelInfo(id="dep-tiered", team_id="t"),
+ ),
+ updated_patch=updateDeployment(
+ model_info=ModelInfo(
+ id="dep-tiered",
+ team_id="t",
+ ptu_effective_from=datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc),
+ **self.PTU,
+ )
+ ),
+ )
+ for blob in ("model_info", "litellm_params"):
+ stored = json.loads(written[blob])
+ assert "tiered_pricing" not in stored, blob
+ assert stored["input_cost_per_token"] == 0, blob
+
def test_a_price_the_caller_supplies_as_zero_is_accepted(self):
assert self._zeroed(model_info={**self.PTU, "input_cost_per_token": 0}, supplied={"input_cost_per_token": 0})[
"input_cost_per_token"
@@ -1098,8 +1145,10 @@ class TestPtuDeploymentsAreNotBilledPerToken:
)
written = add_team_model_to_db.call_args.kwargs["model_params"]
- assert all(getattr(written.model_info, field, None) == 0 for field in SPECIAL_MODEL_INFO_PARAMS)
+ assert all(getattr(written.model_info, field, None) == 0 for field in SPECIAL_MODEL_INFO_PARAMS if field != "tiered_pricing")
+ assert written.model_info.tiered_pricing is None
assert all(written.litellm_params.get(field) == 0 for field in _PTU_ZEROED_PRICING_FIELDS)
+ assert written.litellm_params.tiered_pricing is None
@pytest.mark.asyncio
async def test_model_new_refuses_a_priced_ptu_deployment(self):