Commit Graph
2 Commits
Author SHA1 Message Date
CatoandClaude Opus 5 254e451565 fix(ai): the tool loop spent tokens the ledger never saw (#776)
"One door" was wrong, and I said it confidently yesterday.

groq.ts was metered — 19 callers, rows landing, verified. But Loki's CHAT turns
do not go through groq.ts. They go through llm.ts, the tool loop, which records
vendor quota, refusals and preflight skips and never recorded SPEND. Two days
were spent working inside that file without checking it.

THE LEDGER SAID SO PLAINLY

  2026-09-17  groq  15 calls  13,806 tokens
  2026-09-16  groq  11 calls  14,586 tokens
  2026-09-15  groq   4 calls   1,926 tokens

Three days, groq only, never once google — not because Gemini was idle, but
because Gemini serves CHAT and chat was not being recorded at all. Every row on
the capacity page's "spent today" was a background feature; the biggest consumer
was missing entirely.

IT ALSO MADE YESTERDAY'S FIX DEAD CODE

The google row reads ai_usage to prove a vendor served despite publishing no
limits. For the one vendor it was written for, that table could never contain a
row. Correct, and structurally unable to fire.

THE FIX

`feature` is required on ModelCallInput, exactly as on GroqOptions, so the
compiler names any caller that has not declared itself. The tool loop charges
its spend on the success path, beside the health signal, fire-and-forget and
caught like every other write there.

A turn's three calls — round, plan retry, grounding repair — share one label,
"loki-chat". They are one question being answered; three lines on the capacity
page would read as three features.

Two checks aimed at how the hole opened: BOTH doors must call recordUsage, and
the label must be REQUIRED rather than optional. An optional field is what let
sixteen callers go unnamed the first time.

Gate green: tsc, lint, format, design, unit 194/194, home, desktop, ops.


Claude-Session: https://claude.ai/code/session_01Nm2REqtdsVsvjukBU2maCz

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 10:27:26 -07:00
CatoandClaude Opus 5 71a7b44ee6 feat(ai): show what the day's tokens were spent on (#723)
* feat(ai): show what the day's tokens were spent on

"What are the current providers, with limits, what is spent and on what" could
not be answered. The limits were there; the spend was one number with no
breakdown, and fifteen features were not in it at all.

ROUTING WAS NEVER THE PROBLEM

Worth saying plainly because I got it wrong first: every caller already goes
through ai-kit's chain. callTextDetailed walks the same chainFrom() with the
same fallback as the tool loop. There is one router, exactly as this file's own
header claims.

The gap was METERING. callOneLink did the fetch, read the body, and threw the
response headers away — so every digest, brief and form-fill drew on the same
Groq pool the capacity page was reading, and the page never saw them.

ONE LINE COVERS FIFTEEN FEATURES

recordVendorQuota() in callOneLink. They all reach the vendor through there.
Refusals record too: a 429 is the most reliable reading there is, because it
corrects a counter that had drifted optimistic.

`feature` IS NOW REQUIRED, AND THAT IS THE DESIGN

No default. The compiler then named every caller that had not named itself —
19 call sites in 15 files — and each now declares a slug the page can show a
person: activity-digest, form-assist, calendar-extract, project-brief,
frontier-propose, frontier-judge (its own slug: the panel's value is being an
uncorrelated second lineage, so its cost is worth seeing separately), dod-gate,
hosted-runner-analyze, and the rest.

A default would have let the twentieth caller be invisible again. That is how
the first fifteen got there — not by anyone hiding them, but because the
cheapest thing to write was the unnamed thing.

A SECOND TABLE, NOT A WIDER ONE

ai_spend answers "what has THIS USER spent today" and is read by the admission
gate on every turn; it must stay a point read on one row per (user, day).
Putting provider/model/feature in its key would make the rationing path pay for
reporting. ai_usage answers the orthogonal question and is keyed
(day, provider, model, feature). The user dimension is deliberately absent —
recording the same fact twice is how two tables start disagreeing.

VISION AND WHISPER REPORT TOO

They have their own transports and I nearly left them out. They spend the same
vendor pools, so omitting them would have made the page complete-looking and
wrong — the failure this surface exists to end.

EMPTY IS NOT ZERO

The ledger starts empty each UTC day, so the page says "nothing recorded yet
today", never "nothing spent". Same three-state discipline the provider rows
already keep.

5 coverage checks defend the guard itself, including that nobody can quietly
restore `options: GroqOptions = {}` and reopen the hole. Verify green, 237.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Nm2REqtdsVsvjukBU2maCz

* chore: re-trigger CI (previous run stalled 5s after start)

The prior CI run for this branch froze with updatedAt five seconds after
creation and sat in_progress for 45 minutes, while every other branch's CI
completed in 3-5 minutes in the same window. A stuck runner, not a slow test —
two candidate causes in the change itself were tested and both disproved:
a unit test writing to a reachable database still exits, and importing the db
module against a live connection does not hold the event loop.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Nm2REqtdsVsvjukBU2maCz

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 03:08:59 -07:00