Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 17 additions & 5 deletions src/agent_env/task/usage/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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


Expand Down
31 changes: 28 additions & 3 deletions tst/unit/task/usage/summary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
Loading