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.
55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package archiegateway
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/samcharles93/ai-sdk/chat"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/usage"
|
|
"github.com/samcharles93/archie-core/internal/gateway"
|
|
)
|
|
|
|
const usageWriteTimeout = 5 * time.Second
|
|
|
|
// usageSink records the gateway's model calls. The State Store bills each to
|
|
// the org of the principal the call was made for. A nil sink records nothing.
|
|
type usageSink struct {
|
|
store usage.Recorder
|
|
models gateway.ModelManager
|
|
log *slog.Logger
|
|
}
|
|
|
|
// record appends one call's usage. It never fails the call: the write is
|
|
// detached from the caller's cancellation and a failure is only logged.
|
|
func (u *usageSink) record(ctx context.Context, source usage.Source, ref string, used chat.Usage) {
|
|
if u == nil || u.store == nil || ref == "" {
|
|
return
|
|
}
|
|
alias := ""
|
|
if u.models != nil {
|
|
if model, err := u.models.ModelFor(ctx); err == nil && model == ref {
|
|
alias = u.models.ActiveAlias(ctx)
|
|
}
|
|
}
|
|
// The dashboard forwards the caller's principal, so a chat call is
|
|
// attributed to whoever asked for it. A channel front-end with no
|
|
// principal leaves it unknown rather than guessing.
|
|
identity := ""
|
|
if p, ok := access.PrincipalFromContext(ctx); ok {
|
|
identity = string(p.IdentityID)
|
|
}
|
|
record := usage.Record{
|
|
Source: source, Alias: alias, Identity: identity,
|
|
InputTokens: int64(used.PromptTokens), OutputTokens: int64(used.CompletionTokens),
|
|
CachedTokens: int64(used.CachedTokens), At: time.Now().UTC(),
|
|
}.WithRef(ref)
|
|
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), usageWriteTimeout)
|
|
defer cancel()
|
|
if err := u.store.RecordUsage(ctx, record); err != nil && u.log != nil {
|
|
u.log.WarnContext(ctx, "record model usage failed", "source", source, "model", ref, "err", err)
|
|
}
|
|
}
|