// What each owner's model calls used: tokens and cost, by day, agent and model, and their latest calls. // // Every call through `meteredModels` ends here, on the service's keys or the owner's own. A call is // added to a day's total for its agent and model, so the record stays small however many calls there // are: about a row per agent, model and day. The latest calls are kept as they were, for the Usage // page's list. Days older than USAGE_DAYS are dropped. import { AsyncLocalStorage } from 'node:async_hooks' import type { State } from './state.ts' /** What a model call is for, beyond its agent: set around a Machine run, so its calls are counted to the Machine. */ export const usageTag = new AsyncLocalStorage<{ machine?: string }>() export const USAGE_DAYS = 90 export const RECENT_CALLS = 50 /** Totals for one agent and model on one day. `cost` is what the provider charged; `charged` is what the owner paid Languid (0 on their own key). */ export type UsageTotals = { calls: number; input: number; output: number; cached: number; cost: number; charged: number } export type UsageCall = { at: string; agent: string; model: string; input: number; output: number; cached: number; cost: number; charged: number; ownKey?: boolean; machine?: string } export type OwnerUsage = { /** Day (UTC, YYYY-MM-DD) → agent → `provider/model` → totals. */ days: Record>> /** Day → agent → Machine record key → totals: the calls Machine runs made (also counted in `days`). */ machines?: Record>> /** The latest calls, newest first. */ recent: UsageCall[] } const zero = (): UsageTotals => ({ calls: 0, input: 0, output: 0, cached: 0, cost: 0, charged: 0 }) const add = (t: UsageTotals, c: Pick, calls = 1) => { t.calls += calls t.input += c.input t.output += c.output t.cached += c.cached t.cost += c.cost t.charged += c.charged } /** Add one finished call to `did`'s usage in `state`. */ export function recordUsage(state: State, did: string, call: UsageCall) { const all = (state.usage ??= {}) const u = (all[did] ??= { days: {}, recent: [] }) const day = call.at.slice(0, 10) const t = (((u.days[day] ??= {})[call.agent] ??= {})[call.model] ??= zero()) add(t, call) if (call.machine) add((((u.machines ??= {})[day] ??= {})[call.agent] ??= {})[call.machine] ??= zero(), call) u.recent = [call, ...u.recent].slice(0, RECENT_CALLS) const cutoff = new Date(Date.parse(call.at) - USAGE_DAYS * 86_400_000).toISOString().slice(0, 10) for (const d of Object.keys(u.days)) if (d < cutoff) delete u.days[d] for (const d of Object.keys(u.machines ?? {})) if (d < cutoff) delete u.machines![d] } /** What an agent's Machine runs used over the last `days` days, by Machine record key. */ export function machineUsage(state: State, did: string, agent: string, days: number, now = new Date()): Map { const out = new Map() const m = state.usage?.[did]?.machines ?? {} for (const day of lastDays(days, now)) { for (const [rkey, t] of Object.entries(m[day]?.[agent] ?? {})) { const sum = out.get(rkey) ?? zero() add(sum, t, t.calls) out.set(rkey, sum) } } return out } /** The UTC days from `days - 1` days ago to today, oldest first. */ export function lastDays(days: number, now = new Date()): string[] { const today = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()) return Array.from({ length: days }, (_, i) => new Date(today - (days - 1 - i) * 86_400_000).toISOString().slice(0, 10)) } export type UsageSummary = { days: { day: string; totals: UsageTotals }[] total: UsageTotals byAgent: { name: string; totals: UsageTotals }[] byModel: { name: string; totals: UsageTotals }[] recent: UsageCall[] } /** `did`'s usage over the last `days` days: per day, in all, and by agent and by model, biggest first. */ export function summarizeUsage(state: State, did: string, days: number, now = new Date()): UsageSummary { const u = state.usage?.[did] const range = lastDays(days, now) const total = zero() const agents = new Map() const models = new Map() const perDay = range.map((day) => { const totals = zero() for (const [agent, byModel] of Object.entries(u?.days[day] ?? {})) { for (const [model, t] of Object.entries(byModel)) { add(totals, t, t.calls) add(total, t, t.calls) const a = agents.get(agent) ?? zero() add(a, t, t.calls) agents.set(agent, a) const m = models.get(model) ?? zero() add(m, t, t.calls) models.set(model, m) } } return { day, totals } }) // Biggest first: what they paid, then what it cost, then how many calls. const rank = (m: Map) => [...m].map(([name, totals]) => ({ name, totals })).sort((a, b) => b.totals.charged - a.totals.charged || b.totals.cost - a.totals.cost || b.totals.calls - a.totals.calls) return { days: perDay, total, byAgent: rank(agents), byModel: rank(models), recent: (u?.recent ?? []).filter((c) => c.at.slice(0, 10) >= range[0]) } }