Token script: count helper agents in the row, one row per model
This commit is contained in:
1 parent
7105cfc239
commit
2768234739
3 files changed
+39
-20
No files matched your search
@@ -10,7 +10,7 @@ between lil-koneko.com, CarsHub, iriks-it.nl and bouwbedrijf.iriks-it.nl, as one
|
||||
- Nothing site-specific in the package. A site's own part is its own module class.
|
||||
- Tests: `composer test` (Pest + Testbench); style: `composer lint` (Pint).
|
||||
- Branches: `production` is main, work on `feature/…` from `develop`; I never merge to `production`.
|
||||
- No mention of an AI in commits or PRs. Keep a row per piece of work in `TOKENS.md` (`dev/token-usage.py`).
|
||||
- No mention of an AI in commits or PRs. Keep a row per piece of work in `TOKENS.md` (`dev/token-usage.py`; helper agents are counted in the row, one row per model).
|
||||
|
||||
## Open decisions
|
||||
|
||||
|
||||
@@ -5,3 +5,4 @@ Per stuk werk een rij; zie `dev/token-usage.py`.
|
||||
| Datum | Onderdeel | Versie | Model | Stappen | Uitvoer | Invoer | Cache geschreven | Cache gelezen | Uren |
|
||||
|---|---|---|---|---:|---:|---:|---:|---:|---:|
|
||||
| 2026-12-04 | Eerste opzet van het pakket: modules, schakelaars, rechten. Gebruikers en media uit bouwbedrijf overgezet | - | claude-sonnet-5-5 | 34 | 57.433 | 76 | 131.194 | 3.225.205 | 0,18 |
|
||||
| 2026-12-04 | Token-script telt de tokens van helper-agents mee in de rij, per model | - | claude-sonnet-5-5 | 16 | 27.013 | 36 | 45.155 | 2.982.056 | 0,06 |
|
||||
+37
-19
@@ -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,12 +130,16 @@ 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]
|
||||
# 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) + " |")
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user