Answer principles round 1: 0 🔴, 3 🟡 (0 misread); name Spent, derive cache-write prices, say a missing subagent's cost lands on the held messages

This commit is contained in:
2026-10-28 11:26:12 +01:00
parent e83ed096c4
commit 0c1e92943a
2 changed files with 31 additions and 17 deletions
+17 -9
View File
@@ -8,9 +8,9 @@ import typing
from claude_code.entries import assistant_parts, parts, tool_call
from git_command import commits
from model_routing.route import ESCALATION_MARKER, route_from
from routing_figures.prices import listed
from model_routing.ladder import position, Rung
from round_titles import round_counts
from routing_figures.prices import listed
from shell_command import simple_commands
from transcript_archive.outcomes import UNFINISHED_AT, workers
@@ -83,12 +83,19 @@ class Message:
at: str
tools: list[str]
model: str
listed: float
list_price: float
class Spent(typing.NamedTuple):
"""A message and whose it is: the subagent type, or `worker`, and the rung it ran on."""
kind: str
rung: str
message: Message
def measured(chunk: Chunk, home: str) -> ChunkFigures:
cycles = sorted((cycle for kept in chunk.outcomes for cycle in workers(kept)), key=lambda cycle: cycle[0])
spent = [("worker", str(rung_at(cycles, message.at)), message) for message in messages(chunk.worker)]
spent = [Spent("worker", str(rung_at(cycles, message.at)), message) for message in messages(chunk.worker)]
launches = agent_calls(chunk.worker)
reported = results(chunk.worker)
delegated: collections.Counter[str] = collections.Counter()
@@ -99,7 +106,7 @@ def measured(chunk: Chunk, home: str) -> ChunkFigures:
rung = str(subagent_rung(subagent.entries, arguments, rung_at(cycles, launched_at)))
delegated[rung] += 1
reports[kind] = reports.get(kind, Reports(0, 0, 0)) + Reports(1, len(reported.get(subagent.launched_by, "")), sum(map(len, results(subagent.entries).values())))
spent += [(kind, rung, message) for message in messages(subagent.entries)]
spent += [Spent(kind, rung, message) for message in messages(subagent.entries)]
dollars, unattributed = costed(spent, chunk.project, recorded_costs(chunk.worker))
start = route_from(cycles[0][1]) if cycles else None
return ChunkFigures(
@@ -140,15 +147,15 @@ def recorded_costs(worker: list[dict]) -> dict[str, float]:
return {}
def costed(spent: list[tuple[str, str, Message]], project: str, recorded: dict[str, float]) -> tuple[dict[str, collections.Counter[str]], float]:
"""Each message's dollars by DIMENSIONS, and the recorded cost that no message takes. A model's recorded cost is split across its messages in proportion to their list prices, so the calls no message records, such as compactions, are counted. A model the record omits keeps its list prices; a recorded model with no priced message is the cost no message takes."""
def costed(spent: list[Spent], project: str, recorded: dict[str, float]) -> tuple[dict[str, collections.Counter[str]], float]:
"""Each message's dollars by DIMENSIONS, and the recorded cost that no message takes. A model's recorded cost is split across its messages in proportion to their list prices, so the calls no message records, such as compactions, are counted. A subagent transcript the archive lacks puts its cost on the messages it holds in the same proportion, since nothing says whose it was. A model the record omits keeps its list prices; a recorded model with no priced message is the cost no message takes."""
listed_by_model: collections.Counter[str] = collections.Counter()
for _, _, message in spent:
listed_by_model[message.model] += message.listed
listed_by_model[message.model] += message.list_price
scale = {model: recorded[model] / total for model, total in listed_by_model.items() if total and model in recorded}
dollars: dict[str, collections.Counter[str]] = {dimension: collections.Counter() for dimension in DIMENSIONS}
for kind, rung, message in spent:
cost = message.listed * scale.get(message.model, 1.0)
cost = message.list_price * scale.get(message.model, 1.0)
dollars["project"][project] += cost
dollars["subagent type"][kind] += cost
dollars["rung"][rung] += cost
@@ -174,6 +181,7 @@ def subagent_rung(entries: list[dict], arguments: dict, worker: Rung | str) -> R
def messages(entries: list[dict]) -> list[Message]:
"""The assistant messages, counted once each as reading_cost's tokens_in dedupes them, each with the tools whose results it took in, or `prompt` where it took none in."""
# Claude Code writes one entry per content block of a message, each with the message's usage. The usage is the same in every entry (none of 1,681 multi-entry messages in the archive of 2026-10-28 differed), so the first is kept, where reading_cost's tokens_in keeps the last.
kept: dict[str, tuple[str, dict, list[str]]] = {}
for entry in entries:
if entry.get("type") != "assistant" or not isinstance(message := entry.get("message"), dict):
@@ -255,7 +263,7 @@ def render(figures: Figures, found: list[str], at: str, cycle: int, since: str)
"""The figures as markdown; MEASURED_AT reads the cycle back. since is the latest reset's stamp, empty for none."""
sections = [
"# Routing figures",
f"Measured {at}, at cycle {cycle} across loop-claude's archive, from {figures.chunks} finished chunk{'' if figures.chunks == 1 else 's'}{f' that finished after the reset of {since}' if since else ''}. Dollars are what each worker transcript's last `cost-state` entry records for each model, cache reads included, split across the messages in the archive in proportion to their cost at list prices; a chunk without the entry keeps the list prices. A message's dollars count towards the tools whose results it took in, split evenly; a message that took in no tool result counts towards `prompt`. The judge's runs are not archived, so their cost is not counted.{f' {money(figures.unattributed)} of the recorded cost is left out, since it belongs to a model with no list price or with no message in the archive.' if figures.unattributed else ''}",
f"Measured {at}, at cycle {cycle} across loop-claude's archive, from {figures.chunks} finished chunk{'' if figures.chunks == 1 else 's'}{f' that finished after the reset of {since}' if since else ''}. Dollars are what each worker transcript's last `cost-state` entry records for each model, cache reads included, split across the messages in the archive in proportion to their cost at list prices, so a subagent transcript the archive lacks puts its cost on the messages it holds; a chunk without the entry keeps the list prices. A message's dollars count towards the tools whose results it took in, split evenly; a message that took in no tool result counts towards `prompt`. The judge's runs are not archived, so their cost is not counted.{f' {money(figures.unattributed)} of the recorded cost is left out, since it belongs to a model with no list price or with no message in the archive.' if figures.unattributed else ''}",
"## Calls for a routing change",
"".join(f"- {call}\n" for call in found).rstrip("\n") or "None.",
*[part for dimension in DIMENSIONS for part in (f"## Dollars by {dimension}", table((dimension.capitalize(), "Dollars"), [(label, money(dollars)) for label, dollars in figures.dollars[dimension].most_common()]))],
+14 -8
View File
@@ -6,20 +6,26 @@ from claude_code.entries import usage_count
@dataclasses.dataclass(frozen=True)
class Price:
"""Dollars per million tokens."""
"""Dollars per million tokens. A cache write costs 1.25 times the input for five minutes and 2 times for an hour, as Opus 5.5's $5 and $8 show."""
input: float
write_5m: float
write_1h: float
read: float
output: float
@property
def write_5m(self) -> float:
return self.input * 1.25
# Anthropic's list prices as the claude-api skill lists them (cached 2026-10-06), with Opus 5.5's cache prices from `docs/decisions.md` → `loop-claude: the worker's claude compacts…`. A cache write costs 1.25 times the input for five minutes and 2 times for an hour, as Opus 5.5's $5 and $8 show. Haiku 5.5's cache read is assumed at a tenth of its input, and its dearer prompts over 100k tokens are left out. Only the proportions between one model's messages matter, since a chunk's recorded cost sets the amounts, so these may be a little off.
@property
def write_1h(self) -> float:
return self.input * 2
# Anthropic's list prices as the claude-api skill lists them (cached 2026-10-06), with Opus 5.5's cache prices from `docs/decisions.md` → `loop-claude: the worker's claude compacts…`. Haiku 5.5's cache read is assumed at a tenth of its input, and its dearer prompts over 100k tokens are left out. Only the proportions between one model's messages matter, since a chunk's recorded cost sets the amounts, so these may be a little off.
PRICES = {
"claude-opus-5-5": Price(4, 5, 8, 0.2, 20),
"claude-sonnet-5-5": Price(2, 2.5, 4, 0.2, 10),
"claude-fable-5-1": Price(10, 12.5, 20, 0.25, 50),
"claude-haiku-5-5": Price(0.1, 0.125, 0.2, 0.01, 0.5),
"claude-fable-5-1": Price(10, 0.25, 50),
"claude-haiku-5-5": Price(0.1, 0.01, 0.5),
"claude-opus-5-5": Price(4, 0.2, 20),
"claude-sonnet-5-5": Price(2, 0.2, 10),
}