mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 09:20:03 +01:00
* 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>
19 lines
953 B
SQL
19 lines
953 B
SQL
-- What the day's AI tokens were spent ON: provider, model, and the feature that
|
|
-- asked. `ai_spend` stays the per-user rationing ledger and is untouched; this
|
|
-- answers the orthogonal question the capacity page needs.
|
|
CREATE TABLE IF NOT EXISTS "ai_usage" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
"day" date NOT NULL,
|
|
"provider" text NOT NULL,
|
|
"model" text NOT NULL,
|
|
"feature" text NOT NULL,
|
|
"tokens" integer DEFAULT 0 NOT NULL,
|
|
"calls" integer DEFAULT 0 NOT NULL,
|
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
|
);
|
|
--> statement-breakpoint
|
|
-- The upsert target. Without it two concurrent calls both read zero, both
|
|
-- insert, and the day reports twice the rows at half the truth each.
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "uq_ai_usage_bucket" ON "ai_usage" USING btree ("day","provider","model","feature");--> statement-breakpoint
|
|
CREATE INDEX IF NOT EXISTS "idx_ai_usage_day" ON "ai_usage" USING btree ("day");
|