Parse each transcript entry once into a typed value naming its kind, and have every consumer read those types
This commit is contained in:
@@ -3,7 +3,7 @@ import collections
|
||||
import dataclasses
|
||||
import typing
|
||||
|
||||
from claude_code.entries import assistant_parts, tool_call
|
||||
from claude_code.entries import AssistantTurn, CostState, Entry, ToolCall
|
||||
from routing_figures.prices import listed
|
||||
|
||||
DIMENSIONS = ("project", "subagent type", "rung", "tool")
|
||||
@@ -31,31 +31,25 @@ class Spent(typing.NamedTuple):
|
||||
message: Message
|
||||
|
||||
|
||||
def messages(entries: list[dict]) -> list[Message]:
|
||||
def messages(entries: list[Entry]) -> list[Message]:
|
||||
"""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]]] = {}
|
||||
kept: dict[str, tuple[AssistantTurn, list[str]]] = {}
|
||||
for entry in entries:
|
||||
if entry.get("type") != "assistant" or not isinstance(message := entry.get("message"), dict):
|
||||
continue
|
||||
found = kept.setdefault(str(message.get("id") or entry.get("uuid")), (str(entry.get("timestamp", "")), message, []))
|
||||
found[2].extend(name for part in assistant_parts(entry) if (call := tool_call(part)) and isinstance(name := call[0], str))
|
||||
if isinstance(entry, AssistantTurn):
|
||||
found = kept.setdefault(entry.message_id or entry.uuid, (entry, []))
|
||||
found[1].extend(part.name for part in entry.parts if isinstance(part, ToolCall))
|
||||
counted = []
|
||||
before = [PROMPT]
|
||||
for at, message, called in kept.values():
|
||||
model = str(message.get("model") or "")
|
||||
counted.append(Message(at, before, model, listed(model, message.get("usage"))))
|
||||
for turn, called in kept.values():
|
||||
counted.append(Message(turn.timestamp, before, turn.model, listed(turn.model, turn.usage)))
|
||||
before = called or [PROMPT]
|
||||
return counted
|
||||
|
||||
|
||||
# {"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]:
|
||||
"""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))}
|
||||
return {}
|
||||
def recorded_costs(worker: list[Entry]) -> dict[str, float]:
|
||||
"""The cost the worker transcript's last cost-state entry records for each model."""
|
||||
return next((entry.costs for entry in reversed(worker) if isinstance(entry, CostState)), {})
|
||||
|
||||
|
||||
def costed(spent: list[Spent], judged: list[tuple[str, float]], project: str, recorded: dict[str, float]) -> tuple[Dollars, float]:
|
||||
|
||||
Reference in New Issue
Block a user