Skip to content

Commit d89d3c7

Browse files
committed
fix(sync_cost_map): skip vercel rows whose output/cache tier breakpoints have no matching input tier
The runtime cost calculator only walks input_cost_per_token_above_* keys and looks up output/cache tiers at the same threshold, so a tier whose breakpoint is not also an input breakpoint is stored but never billed. Reject such rows as Unmappable instead of undercharging them.
1 parent 9478a32 commit d89d3c7

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎scripts/sync_cost_map.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,32 @@ def _tiered(name: str, base: float | None, tiers: Sequence[VercelTier] | None) -
261261

262262

263263
def _vercel_tiers(pricing: VercelPricing, cache_read: float | None, cache_write: float | None) -> Prices | Unmappable:
264-
parts: Final = (
265-
_tiered("input_cost_per_token", _token_price(pricing.input), pricing.input_tiers),
264+
input_part: Final = _tiered("input_cost_per_token", _token_price(pricing.input), pricing.input_tiers)
265+
dependents: Final = (
266266
_tiered("output_cost_per_token", _token_price(pricing.output), pricing.output_tiers),
267267
_tiered("cache_read_input_token_cost", cache_read, pricing.input_cache_read_tiers),
268268
_tiered("cache_creation_input_token_cost", cache_write, pricing.input_cache_write_tiers),
269269
)
270+
parts: Final = (input_part, *dependents)
270271
problem: Final = next((part for part in parts if isinstance(part, Unmappable)), None)
271272
if problem is not None:
272273
return problem
274+
input_thresholds: Final = frozenset(
275+
name.rsplit("_above_", 1)[1] for part in (input_part,) if not isinstance(part, Unmappable) for name in part
276+
)
277+
orphan: Final = next(
278+
(
279+
name
280+
for part in dependents
281+
if not isinstance(part, Unmappable)
282+
for name in part
283+
if name.rsplit("_above_", 1)[1] not in input_thresholds
284+
),
285+
None,
286+
)
287+
if orphan is not None:
288+
base, threshold = orphan.rsplit("_above_", 1)
289+
return Unmappable(f"{base} tier at {threshold} has no matching input tier")
273290
return MappingProxyType(
274291
{name: price for part in parts if not isinstance(part, Unmappable) for name, price in part.items()}
275292
)

‎tests/test_litellm/test_sync_cost_map.py‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,14 +572,17 @@ def test_vercel_long_context_tiers_map_to_above_threshold_prices(sync: ModuleTyp
572572
pricing: Final = {
573573
"input": "0.0000015",
574574
"input_tiers": [
575-
{"cost": "0.0000015", "min": 0, "max": 32001},
576-
{"cost": "0.0000027", "min": 32001, "max": 128001},
577-
{"cost": "0.0000045", "min": 128001},
575+
{"cost": "0.0000015", "min": 0, "max": 128001},
576+
{"cost": "0.0000027", "min": 128001, "max": 200001},
577+
{"cost": "0.0000045", "min": 200001},
578578
],
579579
"output": "0.0000075",
580580
"output_tiers": [{"cost": "0.0000075", "max": 200001}, {"cost": "0.00001125", "min": 200001}],
581581
"input_cache_read": "0.0000003",
582-
"input_cache_read_tiers": [{"cost": "0.0000006", "min": 256000}],
582+
"input_cache_read_tiers": [
583+
{"cost": "0.0000003", "min": 0, "max": 200001},
584+
{"cost": "0.0000006", "min": 200001},
585+
],
583586
"input_cache_write": "0.000002",
584587
"input_cache_write_tiers": [{"cost": "0.000002", "min": 0, "max": 200001}, {"cost": "0.000004", "min": 200001}],
585588
}
@@ -589,14 +592,37 @@ def test_vercel_long_context_tiers_map_to_above_threshold_prices(sync: ModuleTyp
589592

590593
entry: Final = outcome.cost_map["vercel_ai_gateway/acme/long"]
591594
assert entry["input_cost_per_token"] == 1.5e-6
592-
assert entry["input_cost_per_token_above_32k_tokens"] == 2.7e-6
593-
assert entry["input_cost_per_token_above_128k_tokens"] == 4.5e-6
595+
assert entry["input_cost_per_token_above_128k_tokens"] == 2.7e-6
596+
assert entry["input_cost_per_token_above_200k_tokens"] == 4.5e-6
594597
assert entry["output_cost_per_token_above_200k_tokens"] == 1.125e-5
595-
assert entry["cache_read_input_token_cost_above_256k_tokens"] == 6e-7
598+
assert entry["cache_read_input_token_cost_above_200k_tokens"] == 6e-7
596599
assert entry["cache_creation_input_token_cost_above_200k_tokens"] == 4e-6
597600
assert not any(key.endswith("_above_0k_tokens") for key in entry)
598601

599602

603+
def test_output_or_cache_tiers_without_matching_input_tier_skip_the_row(sync: ModuleType) -> None:
604+
pricing: Final = {
605+
"input": "0.0000015",
606+
"input_tiers": [
607+
{"cost": "0.0000015", "min": 0, "max": 32001},
608+
{"cost": "0.0000027", "min": 32001, "max": 128001},
609+
{"cost": "0.0000045", "min": 128001},
610+
],
611+
"output": "0.0000075",
612+
"output_tiers": [{"cost": "0.0000075", "max": 200001}, {"cost": "0.00001125", "min": 200001}],
613+
}
614+
catalog: Final = sync.load_vercel(_vercel_rows({"id": "acme/orphan", "pricing": pricing}), now_ms=NOW_MS)
615+
616+
outcome: Final = sync.compute_sync({}, (catalog,))
617+
618+
assert "vercel_ai_gateway/acme/orphan" not in outcome.cost_map
619+
assert dict(catalog.skipped) == {"tiers outside the registry's thresholds": 1}
620+
assert outcome.providers[0].warnings == (
621+
"vercel_ai_gateway/acme/orphan: output_cost_per_token tier at 200k_tokens has no matching input tier; "
622+
"row skipped",
623+
)
624+
625+
600626
@pytest.mark.parametrize(
601627
("tiers", "problem"),
602628
[

0 commit comments

Comments
 (0)