Token script: count helper agents in the row, one row per model

This commit is contained in:
Matthijs Iriks committed 2026-12-04 01:37:13 +01:00
1 parent 7105cfc239
commit 2768234739
3 files changed
+40 -21

No files matched your search

+38 -20
View File
@@ -4,14 +4,18 @@
Usage: dev/token-usage.py LOG.jsonl [--since 2026-12-02T07:00] [--until 2026-12-02T12:00]
[--row "Wat er is gemaakt" [--version v0.2.0]]
With --row it also prints the line for the table of TOKENS.md, with the hours as its last cell.
With --row it also prints the line(s) for the table of TOKENS.md, with the hours as the last cell.
Helper agents (subagents) are part of the work: their tokens are added to the row. The row is made
per model, because the price differs per model: when helper agents ran on another model than the
session, there is one row for each, and the hours are on the first row only.
The hours are the time that was worked in the period: from one entry of the log to the next,
the time between an answer and the next request included, pauses of more than half an hour
left out.
LOG.jsonl is the log of the session (~/.claude/projects/<folder>/<session>.jsonl); the logs of
helper agents in the folder `<session>/subagents` next to it are counted separately. The moments
helper agents in the folder `<session>/subagents` next to it are shown separately and also counted in the row. The moments
are UTC, as in the log. An answer of the model can be in the log more than once; it is counted
once, by its message id.
@@ -70,20 +74,28 @@ def hours(path, since, until):
def total(found, since, until):
"""Steps, output, input, written to the cache, read from the cache; and the models seen."""
"""Steps, output, input, written to the cache, read from the cache; and the same per model."""
row = [0, 0, 0, 0, 0]
models = {}
per_model = {}
for usage, moment, model in found.values():
if (since and moment[:16] < since) or (until and moment[:16] >= until):
continue
row[0] += 1
row[1] += usage.get("output_tokens", 0)
row[2] += usage.get("input_tokens", 0)
row[3] += usage.get("cache_creation_input_tokens", 0)
row[4] += usage.get("cache_read_input_tokens", 0)
if model and not model.startswith("<"):
models[model] = models.get(model, 0) + 1
return row, models
# Entries the client made itself (model "<synthetic>") hold no tokens and no work.
if model and model.startswith("<"):
continue
add = [
1,
usage.get("output_tokens", 0),
usage.get("input_tokens", 0),
usage.get("cache_creation_input_tokens", 0),
usage.get("cache_read_input_tokens", 0),
]
model = model or "?"
mine = per_model.setdefault(model, [0, 0, 0, 0, 0])
for index, value in enumerate(add):
row[index] += value
mine[index] += value
return row, per_model
def number(value):
@@ -105,9 +117,11 @@ def main():
if len(paths) != 1:
sys.exit(__doc__)
row, models = total(answers(paths[0]), since, until)
session = answers(paths[0])
row, _ = total(session, since, until)
show("session", row)
print(f"hours worked: {hours(paths[0], since, until):.2f}")
worked_hours = hours(paths[0], since, until)
print(f"hours worked: {worked_hours:.2f}")
helpers = {}
for path in glob.glob(os.path.join(os.path.splitext(paths[0])[0], "subagents", "*.jsonl")):
@@ -116,13 +130,17 @@ def main():
show("helper agents", total(helpers, since, until)[0])
if option("--row"):
if len(models) > 1:
print("More than one model in this period: make a row per model (--since/--until).", file=sys.stderr)
model = max(models, key=models.get) if models else "?"
# Session and helper agents together, one row per model.
everything = {**session, **{"helper:" + key: value for key, value in helpers.items()}}
_, per_model = total(everything, since, until)
if not per_model:
per_model = {"?": [0, 0, 0, 0, 0]}
day = (since or "")[:10] or "JJJJ-MM-DD"
worked = f"{hours(paths[0], since, until):.2f}".replace(".", ",")
cells = [day, option("--row"), option("--version") or "", model] + [number(value) for value in row] + [worked]
print("| " + " | ".join(cells) + " |")
# The model with the most steps first; the hours go on that row only.
for position, model in enumerate(sorted(per_model, key=lambda name: -per_model[name][0])):
worked = f"{worked_hours:.2f}" if position == 0 else "0.00"
cells = [day, option("--row"), option("--version") or "", model] + [number(value) for value in per_model[model]] + [worked.replace(".", ",")]
print("| " + " | ".join(cells) + " |")
if __name__ == "__main__":