// --------------------------------------------------------------------------- // What a new sweep (or research run) is likely to spend, shown BEFORE it runs // (plans/umtool-redesign.md, P5: "Article from a question … with the estimate // shown before it runs"). // // There is no price model of a sweep -- how much it reads depends on what the // question matches -- so the estimate is what such runs HAVE spent: the // editor's usage ledger (common/controller/generationUsage.ts, one row per // model per finished API job), summed per job, for the same task and model. // The cap is the ceiling the editor enforces; a local model spends nothing on // the API. Said as it is: "2 earlier runs on : typical $0.41, most // $0.63 · stops at $5.00 (its cap)". // // Plain ESM: the server reads the ledger, the client form says the estimate. // --------------------------------------------------------------------------- /** How many of a model's latest jobs an estimate reads. */ export const HISTORY_RUNS = 20; /** * The ledger as per-job spend: task -> model -> [usd per job], oldest first, * at most HISTORY_RUNS each. A job's rows (one per model it called) are summed * under the model of its largest row, which is the job's own model: an API * sweep's extractor runs on a cheaper model beside it. * @param {{ jobId: string, task: string, model: string, usd: number, at?: string }[]} rows * @param {readonly string[]} [tasks] * @returns {Record>} */ export function spendHistory(rows, tasks = ["sweep", "research"]) { /** @type {Map }>} */ const jobs = new Map(); for (const r of rows) { // A local row (route "local", demo epic D3) records calls, not spend. if (!r || !tasks.includes(r.task) || typeof r.usd !== "number" || !r.jobId || r.route === "local") continue; const j = jobs.get(r.jobId) ?? { task: r.task, at: r.at ?? "", usd: 0, models: new Map() }; j.usd += r.usd; j.models.set(r.model, (j.models.get(r.model) ?? 0) + r.usd); if ((r.at ?? "") > j.at) j.at = r.at ?? j.at; jobs.set(r.jobId, j); } /** @type {Record>} */ const out = {}; for (const j of [...jobs.values()].sort((a, b) => a.at.localeCompare(b.at))) { const model = [...j.models.entries()].sort((a, b) => b[1] - a[1])[0]?.[0]; if (!model) continue; const list = ((out[j.task] ??= {})[model] ??= []); list.push(Math.round(j.usd * 10_000) / 10_000); if (list.length > HISTORY_RUNS) list.shift(); } return out; } const median = (xs) => { const s = [...xs].sort((a, b) => a - b); const m = s.length >> 1; return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2; }; /** * The estimate for one run. * @param {Record>} history * @param {{ task: string, model: string, cap: number | null, local: boolean }} run * @returns {{ kind: "local" } | { kind: "api", model: string, runs: number, typical: number | null, most: number | null, cap: number | null }} */ export function estimateOf(history, { task, model, cap, local }) { if (local) return { kind: "local" }; const past = history?.[task]?.[model] ?? []; return { kind: "api", model, runs: past.length, typical: past.length ? Math.round(median(past) * 100) / 100 : null, most: past.length ? Math.max(...past) : null, cap: typeof cap === "number" && cap > 0 ? cap : null, }; } const usd = (v) => `$${v.toFixed(2)}`; /** The estimate in words. */ export function estimateText(e) { if (e.kind === "local") return "Runs on the local model: no API spend"; const ceiling = e.cap !== null ? ` · stops at ${usd(e.cap)} (its cap)` : ""; if (!e.runs) return `No earlier run on ${e.model} in the ledger${ceiling}`; return `${e.runs} earlier ${e.runs === 1 ? "run" : "runs"} on ${e.model}: typical ${usd(e.typical)}, most ${usd(e.most)}${ceiling}`; }