Skip to content

Replay your coding agent sessions as a map and a timeline

fix(waste): price each request at its long-context tier

Muhammad Aiman68d681a30a83
diff --git a/python/src/agentplay/_core/ingestion/coding_agent/session_pure.py b/python/src/agentplay/_core/ingestion/coding_agent/session_pure.py
index fbbd94f..9c55161 100644
--- a/python/src/agentplay/_core/ingestion/coding_agent/session_pure.py
+++ b/python/src/agentplay/_core/ingestion/coding_agent/session_pure.py
@@ -270,10 +270,18 @@ _CACHE_WRITE_5M_MULTIPLIER = 1.25
_CACHE_WRITE_1H_MULTIPLIER = 2.0
-def _component_rates(model_id: str | None) -> tuple[float, float, float, float, float] | None:
+def _component_rates(
+ model_id: str | None, request: tuple[int, int, int, int, int] | None = None
+) -> tuple[float, float, float, float, float] | None:
"""Per-Mtok USD rates ``(fresh_input, cache_read, cache_write_5m,
cache_write_1h, output)`` for a model, or None when unpriceable.
+ ``request`` is ONE request's token components. When its cache-inclusive
+ input crosses the card's ``long_context_threshold``, the whole request is
+ billed at the long-context tier, the same per-request rule as
+ ``builder.estimate_cost_usd``. Turn aggregates pass no ``request``: they
+ can sum several requests, so they stay at base rates.
+
Resolves through the ONE rate card (``agentplay._core.pricing.MODEL_RATES``
via ``normalize_model_key``) — no local price table. Missing cache keys
fall back to the standard multipliers above, which reproduce the
@@ -286,13 +294,22 @@ def _component_rates(model_id: str | None) -> tuple[float, float, float, float,
if not entry:
return None
input_rate = float(entry["input"])
- return (
+ rates = (
input_rate,
float(entry.get("cache_read", input_rate * _CACHE_READ_MULTIPLIER)),
float(entry.get("cache_write", input_rate * _CACHE_WRITE_5M_MULTIPLIER)),
input_rate * _CACHE_WRITE_1H_MULTIPLIER,
float(entry["output"]),
)
+ threshold = entry.get("long_context_threshold")
+ if request is None or threshold is None or sum(request[:4]) <= threshold:
+ return rates
+ in_mult = float(entry["long_context_input_multiplier"])
+ *input_rates, output_rate = rates
+ return (
+ *(rate * in_mult for rate in input_rates),
+ output_rate * float(entry["long_context_output_multiplier"]),
+ )
def _turn_token_components(spans: list[SpanData]) -> dict[int, tuple[int, int, int, int, int]]:
@@ -492,7 +509,7 @@ def _marginal_waste_usd(
# would make every waste figure unknown for nothing
# Positive usage still must price.
continue
- rates = _component_rates(event.get("model"))
+ rates = _component_rates(event.get("model"), component)
if rates is None:
return _none
full += _turn_cost_usd(component, rates)
diff --git a/python/tests/core/test_waste_long_context.py b/python/tests/core/test_waste_long_context.py
new file mode 100644
index 0000000..3b70a7e
--- /dev/null
+++ b/python/tests/core/test_waste_long_context.py
@@ -0,0 +1,53 @@
+"""Marginal waste prices each request at its own long-context tier."""
+
+from __future__ import annotations
+
+from datetime import UTC, datetime
+
+import pytest
+
+from agentplay._core.ingestion.coding_agent.session_pure import _marginal_waste_usd
+from agentplay._core.ingestion.coding_agent.spans import SpanData
+
+
+def _turn(n: int, *requests: tuple[int, int]) -> SpanData:
+ """A turn whose ``(input, output)`` requests are its whole billed usage."""
+ now = datetime.now(UTC)
+ events = [{"model": "gpt-6-luna", "input_tokens": inp, "output_tokens": out} for inp, out in requests]
+ return SpanData(
+ span_id=f"s{n}",
+ trace_id="t",
+ parent_id="root",
+ name=f"Turn {n}",
+ span_kind="LLM",
+ status_code="OK",
+ start_time=now,
+ end_time=now,
+ attributes={
+ "llm.token_count.input_fresh": sum(inp for inp, _ in requests),
+ "llm.token_count.completion": sum(out for _, out in requests),
+ "llm.cost_events": events,
+ },
+ )
+
+
+def _retry_usd(*turns: SpanData) -> float:
+ return _marginal_waste_usd(list(turns), [], {1}, set(), "gpt-6-luna")["retry_waste_marginal_usd"]
+
+
+def test_a_long_request_in_a_retry_turn_is_priced_at_the_long_context_tier():
+ # gpt-6-luna: $0.10 in / $0.50 out per 1M; above 272K input the whole
+ # request bills at 2x input and 1.5x output ($0.20 / $0.75).
+ usd = _retry_usd(_turn(1, (300_000, 10_000)), _turn(2, (1_000, 1_000)))
+ assert usd == pytest.approx(300_000 * 0.20 / 1e6 + 10_000 * 0.75 / 1e6)
+
+
+def test_requests_below_the_threshold_stay_at_base_rates_even_when_the_turn_is_long():
+ # Two 200K requests: the turn sums to 400K but neither request crosses 272K.
+ usd = _retry_usd(_turn(1, (200_000, 0), (200_000, 0)), _turn(2, (1_000, 1_000)))
+ assert usd == pytest.approx(400_000 * 0.10 / 1e6)
+
+
+def test_a_request_exactly_at_the_threshold_is_not_long_context():
+ usd = _retry_usd(_turn(1, (272_000, 0)), _turn(2, (1_000, 1_000)))
+ assert usd == pytest.approx(272_000 * 0.10 / 1e6)