Record token usage per turn and show daily, weekly and all-time totals by host

This commit is contained in:
Gmer4Lfe
2026-08-04 18:04:46 -04:00
parent 961c57c6f0
commit 969a85f303
8 changed files with 340 additions and 3 deletions
+35
View File
@@ -225,3 +225,38 @@ No cron entry and no `DAILY_MAINTENANCE_SCRIPTS` line are needed; the daily pull
An incremental run on an unchanged repo is ~70 ms, so a daily entry costs effectively nothing
and a pull that changed twelve files costs a few seconds.
---
## ━━━ TOKEN ACCOUNTING ━━━
Every completed `ask` appends one row to `AI_TOKEN_DB` (`data/ai_token_history.db`):
```
date|time|host|profile|source|prompt_tokens|completion_tokens|tok_s
2026-08-04|22:03:51|host1|varaverk|cli|2041|318|61.4
```
Both paths write it — this CLI (`source=cli`) and the WebGUI worker (`source=webgui`) — so the
totals are not quietly the tab's alone. `ai_query.sh` passes `--token-db` and `--token-host`;
called by hand without them, `cli.js` simply skips the row rather than guessing a path, because
this file never reads conf itself.
Read it on the plugin's AI tab, which aggregates today / last 7 days / all time, per host. Or
straight from the shell, since it is just a delimited file:
```bash
# tokens used today
awk -F'|' -v d="$(date +%F)" '$1==d {p+=$6; c+=$7} END {print p+c}' data/ai_token_history.db
```
**The host column is where the turn ran, not where the file is read.** Each host keeps its own
`data/` and nothing syncs it, so a host only ever sees its own rows — the partner shows as
"not collected here" on the tab, never as zero. Carrying a partner's totals would mean extending
the partnership payload fetch; the column exists so that stays a display change rather than a
migration.
Pruning is by row count (`AI_TOKEN_RETAIN_ROWS`, default 20000) and happens on write, but only
once the file passes a size threshold — an ordinary turn costs a `stat()` and an append. The CLI
deliberately does not prune: duplicating a read-modify-write of the whole file in a second
language is how the two drift apart.