Skip to content

feat: record token usage per board run and show it in the web UI - #23

Merged
springfall2008 merged 11 commits into
mainfrom
feat/token-usage
Nov 27, 2026
Merged

springfall2008 merged 11 commits into
mainfrom
feat/token-usage

Conversation

@springfall2008

Copy link
Copy Markdown
Owner

What

The board runner records the raw token counts (input, output, cache read, cache write, per model; never dollars) of every session it starts, stores them in shared memory, and the web UI shows them.

  • Workers: on exit, from the transcript's result line, or summed from its messages (each message id once) when it was killed or cut off. Workers orphaned by a crashed runner, or killed by a double Ctrl-C, are counted too.
  • Lead: one row per lead label, replaced after each turn with the session's totals, including sub-agents.
  • Start and release one-shots: a row each.
  • Storage: a new board_usage table (20261127120000_board_usage.sql). Writes go only through record_board_usage (an upsert on scope, board and run label; can_write required). The intent id is kept only when it names an intent of that board. Reads are under a can_read policy. delete_board_usage deletes a board's rows. Agents never see the rows.
  • web-api: POST /usage, GET /usage?scope=&board= (runs, newest first, plus the summed total), POST /usage/delete. Scope errors answer 404 or 403 and database errors 503, so the runner retries them.
  • Runner sending: rows go to usage-pending.jsonl first and are retried with backoff until accepted, at most 10 per loop tick. Only 400, 413 and 422 drop a row. Credentials come from AGENT_MEMORY_TOKEN and WEB_API_URL in the environment, or from the board file's usage_env_file.
  • Cleanup: board-runner forget-usage <board file> deletes a board's rows (toy-board cleanup).
  • Web UI: "Tokens 12M in · 310k out" in the status strip, and a Tokens block in an intent's, fact's or goal's drawer. The proxy allows GET /api/usage only.
  • Backup and restore include board_usage.

Design: docs/superpowers/specs/2026-11-27-token-usage-design.md; plan: docs/superpowers/plans/2026-11-27-token-usage.md.

Tests

  • deno task check, 318 unit tests, integration (usage-store, web-api, backup), pgTAP 18_board_usage, and the runner unittest suite all pass.
  • Local end to end: the runner's sender to a local web-api, then GET through the local web server proxy, then forget-usage.
  • The browser strip and drawer block were not viewed in a browser; please check them after deploy.

Found and fixed along the way

  • The edge runtime's parser refused a regex literal containing a quote in web-api.ts, although deno check passed (BOOT_ERROR). It is replaced, and noted in CLAUDE.md.
  • Scope and database errors had answered 400, which the runner drops for good.

Deferred minors

  • An intent id over 100 characters, or a board name over about 86 characters, gets the row refused and dropped.
  • A timed-out start one-shot is not counted.
  • A row stuck on 403/404 blocks the board's later rows.
  • The lead transcript is re-read in full each turn.
  • The lead row's startedAt resets on each runner restart.
  • The drawer's Tokens block does not refresh while it is open.

After merge (each needs consent)

  1. supabase db push --remote --dry-run, a backup, then supabase db push --remote.
  2. supabase functions deploy web-api --use-api.
  3. Restart runners from the updated checkout with AGENT_MEMORY_TOKEN and WEB_API_URL (or usage_env_file).

🤖 Generated with Claude Code

springfall2008 and others added 11 commits November 27, 2026 08:31
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The runner drops a row on 400, so a token not yet granted on a scope would
have lost its rows. Plain string tests replace the regex literals, which the
edge runtime's parser refused to boot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… a backlog per tick

- POST /usage answers 503 for a database error, so the runner retries it
- workers killed by a double Ctrl-C are counted from their transcripts
- the sender sends at most 10 rows per loop tick

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@springfall2008
springfall2008 merged commit b52d9cb into main Nov 27, 2026
2 checks passed
@springfall2008
springfall2008 deleted the feat/token-usage branch November 27, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant