From 0c1e92943aa4dabff61ad7a393aa3e9484d02692 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Wed, 28 Oct 2026 11:26:12 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20principles=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=203=20=F0=9F=9F=A1=20(0=20misread);=20name=20Spen?= =?UTF-8?q?t,=20derive=20cache-write=20prices,=20say=20a=20missing=20subag?= =?UTF-8?q?ent's=20cost=20lands=20on=20the=20held=20messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/routing_figures/measure.py | 26 +++++++++++++++++--------- bin/routing_figures/prices.py | 22 ++++++++++++++-------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/bin/routing_figures/measure.py b/bin/routing_figures/measure.py index 59fe340f..65094f7e 100644 --- a/bin/routing_figures/measure.py +++ b/bin/routing_figures/measure.py @@ -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()]))], diff --git a/bin/routing_figures/prices.py b/bin/routing_figures/prices.py index ed12c985..cac3c3c4 100644 --- a/bin/routing_figures/prices.py +++ b/bin/routing_figures/prices.py @@ -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), }