Answer stability round 1: 1 🔴, 2 🟡 (0 misread); keep f-strings 3.11-valid, count Agent calls without a transcript across subagents, pin the undated message's rung

This commit is contained in:
2026-10-28 11:43:46 +01:00
parent bb3eb4fac6
commit 69f05a8c3c
4 changed files with 31 additions and 11 deletions
+17 -7
View File
@@ -54,7 +54,7 @@ class Reports:
@dataclasses.dataclass(frozen=True)
class ChunkFigures:
"""dollars maps each of DIMENSIONS to its labels' dollars; start is the rung a routed chunk started on, None where no item routed it; escalated counts the worker's escalations by trigger; unattributed is the recorded cost that no message of the chunk takes; lacking counts the subagents the worker launched that the archive holds no transcript of."""
"""dollars maps each of DIMENSIONS to its labels' dollars; start is the rung a routed chunk started on, None where no item routed it; escalated counts the worker's escalations by trigger; unattributed is the recorded cost of a model with no priced message in the chunk; lacking counts the Agent calls, the worker's and the subagents', that have no subagent transcript in the archive."""
dollars: dict[str, collections.Counter[str]]
start: Rung | None
rounds_past_first: int
@@ -89,7 +89,7 @@ class Message:
class Spent(typing.NamedTuple):
"""A message and whose it is: the subagent type, or `worker`, and the rung it ran on."""
"""A message with the subagent type that wrote it, or `worker`, and the rung it ran on."""
kind: str
rung: str
message: Message
@@ -120,7 +120,7 @@ def measured(chunk: Chunk, home: str) -> ChunkFigures:
reports=reports,
escalated=escalations(launches),
unattributed=unattributed,
lacking=max(0, len(launches) - len(chunk.subagents)),
lacking=max(0, len(launches) + sum(len(agent_calls(subagent.entries)) for subagent in chunk.subagents) - len(chunk.subagents)),
)
@@ -143,7 +143,7 @@ def agent_calls(worker: list[dict]) -> dict[str, tuple[str, dict]]:
# {"type": "cost-state", "totalCostUSD": 40.1, "modelUsage": {"claude-opus-5-5": {"costUSD": 40.1, "outputTokens": 334504, ...}}, ...}
def recorded_costs(worker: list[dict]) -> dict[str, float]:
"""What the worker transcript's last cost-state entry records each model cost. Claude Code counts every call of the session in it, the subagents' and the compactions' too."""
"""The cost the worker transcript's last cost-state entry records for each model. Claude Code counts every call of the session in it, the subagents' and the compactions' too."""
for entry in reversed(worker):
if entry.get("type") == "cost-state" and isinstance(by_model := entry.get("modelUsage"), dict):
return {model: cost for model, usage in by_model.items() if isinstance(usage, dict) and isinstance(cost := usage.get("costUSD"), (int, float))}
@@ -151,7 +151,7 @@ def recorded_costs(worker: list[dict]) -> dict[str, float]:
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, which counts it mostly as the worker's; `lacking` counts those subagents. A model the record omits keeps its list prices; a recorded model with no priced message is the cost no message takes."""
"""Each message's dollars by DIMENSIONS, and the recorded cost left over, that of a recorded model with no priced message. 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 archived messages of the model that ran it, in the same proportion. A model the record omits keeps its list prices."""
listed_by_model: collections.Counter[str] = collections.Counter()
for _, _, message in spent:
listed_by_model[message.model] += message.list_price
@@ -183,7 +183,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."""
"""The assistant messages, counted once each, 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:
@@ -262,11 +262,21 @@ def mean(chunks: list[ChunkFigures], of: typing.Callable[[ChunkFigures], int]) -
return statistics.mean(map(of, chunks))
def basis(figures: Figures) -> str:
"""What the dollars are, what they leave out, and where the archive falls short of the recorded cost."""
text = "Dollars are what each worker transcript's last `cost-state` entry records for each model, cache reads included. A model's dollars are split across its messages in the archive in proportion to their cost at list prices, so the cost of a compaction, or of a subagent transcript the archive lacks, lands on the archived messages of the model that ran it. A chunk without the entry, or a model it leaves out, keeps the list prices, which may be a little off. 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."
if figures.unattributed:
text += f" {money(figures.unattributed)} of the recorded cost is left out, since it belongs to a model with no list price or with no priced message in the archive."
if figures.lacking:
text += f" The archive holds no subagent transcript for {figures.lacking} of the chunks' Agent calls."
return text
def render(figures: Figures, found: list[str], at: str, cycle: int, since: str) -> 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, so a subagent transcript the archive lacks puts its cost mostly on the worker's messages; 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' {figures.lacking} launched subagent{"" if figures.lacking == 1 else "s"} {"has" if figures.lacking == 1 else "have"} no transcript in the archive.' if figures.lacking 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 ''}. {basis(figures)}",
"## 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()]))],