diff --git a/bin/routing_figures/measure.py b/bin/routing_figures/measure.py index 06fe0861..67b24ea6 100644 --- a/bin/routing_figures/measure.py +++ b/bin/routing_figures/measure.py @@ -19,6 +19,8 @@ MEASURED_AT = re.compile(r"^Measured .+, at cycle ([0-9]+) across", re.MULTILINE # A rung or a subagent type is compared only once this many of its chunks or subagents are measured, so a single odd chunk calls for no change. MEASURED_AT_LEAST = 5 DIMENSIONS = ("project", "subagent type", "rung", "tool") +# Each of DIMENSIONS to its labels' dollars. +Dollars = dict[str, collections.defaultdict[str, float]] # The tool label of a message that follows no tool call, such as the first message after a prompt. PROMPT = "prompt" ESCALATION = re.compile(rf"{ESCALATION_MARKER} ([0-9]+):") @@ -55,7 +57,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 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]] + dollars: Dollars start: Rung | None rounds_past_first: int unfinished: int @@ -70,7 +72,7 @@ class ChunkFigures: class Figures: """escalated counts every chunk's escalations by trigger, routed or not; skipped names each chunk folder the archive could not read whole.""" chunks: int - dollars: dict[str, collections.Counter[str]] + dollars: Dollars routed: dict[Rung, list[ChunkFigures]] reports: dict[str, Reports] escalated: collections.Counter[int] @@ -150,13 +152,13 @@ def recorded_costs(worker: list[dict]) -> dict[str, float]: return {} -def costed(spent: list[Spent], project: str, recorded: dict[str, float]) -> tuple[dict[str, collections.Counter[str]], float]: +def costed(spent: list[Spent], project: str, recorded: dict[str, float]) -> tuple[Dollars, float]: """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() + listed_by_model: collections.defaultdict[str, float] = collections.defaultdict(float) for _, _, message in spent: 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} + dollars: Dollars = {dimension: collections.defaultdict(float) for dimension in DIMENSIONS} for kind, rung, message in spent: cost = message.list_price * scale.get(message.model, 1.0) dollars["project"][project] += cost @@ -234,7 +236,11 @@ def due(cycle: int, last_measured: int) -> bool: def totals(chunks: list[ChunkFigures], skipped: tuple[str, ...] = ()) -> Figures: - dollars = {dimension: summed(chunks, lambda chunk: chunk.dollars.get(dimension, collections.Counter())) for dimension in DIMENSIONS} + dollars: Dollars = {dimension: collections.defaultdict(float) for dimension in DIMENSIONS} + for chunk in chunks: + for dimension in DIMENSIONS: + for label, amount in chunk.dollars.get(dimension, {}).items(): + dollars[dimension][label] += amount routed: dict[Rung, list[ChunkFigures]] = {} reports: dict[str, Reports] = {} for chunk in chunks: @@ -279,7 +285,7 @@ def render(figures: Figures, found: list[str], at: str, cycle: int, since: str) 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()]))], + *[part for dimension in DIMENSIONS for part in (f"## Dollars by {dimension}", table((dimension.capitalize(), "Dollars"), [(label, money(dollars)) for label, dollars in sorted(figures.dollars[dimension].items(), key=lambda spent: -spent[1])]))], "## Routed chunks by the rung they started on", table(("Rung", "Chunks", "Review rounds past each pass's first, mean", "Unfinished verdicts, mean", "Subagent rungs", "Escalations by trigger"), [ (str(rung), str(len(chunks)), f"{mean(chunks, lambda chunk: chunk.rounds_past_first):.1f}", f"{mean(chunks, lambda chunk: chunk.unfinished):.1f}", subagent_rungs(chunks), triggers(summed(chunks, lambda chunk: chunk.escalated)))