codeburn/src/model-breakdown.ts
Ulises 724b9f3621
feat(models): carry the billing route on the call and key model rows on it (#1448)
The same model billed through a different door is its own row on every surface.

The route rides on the call as a field: the model id names it when the door renames the model (Bedrock's <vendor>.<model>[-vN:M], with the cross-region profile kept as its own SKU), and the provider's own column names it when it does not (Hermes billing_provider). Only bedrock is registered; openrouter and bedrock-mantle wait for real sessions.

Every report keys a model row on one function, modelRowKey: short name plus the door's label. models-report folds on that row key rather than the route id, so two doors sharing one label can never print two rows a reader cannot tell apart. codeburn models --format json carries route per row.

Daily cache moves to v33: day.models is keyed by the row key so a column-sourced route survives into a finalized day, and hermes joins the pending-rederive table. Hermes sessions re-parse once.

Pricing never consults the route and runs on the raw id. granular-history still keys on the raw model id, so the desktop Trend timeline is the one surface outside this.

Verified on a real cache: 142 finalized days, $37,898.851498 and 272,985 calls unchanged.
2026-09-17 13:32:34 +02:00

43 lines
1.8 KiB
TypeScript

import { modelRowKey } from './models.js'
import type { ProjectSummary } from './types.js'
export interface ModelTotals {
calls: number
costUSD: number
estimatedCostUSD: number
freshInput: number
cacheRead: number
cacheWrite: number
activeDurationMs: number
activeGeneratedTokens: number
}
/// Aggregate per-model usage across every session, keyed by the friendly display
/// name rather than the raw model id. A cache can hold more than one raw key that
/// resolves to the same name — e.g. the full Fireworks path
/// `accounts/fireworks/models/glm-5p2` written by an older build and the
/// normalized `glm-5p2`/`GLM-5.2` written by a newer one — and keying by the
/// resolved name merges those into a single row instead of rendering duplicates.
export function aggregateModelTotals(projects: ProjectSummary[]): Record<string, ModelTotals> {
const modelTotals: Record<string, ModelTotals> = {}
for (const project of projects) {
for (const session of project.sessions) {
for (const [model, data] of Object.entries(session.modelBreakdown)) {
const name = modelRowKey(model)
const totals = (modelTotals[name] ??= {
calls: 0, costUSD: 0, estimatedCostUSD: 0, freshInput: 0, cacheRead: 0, cacheWrite: 0,
activeDurationMs: 0, activeGeneratedTokens: 0,
})
totals.calls += data.calls
totals.costUSD += data.costUSD
totals.estimatedCostUSD += data.estimatedCostUSD ?? 0
totals.freshInput += data.tokens.inputTokens
totals.cacheRead += data.tokens.cacheReadInputTokens
totals.cacheWrite += data.tokens.cacheCreationInputTokens
totals.activeDurationMs += data.activeDurationMs ?? 0
totals.activeGeneratedTokens += data.activeGeneratedTokens ?? 0
}
}
}
return modelTotals
}