Classifiers and Cost: unknown runs, phone layout, distinct items, unpriced models

Classifiers says when ?run= names no run, scrolls a long By repo list and
stacks both answers under the title on phones. Cost counts distinct classified
items, lists items and price per model, says "price not reported" for models
without USD, and leaves out the spend chart below three buckets.
This commit is contained in:
aiongg committed 2026-11-02 19:01:52 +00:00
1 parent 49d719f3fa
commit 79fe373bcd
6 files changed
+75 -15

No files matched your search

+1
View File
@@ -148,6 +148,7 @@ export function cost(db: DatabaseSync, req: Req): CostReport {
runs: db.prepare(`SELECT run_id AS id, MIN(started_at) AS "from", MAX(COALESCE(ended_at, started_at)) AS "to" FROM token_usage GROUP BY run_id ORDER BY MAX(COALESCE(ended_at, started_at)) DESC`).all() as CostReport["runs"],
run,
total: { ...(total as object) } as UsageTotals,
itemsClassified: (db.prepare(`SELECT COUNT(*) AS n FROM (SELECT DISTINCT repo, number FROM classifications WHERE ${flt})`).get(p) as { n: number }).n,
stages: q<CostReport["stages"][number]>(`SELECT COALESCE(stage, '') AS stage, COALESCE(model, '') AS model, ${TOTALS} FROM token_usage WHERE ${flt} GROUP BY stage, model ORDER BY stage, model`),
repos: q<CostReport["repos"][number]>(`SELECT repo, ${TOTALS} FROM token_usage WHERE ${flt} AND repo IS NOT NULL GROUP BY repo ORDER BY COALESCE(SUM(COALESCE(input_tokens, 0) + COALESCE(output_tokens, 0) + COALESCE(cache_read_tokens, 0) + COALESCE(cache_write_tokens, 0)), 0) DESC`),
series: q<CostReport["series"][number]>(`SELECT substr(COALESCE(ended_at, started_at), 1, ${len}) AS bucket,
+1
View File
@@ -65,6 +65,7 @@ describe("insight", () => {
test("cost totals, wall time and series", () => {
const c = cost(db, rq("run=r1"));
assert.deepEqual(c.total, { calls: 2, items: 6, input: 150, output: 30, cacheRead: 10, cacheWrite: 0, usd: 0.25, wallSeconds: 180 });
assert.equal(c.itemsClassified, 6);
assert.equal(c.stages.length, 1);
assert.deepEqual(c.series, [{ bucket: "2026-11-01T10", tokens: 190, usd: 0.25 }]);
});
+1 -1
View File
@@ -122,5 +122,5 @@ export function costReport(run = ""): CostReport {
tokens: Math.round((900_000 + r() * 6_000_000) * k),
usd: Math.round((0.4 + r() * 4.5) * k * 100) / 100,
}));
return { runs: [{ id: RUN, from: "2026-10-27T08:00:00Z", to: "2026-11-02T10:30:00Z" }, { id: "pilot-1029", from: "2026-10-29T14:00:00Z", to: "2026-10-29T15:10:00Z" }], run, total, stages, repos, series, bucketSize: "hour" };
return { runs: [{ id: RUN, from: "2026-10-27T08:00:00Z", to: "2026-11-02T10:30:00Z" }, { id: "pilot-1029", from: "2026-10-29T14:00:00Z", to: "2026-10-29T15:10:00Z" }], run, total, itemsClassified: Math.round(21000 * k), stages, repos, series, bucketSize: "hour" };
}
+2
View File
@@ -489,6 +489,8 @@ export interface CostReport {
/** A run id, or "" for all runs. */
run: string;
total: UsageTotals;
/** Distinct items with a classification in the run (or all runs); `total.items` counts an item once per model. */
itemsClassified: number;
stages: ({ stage: string; model: string } & UsageTotals)[];
repos: ({ repo: string } & UsageTotals)[];
/** Spend per time bucket; `bucket` is an ISO hour or day. */
+27 -7
View File
@@ -93,8 +93,8 @@ function Disagreements({ r, label, band, setFilter }: { r: ClassifierReport; lab
<tr>
<Th>Item</Th>
<Th>Title</Th>
<Th>{r.a}</Th>
<Th>{r.b}</Th>
<Th className="hidden sm:table-cell">{r.a}</Th>
<Th className="hidden sm:table-cell">{r.b}</Th>
</tr>
</thead>
<tbody>
@@ -103,11 +103,24 @@ function Disagreements({ r, label, band, setFilter }: { r: ClassifierReport; lab
<td className="whitespace-nowrap px-3 py-1.5">
<RefLink repo={d.repo} number={d.number} kind={d.kind} />
</td>
<td className="min-w-[16rem] px-3 py-1.5 text-ink/85">{d.title}</td>
<td className="whitespace-nowrap px-3 py-1.5">
<td className="px-3 py-1.5 text-ink/85 sm:min-w-[16rem]">
{d.title}
{/* Phones: the two answers under the title instead of in their own columns. */}
<dl className="mt-1 grid grid-cols-[auto_1fr] gap-x-2 text-xs sm:hidden">
<dt className="text-ink/50">{r.a}</dt>
<dd>
{d.a.label} <span className="num text-ink/50">{prob(d.a.prob)}</span>
</dd>
<dt className="text-ink/50">{r.b}</dt>
<dd>
{d.b.label} <span className="num text-ink/50">{prob(d.b.prob)}</span>
</dd>
</dl>
</td>
<td className="hidden whitespace-nowrap px-3 py-1.5 sm:table-cell">
{d.a.label} <span className="num text-xs text-ink/50">{prob(d.a.prob)}</span>
</td>
<td className="whitespace-nowrap px-3 py-1.5">
<td className="hidden whitespace-nowrap px-3 py-1.5 sm:table-cell">
{d.b.label} <span className="num text-xs text-ink/50">{prob(d.b.prob)}</span>
</td>
</tr>
@@ -168,6 +181,11 @@ function Classifiers(_: ViewProps) {
options={cur.pairs.map((p) => ({ value: `${p.a}|${p.b}`, label: `${p.a} vs ${p.b} (${num(p.items)})` }))}
/>
</div>
{run && run !== r.run && (
<p role="status" className="mt-2 rounded-md border border-warn/40 bg-warn/10 px-3 py-2 text-sm">
There is no run <span className="code">{run}</span>. Showing the latest run, <span className="code">{r.run}</span>.
</p>
)}
<p className="mt-1 text-sm text-ink/55">Agreement on the type label, per run. Rows are grouped by {r.a}&apos;s answer.</p>
<div className="mt-4 grid grid-cols-2 gap-3 lg:grid-cols-4">
@@ -181,8 +199,10 @@ function Classifiers(_: ViewProps) {
<Block title={`By ${r.a}'s label`} aside="Click to filter disagreements" flush>
<AgreementRows name="Label" rows={r.byLabel.map((x) => ({ key: x.label, n: x.n, agree: x.agree }))} picked={label} onPick={(k) => set({ label: label === k ? "" : k })} />
</Block>
<Block title="By repo" flush>
<AgreementRows name="Repo" rows={r.byRepo.map((x) => ({ key: x.repo, n: x.n, agree: x.agree }))} />
<Block title="By repo" aside={`${num(r.byRepo.length)} repos with items judged by both`} flush>
<div className="max-h-[22rem] overflow-y-auto">
<AgreementRows name="Repo" rows={r.byRepo.map((x) => ({ key: x.repo, n: x.n, agree: x.agree }))} />
</div>
</Block>
</div>
+43 -7
View File
@@ -4,6 +4,7 @@ import { useQuery } from "../../api/client";
import type { CostReport, UsageTotals } from "../../api/types";
import type { FeatureModule, ViewProps } from "../../registry/types";
import { num } from "../../lib/format";
import { cn } from "../../ui/cn";
import { Block, Select, Stat, Th } from "../insight/parts";
const tokens = (u: UsageTotals) => u.input + u.output + u.cacheRead + u.cacheWrite;
@@ -44,7 +45,7 @@ function UsageTable({ rows, name, subName }: { rows: Row[]; name: string; subNam
<td className="px-3 py-1.5 text-right">{num(u.output)}</td>
<td className="px-3 py-1.5 text-right">{num(u.cacheRead)}</td>
<td className="px-3 py-1.5 text-right">{num(u.cacheWrite)}</td>
<td className="px-3 py-1.5 text-right">{usd(u.usd)}</td>
<td className={cn("px-3 py-1.5 text-right", u.usd === null && "whitespace-nowrap text-xs text-ink/45")}>{u.usd === null ? "price not reported" : usd(u.usd)}</td>
<td className="px-3 py-1.5 text-right">{per(tokens(u), u.items)}</td>
<td className="px-3 py-1.5 text-right">{perUsd(u.usd, u.items)}</td>
<td className="px-3 py-1.5 text-right">{wall(u.wallSeconds)}</td>
@@ -86,12 +87,45 @@ function SpendChart({ r }: { r: CostReport }) {
);
}
type ModelTotals = { model: string; items: number; usd: number | null };
// Totals per model across stages. USD stays null for a model that never reported a price.
function byModel(r: CostReport): ModelTotals[] {
const m = new Map<string, ModelTotals>();
for (const s of r.stages) {
const cur = m.get(s.model) ?? { model: s.model || "unknown model", items: 0, usd: null };
cur.items += s.items;
if (s.usd !== null) cur.usd = (cur.usd ?? 0) + s.usd;
m.set(s.model, cur);
}
return [...m.values()].sort((a, b) => b.items - a.items);
}
// Spend per item counts only the items of models that report a price.
function SpendSub({ models }: { models: ModelTotals[] }) {
const priced = models.filter((m) => m.usd !== null);
const unpriced = models.filter((m) => m.usd === null);
return (
<>
{priced.map((m) => (
<div key={m.model}>
{m.model}: {perUsd(m.usd, m.items)} per item
</div>
))}
{unpriced.length > 0 && <div>{unpriced.map((m) => m.model).join(", ")}: price not reported</div>}
</>
);
}
function Cost(_: ViewProps) {
const [sp, setSp] = useSearchParams();
const run = sp.get("run") ?? "";
const { data: r } = useQuery(`cost:${run}`, (api) => api.cost(run));
if (!r) return <div className="p-6 text-ink/50">Loading…</div>;
const t = r.total;
const models = byModel(r);
// Each model's calls count the same item again, so the headline uses distinct classified items.
const items = r.itemsClassified || Math.max(0, ...models.map((m) => m.items));
return (
<div className="h-full overflow-y-auto">
<div className="mx-auto max-w-[1180px] px-4 pb-12 pt-5 md:px-6">
@@ -107,15 +141,17 @@ function Cost(_: ViewProps) {
<p className="mt-1 text-sm text-ink/55">Token use per agent call. USD appears where the agent reported a cost.</p>
<div className="mt-4 grid grid-cols-2 gap-3 lg:grid-cols-4">
<Stat label="Calls" value={num(t.calls)} sub={`${num(t.items)} items handled`} />
<Stat label="Tokens" value={num(tokens(t))} sub={`${per(tokens(t), t.items)} per item`} />
<Stat label="Spend" value={usd(t.usd)} sub={t.usd === null ? "none reported" : `${perUsd(t.usd, t.items)} per item`} />
<Stat label="Items" value={num(items)} sub={models.map((m) => `${m.model} ${num(m.items)}`).join(" · ")} />
<Stat label="Tokens" value={num(tokens(t))} sub={`${num(t.calls)} calls · ${per(tokens(t), t.items)} per item and model`} />
<Stat label="Spend" value={usd(t.usd)} sub={t.usd === null ? "price not reported" : <SpendSub models={models} />} />
<Stat label="Wall time" value={wall(t.wallSeconds)} sub="first start to last end" />
</div>
<Block title="Spend over time" className="mt-4">
<SpendChart r={r} />
</Block>
{r.series.length >= 3 && (
<Block title="Spend over time" className="mt-4">
<SpendChart r={r} />
</Block>
)}
<Block title="By stage and model" className="mt-4" flush>
<UsageTable name="Stage" subName="Model" rows={r.stages.map((s) => ({ key: s.stage, sub: s.model, u: s }))} />
</Block>