package archiegateway import ( "context" "log/slog" "time" "github.com/samcharles93/ai-sdk/chat" "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) } } record := usage.Record{ Source: source, Alias: alias, 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) } }