Model usage was recorded per call and never read, so an org could not see what it spent. Add the read side: - model_usage carries identity_id. Task calls take it from the task row, chat and curator calls from the calling principal carried in the usage record, so an outbox replay keeps its attribution. Existing task rows are backfilled from tasks. - StatsService.UsageTotals sums the caller org's rows by UTC day, source, provider, model and identity. The org comes from the principal, with no org parameter, so cross-org reads do not exist. - GET /api/usage prices tokens at the catalog's list rate, netting cache hits, and reports calls to unpriced models as tokens only. The published catalog view now carries each model's cost. - A new org Usage page behind a read_usage grant: admin and owner only, with a 7, 30 or 90 day range, totals, the daily series, and model, source and identity breakdowns. The org shell keeps it on the caller's own org.
17 lines
619 B
SQL
17 lines
619 B
SQL
-- Attribute every model call to the identity the work was requested as, so
|
|
-- usage reports can break down by the people and agents that asked for it.
|
|
-- A task's calls belong to the identity that created the task; chat and
|
|
-- curator calls carry the calling principal in the record itself, so a queued
|
|
-- retry keeps its attribution.
|
|
|
|
-- +goose Up
|
|
|
|
ALTER TABLE model_usage ADD COLUMN identity_id text NOT NULL DEFAULT '';
|
|
UPDATE model_usage u SET identity_id = t.identity
|
|
FROM tasks t
|
|
WHERE u.task_id = t.id AND u.task_id <> 0 AND u.identity_id = '';
|
|
|
|
-- +goose Down
|
|
|
|
ALTER TABLE model_usage DROP COLUMN identity_id;
|