From b2e1ca09812cb1ecbb178450227be2e346c8a66e Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Wed, 25 Nov 2026 09:06:53 -0800 Subject: [PATCH] feat(usage): each share of a run's summary says why it went unpriced and which rates it assumed, with its cache reads and writes A run's cost often reads "unpriced" or "at least" with nothing saying why. The ledger's entries carry the reason (unpriced_reason: no_model, no_price, no_tokens) and the rates a cost assumed (cost_assumed), but the summary dropped both, so no reader of its JSON shape could explain the figure. Each share now keeps them, as unpriced_reasons and cost_assumed, beside its cache reads and writes; to_dict carries all four. The CLI's lines are unchanged. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 4 +++- src/agent_env/task/usage/summary.py | 22 +++++++++++++++----- tst/unit/task/usage/summary_test.py | 31 ++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index f3c71422d..a786f55c6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -404,7 +404,9 @@ orchestrators call it the same way. What callers rely on: it holds every attempt and generation. `task/usage/summary.py` sums it by role, then by model, for the run summaries (`agent-env run`, `eval run`, `task run`), unpriced usage counted apart, never as $0, and a cost priced on an assumed rate (`cost_assumed`) shown as at most. Its `to_dict` is the summary's one JSON shape, with the `Bound` the - CLI prints (`exact`, `at_least`, `at_most`, `estimated`, `unpriced`), for any reader that shows a run's cost. A + CLI prints (`exact`, `at_least`, `at_most`, `estimated`, `unpriced`), for any reader that shows a run's cost, and + each share's cache reads and writes, why any of it went unpriced (`unpriced_reasons`) and the rates it assumed + (`cost_assumed`). A runner that doesn't set `ATTEMPT_EPOCH` writes entries with no attempt, kept by step and role alone: exact for a run without retries, a lower bound for one with. A runner that writes the instance some other way loses that spend from the ledger. diff --git a/src/agent_env/task/usage/summary.py b/src/agent_env/task/usage/summary.py index 2fac394f3..d0d636b3c 100644 --- a/src/agent_env/task/usage/summary.py +++ b/src/agent_env/task/usage/summary.py @@ -7,7 +7,7 @@ from __future__ import annotations from collections.abc import Iterable -from dataclasses import dataclass +from dataclasses import dataclass, field from enum import StrEnum from functools import reduce from typing import TYPE_CHECKING, Any @@ -35,16 +35,21 @@ class Bound(StrEnum): @dataclass class Share: """One role's usage on one model: its tokens, each None when nothing counted it, and the cost of the part that was - priced, with ``unpriced`` when some part had no cost, ``assumed`` when some part's cost was priced on an assumed - rate (``cost_assumed``), and ``partial`` when its counts fall short (a partial or a still running entry).""" + priced, with ``unpriced`` when some part had no cost and ``unpriced_reasons`` the reasons its entries gave, + ``assumed`` when some part's cost was priced on an assumed rate and ``cost_assumed`` the rates assumed, and + ``partial`` when its counts fall short (a partial or a still running entry).""" role: str model: str | None input_tokens: int | None = None output_tokens: int | None = None + cache_read_tokens: int | None = None + cache_write_tokens: int | None = None cost_usd: float | None = None unpriced: bool = False + unpriced_reasons: set[str] = field(default_factory=set) assumed: bool = False + cost_assumed: set[str] = field(default_factory=set) partial: bool = False @property @@ -61,17 +66,24 @@ def bound(self) -> Bound: def to_dict(self) -> dict[str, Any]: return {"role": self.role, "model": self.model, "cost_usd": self.cost_usd, "bound": self.bound.value, - "input_tokens": self.input_tokens, "output_tokens": self.output_tokens, "unpriced": self.unpriced, - "assumed": self.assumed, "partial": self.partial} + "input_tokens": self.input_tokens, "output_tokens": self.output_tokens, + "cache_read_tokens": self.cache_read_tokens, "cache_write_tokens": self.cache_write_tokens, + "unpriced": self.unpriced, "unpriced_reasons": sorted(self.unpriced_reasons), "assumed": self.assumed, + "cost_assumed": sorted(self.cost_assumed), "partial": self.partial} def add(self, part: UsageEntry | ModelEntry, partial: bool) -> None: self.input_tokens = _plus(self.input_tokens, part.get("input_tokens")) self.output_tokens = _plus(self.output_tokens, part.get("output_tokens")) + self.cache_read_tokens = _plus(self.cache_read_tokens, part.get("cache_read_tokens")) + self.cache_write_tokens = _plus(self.cache_write_tokens, part.get("cache_write_tokens")) if (cost := part.get("cost_usd")) is None: self.unpriced = True + if reason := part.get("unpriced_reason"): + self.unpriced_reasons.add(reason) else: self.cost_usd = (self.cost_usd or 0.0) + cost self.assumed = self.assumed or bool(part.get("cost_assumed")) + self.cost_assumed.update(part.get("cost_assumed") or ()) self.partial = self.partial or partial diff --git a/tst/unit/task/usage/summary_test.py b/tst/unit/task/usage/summary_test.py index 095b1d321..8d293f613 100644 --- a/tst/unit/task/usage/summary_test.py +++ b/tst/unit/task/usage/summary_test.py @@ -156,15 +156,40 @@ def test_a_summary_is_one_json_shape_with_the_bound_word_on_its_total_and_each_s "attempts": 2, "sources": ["harness", "litellm@1.80.0"], "shares": [ {"role": "solver", "model": "gpt-4.1-mini", "cost_usd": pytest.approx(0.0005), "bound": "estimated", - "input_tokens": 1200, "output_tokens": 12, "unpriced": False, "assumed": True, "partial": True}, + "input_tokens": 1200, "output_tokens": 12, "cache_read_tokens": None, "cache_write_tokens": None, + "unpriced": False, "unpriced_reasons": [], "assumed": True, "cost_assumed": ["cache_read_rate"], + "partial": True}, {"role": "user_simulator", "model": "gpt-4.1-nano", "cost_usd": 0.0002, "bound": "exact", - "input_tokens": None, "output_tokens": None, "unpriced": False, "assumed": False, "partial": False}, + "input_tokens": None, "output_tokens": None, "cache_read_tokens": None, "cache_write_tokens": None, + "unpriced": False, "unpriced_reasons": [], "assumed": False, "cost_assumed": [], "partial": False}, {"role": "judge", "model": "mystery-1", "cost_usd": None, "bound": "unpriced", "input_tokens": 300, - "output_tokens": 3, "unpriced": True, "assumed": False, "partial": False}]} + "output_tokens": 3, "cache_read_tokens": None, "cache_write_tokens": None, "unpriced": True, + "unpriced_reasons": ["no_price"], "assumed": False, "cost_assumed": [], "partial": False}]} assert json.loads(json.dumps(shape)) == shape assert summary.to_dict(attempts=False)["attempts"] is None +def test_each_share_says_why_it_is_unpriced_and_which_rates_it_assumed_with_its_cache_counts(): + rows = [{"model": "a", "input_tokens": 9, "output_tokens": 1, "cache_read_tokens": 6, "cost_usd": 0.2, + "cost_assumed": ["cache_write_rate"]}, + {"model": "a", "input_tokens": 3, "output_tokens": 1, "cost_usd": None, "unpriced_reason": "no_price"}] + summary = summarize([ + _entry("solver", "a", 12, 2, 0.2, models=rows), + _entry("solver", "a", 100, 5, 0.01, cache_read_tokens=40, cache_write_tokens=8, + cost_assumed=["cache_read_rate", "cache_write_rate"]), + _entry("judge", "j", None, None, None, unpriced_reason="no_tokens"), + _entry("judge", "j", 5, 1, None, unpriced_reason="no_price"), + _entry("judge", "j", 5, 1, None), + ]) + + solver, judge = summary.to_dict()["shares"] + assert (solver["cache_read_tokens"], solver["cache_write_tokens"]) == (46, 8) + assert solver["unpriced_reasons"] == ["no_price"] + assert solver["cost_assumed"] == ["cache_read_rate", "cache_write_rate"] + assert judge["unpriced_reasons"] == ["no_price", "no_tokens"] and judge["cost_assumed"] == [] + assert (judge["cache_read_tokens"], judge["cache_write_tokens"]) == (None, None) + + def test_the_closing_line_names_each_source_of_the_costs(): priced = [_entry("solver", "m", 1, 1, 0.1)] reported = [{**_entry("judge", "j", 1, 1, 0.2), "cost_source": "harness"},