// A running token and estimated-USD tally across every call of a run (release // 27), on either provider: a local model's calls are counted and cost nothing. import { CACHE_WRITE_MULT, priceOf, type CacheTtl } from "./prices"; // The Messages API's `usage`; ollama's counts map onto input/output. export type Usage = { input_tokens?: number; output_tokens?: number; cache_creation_input_tokens?: number | null; cache_read_input_tokens?: number | null; // The write split by TTL, when the API reports it. cache_creation?: { ephemeral_5m_input_tokens?: number; ephemeral_1h_input_tokens?: number } | null; [k: string]: unknown; }; export type ModelTally = { calls: number; input: number; cacheWrite: number; cacheRead: number; output: number; usd: number; // cache reads over all input (input + writes + reads), 0..1 cacheHit: number; }; const n = (v: unknown) => (typeof v === "number" && Number.isFinite(v) ? v : 0); const round = (v: number, places = 6) => Math.round(v * 10 ** places) / 10 ** places; export class Meter { // The cap `over()` measures against; mutable, so a caller may extend a run // (a bounded repair round) without a second meter. maxUsd: number | undefined; private total = 0; private readonly byModel: Record = {}; constructor(o: { maxUsd?: number } = {}) { this.maxUsd = o.maxUsd; } get usd(): number { return this.total; } // Adds one call's usage and returns its USD. Cache writes the API does not // split by TTL are priced at `cacheTtl` (the run's one TTL; default 5m). add(model: string, usage: Usage, o: { cacheTtl?: CacheTtl } = {}): number { const p = priceOf(model); const input = n(usage.input_tokens); const output = n(usage.output_tokens); const write = n(usage.cache_creation_input_tokens); const read = n(usage.cache_read_input_tokens); const split = usage.cache_creation ?? null; let write1h: number; if (split && (split.ephemeral_1h_input_tokens !== undefined || split.ephemeral_5m_input_tokens !== undefined)) write1h = Math.min(write, n(split.ephemeral_1h_input_tokens)); else write1h = o.cacheTtl === "1h" ? write : 0; const write5m = write - write1h; const usd = (input * p.input + write5m * p.input * CACHE_WRITE_MULT["5m"] + write1h * p.input * CACHE_WRITE_MULT["1h"] + read * p.input * p.cacheReadMult + output * p.output) / 1e6; this.total += usd; const m = (this.byModel[model] ??= { calls: 0, input: 0, cacheWrite: 0, cacheRead: 0, output: 0, usd: 0, cacheHit: 0, }); m.calls += 1; m.input += input; m.cacheWrite += write; m.cacheRead += read; m.output += output; m.usd = round(m.usd + usd); const all = m.input + m.cacheWrite + m.cacheRead; m.cacheHit = all ? round(m.cacheRead / all, 3) : 0; return usd; } // True once the spend reaches `frac` of the cap; never without a cap. over(frac = 1): boolean { return this.maxUsd !== undefined && this.total >= this.maxUsd * frac; } summary(): { usd: number; byModel: Record } { const byModel: Record = {}; for (const [k, v] of Object.entries(this.byModel)) byModel[k] = { ...v }; return { usd: round(this.total), byModel }; } }