fix(pricing): no price across a model version boundary, long-context tier in why, Droid versions kept, Qwen cached tokens once, Hermes reasoning once (#1902)

* fix(pricing): stop the prefix price fallback at a minor version boundary

An unlisted minor version (claude-opus-4-9) took the bare-major row
(claude-opus-4, an older and 3x pricier generation) and counted as priced.
The longest dash prefix now decides: when the rest starts with a 1-2 digit
segment, the same boundary display names use, the id stays unpriced instead
of falling to that row or any shorter one.

* fix(why): price token classes at the call's long-context tier

The per-class split, the hand-back, cache saving, carry and prefix estimates
and the one-tier-down reprice priced without the provider and on a slice of
the prompt, so Claude calls above the long-context threshold split into parts
that added up to a fraction of the call cost. Each slice now takes the tier
its whole call's prompt decides.

* fix(droid): keep the minor version of undated model ids

The trailing-index cleanup meant for custom models ran on every id, so
claude-opus-4-6 was stored and priced as claude-opus-4 (3x) and
claude-haiku-4-5 as an unpriced claude-haiku-4. Only custom ids lose a
trailing number now; others lose only a date. The droid parse version moves
so cached sessions re-parse.

* fix(qwen): stop billing cached prompt tokens twice

promptTokenCount includes cachedContentTokenCount, as in Gemini CLI, so
cached tokens were billed at the full input rate and again at the cache-read
rate. Fresh input now subtracts them. The qwen parse version moves so cached
sessions re-parse.

* fix(hermes): stop counting reasoning tokens twice

Hermes stores output tokens as the API reports them, reasoning included, so
the calculated cost and every output total added reasoning on top. Hermes
joins the providers whose output already holds reasoning, and the calculated
basis prices output alone.

* fix(why): count one-hour cache writes in the tier prompt

calculateCost tiers on the larger of the cache write and its one-hour part,
so a slice priced for a call whose prompt is mostly one-hour writes has to
use the same size.

* fix(qwen): count thoughts once whichever backend reported them

Qwen Code's usage keeps Gemini field names but not one convention: Gemini
reports thoughtsTokenCount beside candidatesTokenCount, while the
OpenAI-compatible, Responses and Qwen OAuth converters put reasoning inside
candidates. Like Qwen Code's own outputTokenCount, a total that lies between
candidates and candidates plus thoughts decides the output; otherwise thoughts
are added as before. The qwen parse version moves again.

* fix(hermes): rebuild ledger cursors that double-counted reasoning

Hermes costs live in the session ledger, so the reasoning fix alone left
every recorded calculated cost overstated. Ledger v4 carries the v3 cursors
and marks those whose calculated observations hold reasoning tokens. The
next snapshot a still-present state.db row supplies replaces such a cursor
with one observation at the recomputed cost; the hermes parse version moves
so every session re-parses once and supplies that snapshot.

Sessions whose source row is gone get no snapshot and keep the v3 figure.
Days the daily cache already sealed also keep their old Hermes totals, as
no DAILY_CACHE_VERSION bump is part of this change.

* fix(why): normalise token counts before picking the tier

calculateCost clamps negative and non-finite counts to zero before it sizes
the prompt, so the tier prompt for a slice does the same; otherwise a corrupt
negative count could price the parts in a lower tier than the call.

* fix(qwen): follow Qwen Code's own rule when the total cannot decide

Without a total in the band, Qwen Code's outputTokenCount falls back to the
auth type: thoughts are inside candidates for openai, openai-responses and
qwen-oauth, beside them otherwise. The auth type rides on user records'
executionContext. Records without one keep thoughts out, the conservative
side Qwen Code's own cost ledger also takes.

* fix(qwen): clamp cached tokens to the prompt before pricing

A record reporting more cached tokens than its prompt billed the excess at
the cache-read rate. Cached now never exceeds the prompt it is part of.

* docs(hermes): say why a gone session keeps its v3 ledger cost

A ledger cursor stores token totals and cost but never the model; a session
whose state.db row is gone is already reported under model unknown. Without
a rate, the reasoning share of its calculated cost cannot be taken out, so
it keeps the v3 figure.

* fix(qwen): clamp cached tokens to zero as well as to the prompt

A negative cached count raised fresh input above the prompt and sent a
negative cache read on. Cached now stays within [0, prompt], and a negative
prompt reads as zero.

* fix(qwen): take the auth type per user record, not per file

Qwen Code stamps each user record's executionContext with the auth type in
force at that moment (chatRecordingService getExecutionContext), and it can
change mid-session. A user record without one now leaves it unknown instead
of inheriting the previous record's.

* fix(droid): strip a trailing eight-digit segment only when it is a date

Any terminal eight-digit run came off as a date. Only a valid YYYYMMDD with
a year from 2020 to 2035 is stripped now; model-12345678 keeps its name.

* test: pin the droid, qwen and hermes parse-version bumps

Each bump is what makes cached sessions re-parse under a pricing fix, and
nothing else fails if one is reverted.
This commit is contained in:
Resham Joshi 2026-11-06 16:31:15 +01:00 • committed by GitHub
parent c357b6ce63
commit 2c0eacaea4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 330 additions and 47 deletions

View file

@ -25,8 +25,15 @@ import type { CachedCall, ProviderSection } from './session-cache.js'
// v3 discards the local-only v2 migration candidate. Seeding v2 from the warm
// session cache could preserve lifetime totals while assigning historical
// observations to the migration day, inflating that day's scoped Hermes spend.
export const HERMES_SESSION_LEDGER_FILENAME = 'hermes-session-ledger.v3.json'
export const HERMES_SESSION_LEDGER_VERSION = 3 as const
// v4: the calculated basis stopped adding reasoning tokens on top of an output
// count that already holds them. A v3 cursor that priced reasoning that way is
// carried with `rebuild` set, and the next snapshot its source still supplies
// replaces it with one recomputed observation. A cursor whose source is gone
// gets no such snapshot and keeps the v3 figure: it holds the tokens but not
// the model, so there is no rate to reprice its output at.
export const HERMES_SESSION_LEDGER_FILENAME = 'hermes-session-ledger.v4.json'
export const HERMES_SESSION_LEDGER_VERSION = 4 as const
const HERMES_SESSION_LEDGER_V3_FILENAME = 'hermes-session-ledger.v3.json'
export type HermesCostBasis = 'actual' | 'estimated' | 'calculated' | 'included'
@ -57,6 +64,7 @@ export type HermesSessionCursor = {
sessionId: string
lastSeen: HermesLastSeen
observations: HermesObservation[]
rebuild?: true
}
export type HermesSessionLedger = {
@ -361,10 +369,10 @@ function validateCursor(v: unknown): v is HermesSessionCursor {
return true
}
function validateLedger(raw: unknown): raw is HermesSessionLedger {
function validateLedger(raw: unknown, version: number = HERMES_SESSION_LEDGER_VERSION): raw is HermesSessionLedger {
if (!raw || typeof raw !== 'object') return false
const o = raw as Record<string, unknown>
if (o['version'] !== HERMES_SESSION_LEDGER_VERSION) return false
if (o['version'] !== version) return false
if (!o['cursors'] || typeof o['cursors'] !== 'object' || Array.isArray(o['cursors'])) return false
for (const profileCursors of Object.values(o['cursors'] as Record<string, unknown>)) {
if (!profileCursors || typeof profileCursors !== 'object' || Array.isArray(profileCursors)) return false
@ -373,9 +381,31 @@ function validateLedger(raw: unknown): raw is HermesSessionLedger {
return true
}
function migrateV3Ledger(): HermesSessionLedger | null {
const path = join(getCodeburnCacheDir(), HERMES_SESSION_LEDGER_V3_FILENAME)
if (!existsSync(path)) return null
try {
const parsed: unknown = JSON.parse(readFileSync(path, 'utf-8'))
if (!validateLedger(parsed, 3)) return null
const cursors: HermesSessionLedger['cursors'] = {}
for (const [profile, sessions] of Object.entries(parsed.cursors)) {
cursors[profile] = {}
for (const [sessionId, cursor] of Object.entries(sessions)) {
const reasoningPriced = cursor.observations.some(o => o.costBasis === 'calculated' && o.reasoningTokens > 0)
cursors[profile][sessionId] = reasoningPriced ? { ...cursor, rebuild: true } : cursor
}
}
return { version: HERMES_SESSION_LEDGER_VERSION, cursors }
} catch {
return null
}
}
function readLedgerFromDisk(): HermesSessionLedger {
const path = hermesSessionLedgerPath()
if (!existsSync(path)) {
const migrated = migrateV3Ledger()
if (migrated) return migrated
// The v1 file is superseded and can never be read again: the parse-version
// bump forces a cold re-parse that rebuilds every cursor from source.
// Left behind it is dead bytes that only invite a future reader to trust
@ -448,7 +478,9 @@ export async function persistHermesSessionLedger(ledger: HermesSessionLedger): P
export async function recordHermesSnapshot(snapshot: HermesSnapshot): Promise<HermesSessionCursor> {
const ledger = loadHermesSessionLedger()
const existing = getHermesCursor(ledger, snapshot.profile, snapshot.sessionId)
const stored = getHermesCursor(ledger, snapshot.profile, snapshot.sessionId)
// An all-zero snapshot is a reset or a vanished row, not a source to rebuild from.
const existing = stored?.rebuild && hasPositiveTokenDelta(snapshot.tokens) ? undefined : stored
const { cursor, dirty } = applyHermesSnapshot(existing, snapshot)
if (dirty) await persistHermesSessionLedger(putCursor(ledger, cursor))
return cursor

View file

@ -60,7 +60,8 @@ export type LongContextTier = {
/// top bills it twice.
/// DSH TokenUsage includes reasoning in output too; see the pinned contract:
/// https://github.com/deepseek-ai/deepseek-harness/blob/c291e7961a515f6d7af9304e7fd1d257929aef26/docs/subsystems/llm-streaming.md#tokenusage
const REASONING_INCLUDED_IN_OUTPUT = new Set(['claude', 'codex', 'copilot', 'dsh'])
/// Hermes stores completion/output tokens as the API reports them, reasoning inside.
const REASONING_INCLUDED_IN_OUTPUT = new Set(['claude', 'codex', 'copilot', 'dsh', 'hermes'])
/// Output tokens to bill and display for one call. Single source of truth so
/// the pricing sites and the display sums can never disagree about whether a
@ -846,15 +847,24 @@ function getPriceOverrideExact(...keys: string[]): ModelCosts | null {
return null
}
function getPriceOverridePrefix(canonical: string): ModelCosts | null {
for (const key of getSortedPriceOverrideKeys()) {
if (canonical.startsWith(key + '-') || canonical === key) {
return userPriceOverrides.get(key)!
// Keys run longest first. A 1-2 digit segment after the longest dash prefix is
// a minor version (claude-opus-4-9 is not claude-opus-4), the boundary
// modelKeyMatches applies to display names; every shorter key crosses it too.
function longestPricingPrefix(canonical: string, keys: string[]): string | null {
for (const key of keys) {
if (canonical === key) return key
if (canonical.startsWith(key + '-')) {
return /^\d{1,2}(?:$|[-.])/.test(canonical.slice(key.length + 1)) ? null : key
}
}
return null
}
function getPriceOverridePrefix(canonical: string): ModelCosts | null {
const key = longestPricingPrefix(canonical, getSortedPriceOverrideKeys())
return key === null ? null : userPriceOverrides.get(key)!
}
function getPriceOverrideCaseInsensitive(canonical: string, withPrefix: string): ModelCosts | null {
const lowerIndex = getLowercasePriceOverrideIndex()
return lowerIndex.get(canonical.toLowerCase()) ?? lowerIndex.get(withPrefix.toLowerCase()) ?? null
@ -1367,11 +1377,8 @@ export function getModelCosts(model: string): ModelCosts | null {
// Iterate keys longest-first so a model id like `gpt-5-mini` matches the
// `gpt-5-mini` entry rather than collapsing to the shorter `gpt-5` entry
// due to dictionary insertion order.
for (const key of getSortedPricingKeys()) {
if (canonical.startsWith(key + '-') || canonical === key) {
return pricingCache.get(key)!
}
}
const prefixKey = longestPricingPrefix(canonical, getSortedPricingKeys())
if (prefixKey !== null) return pricingCache.get(prefixKey)!
const caseInsensitiveOverride = getPriceOverrideCaseInsensitive(canonical, withPrefix)
if (caseInsensitiveOverride) return caseInsensitiveOverride
@ -1562,6 +1569,8 @@ export function calculateCost(
oneHourCacheCreationTokens = 0,
provider?: string,
applyDiscount = false,
// A slice of a call (one token class) is priced at the whole call's tier.
tierPromptTokens?: number,
): number {
const costs = getModelCosts(model)
if (!costs) {
@ -1583,7 +1592,7 @@ export function calculateCost(
const safeOneHourCacheCreation = safe(oneHourCacheCreationTokens)
const safeCacheCreation = Math.max(safe(cacheCreationTokens), safeOneHourCacheCreation)
const safeFiveMinuteCacheCreation = Math.max(0, safeCacheCreation - safeOneHourCacheCreation)
const promptTokens = safe(inputTokens) + safe(cacheReadTokens) + safeCacheCreation
const promptTokens = tierPromptTokens ?? safe(inputTokens) + safe(cacheReadTokens) + safeCacheCreation
const tieredCosts = tieredCostsFor(model, speed === 'flex' ? costs.flex ?? costs : costs, promptTokens, provider)
const multiplier = speed === 'fast' ? tieredCosts.fastMultiplier : speed === 'ultrafast' ? tieredCosts.ultrafastMultiplier ?? 1 : 1

View file

@ -74,11 +74,20 @@ function getFactoryDir(): string {
// e.g. "custom:GLM-5.1-[Proxy]-0" -> "GLM-5.1"
// Cost lookup is handled by codeburn's existing calculateCost/getCanonicalName
// which normalizes case and strips date suffixes automatically.
// Only a custom model carries a trailing index; elsewhere `-6` is a version
// and only a real YYYYMMDD date comes off.
function stripDateSuffix(name: string): string {
const m = /-(\d{4})(\d{2})(\d{2})$/.exec(name)
if (!m) return name
const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])]
const date = new Date(Date.UTC(y, mo - 1, d))
const real = y >= 2020 && y <= 2035 && date.getUTCMonth() === mo - 1 && date.getUTCDate() === d
return real ? name.slice(0, m.index) : name
}
function stripModelPrefix(raw: string): string {
return raw
.replace(/^custom:/, '')
.replace(/\[.*?\]/g, '')
.replace(/-\d+$/, '')
const name = raw.replace(/^custom:/, '').replace(/\[.*?\]/g, '')
return (raw.startsWith('custom:') ? name.replace(/-\d+$/, '') : stripDateSuffix(name))
.replace(/-+$/, '')
.replace(/^-/, '')
}

View file

@ -450,7 +450,7 @@ function resolveHermesCost(
const calculatedCost = calculateCost(
model,
tokens.inputTokens,
tokens.outputTokens + tokens.reasoningTokens,
tokens.outputTokens,
tokens.cacheWriteTokens,
tokens.cacheReadTokens,
0,

View file

@ -36,6 +36,7 @@ type QwenEntry = {
subtype?: string
cwd?: string
model?: string
executionContext?: { authType?: string }
message?: {
role: string
parts: QwenPart[]
@ -69,6 +70,21 @@ function isoTimestamp(value: string | undefined, fallback: string): string {
return fallback
}
// Qwen Code's outputTokenCount (packages/core/src/telemetry/gen-ai-usage.ts):
// thoughts sit inside candidates on the OpenAI-compatible, Responses and Qwen
// OAuth backends and beside them elsewhere; a total in the band both allow
// decides, else the auth type does. Records older than the auth type on user
// records leave it unknown, and then thoughts are not added: the converters
// Qwen Code ships mostly fold them in, and its own cost ledger bills
// candidates alone without a total.
const THOUGHTS_IN_CANDIDATES = new Set(['openai', 'openai-responses', 'qwen-oauth'])
function billedOutputTokens(candidates: number, thoughts: number, total: number, prompt: number, authType: string | undefined): number {
const derived = total - prompt
if (total > 0 && derived >= candidates && derived <= candidates + thoughts) return derived
return authType === undefined || THOUGHTS_IN_CANDIDATES.has(authType) ? candidates : candidates + thoughts
}
function extractTools(parts: QwenPart[]): { tools: string[]; bashCommands: string[] } {
const tools: string[] = []
const bashCommands: string[] = []
@ -97,6 +113,7 @@ function createParser(source: SessionSource, seenKeys: Set<string>): SessionPars
const lines = raw.split('\n').filter(l => l.trim())
let pendingUserMessage = ''
let authType: string | undefined
for (const line of lines) {
let entry: QwenEntry
@ -105,6 +122,8 @@ function createParser(source: SessionSource, seenKeys: Set<string>): SessionPars
} catch {
continue
}
// Qwen Code stamps each user record with the auth type in force then (it can change mid-session).
if (entry.type === 'user') authType = typeof entry.executionContext?.authType === 'string' ? entry.executionContext.authType : undefined
if (entry.type === 'user' && entry.message) {
const texts = (entry.message.parts ?? [])
@ -128,12 +147,17 @@ function createParser(source: SessionSource, seenKeys: Set<string>): SessionPars
const model = entry.model || 'qwen-auto'
const { tools, bashCommands } = extractTools(entry.message?.parts ?? [])
const inputTokens = usage.promptTokenCount
const outputTokens = usage.candidatesTokenCount
const reasoningTokens = usage.thoughtsTokenCount ?? 0
const cachedTokens = usage.cachedContentTokenCount ?? 0
// promptTokenCount includes the cached tokens, as in Gemini CLI.
const prompt = Math.max(0, usage.promptTokenCount ?? 0)
const candidates = usage.candidatesTokenCount ?? 0
const thoughts = usage.thoughtsTokenCount ?? 0
const cachedTokens = Math.min(Math.max(0, usage.cachedContentTokenCount ?? 0), prompt)
const inputTokens = prompt - cachedTokens
const billedOutput = billedOutputTokens(candidates, thoughts, usage.totalTokenCount ?? 0, prompt, authType)
const reasoningTokens = Math.min(thoughts, billedOutput)
const outputTokens = billedOutput - reasoningTokens
const costUSD = calculateCost(model, inputTokens, outputTokens + reasoningTokens, 0, cachedTokens, 0)
const costUSD = calculateCost(model, inputTokens, billedOutput, 0, cachedTokens, 0)
yield {
provider: 'qwen',

View file

@ -526,6 +526,9 @@ export const PROVIDER_PARSE_VERSIONS: Record<string, string> = {
// v0-v4 generations, embedded attempt streams, retry accounting, and the
// version-specific inherited-prefix rules all change cached DSH calls.
dsh: 'session-formats-v0-v4-attempts-v6',
// model-minor-version-v1: undated ids keep their minor version
// (claude-opus-4-6 no longer stored and priced as claude-opus-4).
droid: 'model-minor-version-v1',
// cost-provenance-v3: preserve Hermes included/estimated/actual status and
// rebuild the provider section alongside the v3 lifetime ledger. The parse
// bump is required with the ledger bump: seeding a new ledger from a section
@ -536,7 +539,9 @@ export const PROVIDER_PARSE_VERSIONS: Record<string, string> = {
// `billing` (`included` -> subscription, `actual` -> metered). Cached calls
// hold none, so they must re-parse.
// helper-usage-v1: auxiliary-model rows in session_model_usage now become calls.
hermes: 'reasoning-output-accounting-v1-est-cost-routed-ids-workspace-pr-v5-cost-provenance-v3-billing-route-v1-billing-mode-v1-helper-usage-v1',
// ledger-v4: sessions re-parse so v4 can rebuild the cursors whose calculated
// cost added reasoning on top of output.
hermes: 'reasoning-output-accounting-v1-est-cost-routed-ids-workspace-pr-v5-cost-provenance-v3-billing-route-v1-billing-mode-v1-helper-usage-v1-ledger-v4',
// reported-cost-v1: OpenClaw's per-message `usage.cost.total` is now
// preserved through the cache via `costFromBilling`. This is OpenClaw's
// first parse version; adding it moves the provider's env fingerprint,
@ -608,6 +613,10 @@ export const PROVIDER_PARSE_VERSIONS: Record<string, string> = {
// cannot price rides on the cached call as `fallbackCostUSD`, the session-level
// rollup uses the bare model id, and `google-vertex`/`google-vertex-anthropic`
// ride as the `vertex` route. Cached calls hold none of these.
// cached-prompt-v1: promptTokenCount includes the cached tokens, which
// were billed again as fresh input.
// output-thoughts-v1: thoughts already inside candidates are not added again.
qwen: 'cached-prompt-v1-output-thoughts-v1',
opencode: 'session-model-v1-archived-subtree-v1-billing-routes-v2-v2-legacy-union-v1-unknown-usage-v1-vertex-fallback-cost-v1',
quickdesk: 'emf-sqlite-v2-est-cost',
// session-lineage-capture-v1: SessionLineage (CB-1, slice 1) is now carried

View file

@ -170,18 +170,24 @@ function capTail(s: string, n = 40): string {
const ZERO_PARTS: WhyParts = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, webSearch: 0 }
// calculateCost is linear per token kind for Claude (no provider tier), so the
// parts priced one at a time add up to the call's cost.
// Tokens priced on a call's behalf take that call's long-context tier, which
// its whole prompt decides, so the parts priced one at a time add up to its cost.
function priceFor(c: ParsedApiCall, input: number, output: number, cacheWrite: number, cacheRead: number, webSearch = 0, oneHour = 0, model = c.model): number {
const safe = (n: number | undefined) => (n !== undefined && Number.isFinite(n) && n > 0 ? n : 0)
const prompt = safe(c.usage.inputTokens) + safe(c.usage.cacheReadInputTokens) + Math.max(safe(c.usage.cacheCreationInputTokens), safe(c.cacheCreationOneHourTokens))
return calculateCost(model, input, output, cacheWrite, cacheRead, webSearch, c.speed, oneHour, c.provider, false, prompt)
}
export function partsOf(c: ParsedApiCall): WhyParts {
if (c.costUSD === 0) return { ...ZERO_PARTS }
const { model: m, speed: s, usage: u } = c
const d = c.listCostUSD === undefined ? NO_DISCOUNT : discountFactors(m, c.provider)
const u = c.usage
const d = c.listCostUSD === undefined ? NO_DISCOUNT : discountFactors(c.model, c.provider)
return {
input: calculateCost(m, u.inputTokens, 0, 0, 0, 0, s) * d.input,
output: calculateCost(m, 0, u.outputTokens, 0, 0, 0, s) * d.output,
cacheRead: calculateCost(m, 0, 0, 0, u.cacheReadInputTokens, 0, s) * d.cacheRead,
cacheWrite: calculateCost(m, 0, 0, u.cacheCreationInputTokens, 0, 0, s, c.cacheCreationOneHourTokens ?? 0) * d.cacheWrite,
webSearch: calculateCost(m, 0, 0, 0, 0, u.webSearchRequests, s) * d.other,
input: priceFor(c, u.inputTokens, 0, 0, 0) * d.input,
output: priceFor(c, 0, u.outputTokens, 0, 0) * d.output,
cacheRead: priceFor(c, 0, 0, 0, u.cacheReadInputTokens) * d.cacheRead,
cacheWrite: priceFor(c, 0, 0, u.cacheCreationInputTokens, 0, 0, c.cacheCreationOneHourTokens ?? 0) * d.cacheWrite,
webSearch: priceFor(c, 0, 0, 0, 0, u.webSearchRequests) * d.other,
}
}
@ -214,8 +220,8 @@ function repriced(calls: ParsedApiCall[]): WhyAlt | null {
const models = new Set(calls.map(c => c.model))
const alt = models.size === 1 ? tierDown([...models][0]!) : null
if (!alt) return null
const cost = calls.reduce((s, c) => s + calculateCost(alt, c.usage.inputTokens, c.usage.outputTokens, c.usage.cacheCreationInputTokens,
c.usage.cacheReadInputTokens, c.usage.webSearchRequests, c.speed, c.cacheCreationOneHourTokens ?? 0), 0)
const cost = calls.reduce((s, c) => s + priceFor(c, c.usage.inputTokens, c.usage.outputTokens, c.usage.cacheCreationInputTokens,
c.usage.cacheReadInputTokens, c.usage.webSearchRequests, c.cacheCreationOneHourTokens ?? 0, alt), 0)
return cost > 0 && cost < costOf(calls) ? { model: getShortModelName(alt), cost } : null
}
@ -627,8 +633,8 @@ export async function buildSessionWhy(filePath: string): Promise<SessionWhy> {
delegatedAt: spawn ? iso(spawn.tu.ts) : null, firstCallAt: models[0] ? iso(base + models[0].start) : null, lastCallAt: models.length ? iso(base + models.at(-1)!.end) : null,
handedBackAt: back ? iso(back.ts) : null, handedBackIn: k >= 0 ? k + 1 : null,
taskTokensEst: estimateTokensFromChars(str(input['prompt']).length), reportTokensEst,
handBackCost: reader ? Math.min(reader.costUSD, calculateCost(reader.model, 0, 0, reportTokensEst, 0, 0, reader.speed)) : 0,
cacheSaving: h.callList.reduce((s, c) => s + calculateCost(c.model, c.usage.cacheReadInputTokens, 0, 0, 0, 0, c.speed) - calculateCost(c.model, 0, 0, 0, c.usage.cacheReadInputTokens, 0, c.speed), 0),
handBackCost: reader ? Math.min(reader.costUSD, priceFor(reader, 0, 0, reportTokensEst, 0)) : 0,
cacheSaving: h.callList.reduce((s, c) => s + priceFor(c, c.usage.cacheReadInputTokens, 0, 0, 0) - priceFor(c, 0, 0, 0, c.usage.cacheReadInputTokens), 0),
requests: h.calls, toolCalls: steps.filter(s => s.kind === 'tool').length, parts, tokens, steps,
}
}
@ -811,8 +817,8 @@ function buildFindings({ read, turns, helpers, turnOfHelper, allCalls, stepResul
const later = allCalls.filter(c => c.ts >= ts && c.ts < stop)
if (later.length === 0) return null
const first = later[0]!.call
const writeUsd = calculateCost(first.model, 0, 0, tokens, 0, 0, first.speed, (first.cacheCreationOneHourTokens ?? 0) > 0 ? tokens : 0)
const readUsd = later.slice(1).reduce((s, c) => s + calculateCost(c.call.model, 0, 0, 0, tokens, 0, c.call.speed), 0)
const writeUsd = priceFor(first, 0, 0, tokens, 0, 0, (first.cacheCreationOneHourTokens ?? 0) > 0 ? tokens : 0)
const readUsd = later.slice(1).reduce((s, c) => s + priceFor(c.call, 0, 0, 0, tokens), 0)
return { tokens, calls: later.length, writeUsd, readUsd }
}
for (const x of toolSteps) {
@ -840,7 +846,7 @@ function buildFindings({ read, turns, helpers, turnOfHelper, allCalls, stepResul
const cached = first.usage.cacheReadInputTokens + first.usage.cacheCreationInputTokens
const readers = later.filter(c => c.call.usage.cacheReadInputTokens >= cached)
const writeUsd = partsOf(first).cacheWrite
const readUsd = readers.reduce((s, c) => s + calculateCost(c.call.model, 0, 0, 0, cached, 0, c.call.speed), 0)
const readUsd = readers.reduce((s, c) => s + priceFor(c.call, 0, 0, 0, cached), 0)
out.push({ id: '', kind: 'prefix', estimate: true, turn: 1, usd: writeUsd + readUsd, share: share(writeUsd + readUsd), tokens, cached, uncached: first.usage.cacheCreationInputTokens, writeUsd, readUsd, laterCalls: later.length, readCalls: readers.length })
}
}

View file

@ -1157,6 +1157,17 @@ describe('existing model names still resolve', () => {
expect(v48!.outputCostPerToken).toBeCloseTo(0.000025, 12)
expect(v48!.inputCostPerToken).not.toEqual(getModelCosts('claude-opus-4')!.inputCostPerToken)
})
it('an unlisted minor version stays unpriced instead of taking the bare-major row', () => {
expect(getModelCosts('claude-opus-4-99')).toBeNull()
expect(getModelCosts('claude-opus-4-9')).toBeNull()
expect(getModelCosts('claude-sonnet-4-5-20250929')).not.toBeNull()
expect(getModelCosts('gpt-4o-2024-08-06')).not.toBeNull()
expect(getModelCosts('gpt-5-foo')).toBe(getModelCosts('gpt-5'))
setPriceOverrides({ 'zzz-probe-4': { input: 1, output: 2 } })
expect(getModelCosts('zzz-probe-4-1')).toBeNull()
expect(getModelCosts('zzz-probe-4-fast')!.inputCostPerToken).toBe(1e-6)
})
})
// Issue #159: every model name Cursor emits in its SQLite database must

View file

@ -0,0 +1,12 @@
import { describe, expect, it } from 'vitest'
import { PROVIDER_PARSE_VERSIONS } from '../src/session-cache.js'
describe('provider parse versions', () => {
it('carry the bumps that re-parse cached sessions after pricing-rule fixes', () => {
expect(PROVIDER_PARSE_VERSIONS['droid']).toContain('model-minor-version-v1')
expect(PROVIDER_PARSE_VERSIONS['qwen']).toContain('cached-prompt-v1')
expect(PROVIDER_PARSE_VERSIONS['qwen']).toContain('output-thoughts-v1')
expect(PROVIDER_PARSE_VERSIONS['hermes']).toContain('ledger-v4')
})
})

View file

@ -119,6 +119,20 @@ describe('droid provider', () => {
expect(await parseAll(path, seen)).toHaveLength(0)
})
it('keeps the minor version of an undated model id', async () => {
const path = await writeSession({ settings: { model: 'claude-opus-4-6', tokenUsage: { inputTokens: 1_000_000, outputTokens: 100_000, cacheCreationTokens: 0, cacheReadTokens: 0, thinkingTokens: 0 } } })
const calls = await parseAll(path)
expect(calls[0]!.model).toBe('claude-opus-4-6')
expect(calls[0]!.costUSD).toBeCloseTo(7.5, 6)
})
it('strips only a real date from an undated-looking id', () => {
const provider = createDroidProvider(factoryDir)
expect(provider.modelDisplayName('model-12345678')).toBe('model-12345678')
expect(provider.modelDisplayName('model-20260301')).toBe('model')
expect(provider.modelDisplayName('model-20260231')).toBe('model-20260231')
})
it('strips Droid model wrappers for display', () => {
const provider = createDroidProvider(factoryDir)
expect(provider.modelDisplayName('custom:GLM-5.1-[Proxy]-0')).toBe('GLM-5.1')

View file

@ -4,7 +4,7 @@ import { tmpdir, homedir } from 'os'
import { createRequire } from 'node:module'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { calculateCost } from '../../src/models.js'
import { billableOutputTokens, calculateCost } from '../../src/models.js'
import { createHermesProvider } from '../../src/providers/hermes.js'
import { isSqliteAvailable } from '../../src/sqlite.js'
import { aggregateProjectsIntoDays } from '../../src/day-aggregator.js'
@ -541,8 +541,34 @@ skipUnlessSqlite('hermes provider', () => {
const calls = await collectCalls(tmpDir, `${dbPath}#hermes-session=no-cost-session`)
expect(calls).toHaveLength(1)
expect(calls[0]!.costUSD).toBe(calculateCost('claude-sonnet-4-20250514', 1000, 250, 0, 0, 0))
// Output already includes the reasoning tokens.
expect(calls[0]!.costUSD).toBe(calculateCost('claude-sonnet-4-20250514', 1000, 200, 0, 0, 0))
expect(calls[0]!.reasoningTokens).toBe(50)
expect(billableOutputTokens('hermes', calls[0]!.outputTokens, calls[0]!.reasoningTokens)).toBe(200)
})
it('recomputes a v3 ledger cursor that priced reasoning on top of output, and keeps one whose source is gone', async () => {
const model = 'claude-sonnet-4-20250514'
const tokens = { inputTokens: 1000, outputTokens: 200, cacheReadTokens: 0, cacheWriteTokens: 0, reasoningTokens: 50 }
const v3Cursor = (sessionId: string, costUSD: number) => ({
profile: 'default', sessionId,
lastSeen: { ...tokens, costUSD, costBasis: 'calculated' },
observations: [{ index: 0, timestamp: '2026-05-23T12:33:20.000Z', ...tokens, costUSD, costBasis: 'calculated', supplementaryAccounting: false }],
})
await writeFile(join(cacheDir, 'hermes-session-ledger.v3.json'), JSON.stringify({
version: 3,
cursors: { default: { 'live-session': v3Cursor('live-session', calculateCost(model, 1000, 250, 0, 0, 0)), 'gone-session': v3Cursor('gone-session', 9.99) } },
}))
const dbPath = createHermesDb(tmpDir)
withTestDb(dbPath, (db) => {
insertSession(db, { id: 'live-session', model, ...tokens, estimatedCost: null, actualCost: null, startedAt: 1779539600 })
})
const live = await collectCalls(tmpDir, `${dbPath}#hermes-session=live-session`)
expect(live).toHaveLength(1)
expect(live[0]!.costUSD).toBe(calculateCost(model, 1000, 200, 0, 0, 0))
const gone = await collectCalls(tmpDir, `${dbPath}#hermes-session=gone-session`)
expect(gone.reduce((sum, call) => sum + call.costUSD, 0)).toBe(9.99)
})
it('carries the billing route the session recorded, and stays silent for every other door', async () => {

View file

@ -0,0 +1,103 @@
import { mkdtemp, mkdir, rm, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { join } from 'path'
import { afterEach, beforeAll, describe, expect, it } from 'vitest'
import { calculateCost, loadPricing } from '../../src/models.js'
import { createQwenProvider } from '../../src/providers/qwen.js'
import type { ParsedProviderCall } from '../../src/providers/types.js'
const dirs: string[] = []
beforeAll(async () => {
await loadPricing()
})
afterEach(async () => {
await Promise.all(dirs.splice(0).map(d => rm(d, { recursive: true, force: true })))
})
const model = 'qwen3-coder-flash'
async function parseUsage(usageMetadata: Record<string, number>, authType?: string): Promise<ParsedProviderCall[]> {
const dir = await mkdtemp(join(tmpdir(), 'codeburn-qwen-test-'))
dirs.push(dir)
const chats = join(dir, '-tmp-demo', 'chats')
await mkdir(chats, { recursive: true })
const user = { uuid: 'u1', sessionId: 's1', timestamp: '2026-10-20T09:59:59.000Z', type: 'user', message: { role: 'user', parts: [{ text: 'go' }] }, executionContext: { modelId: model, authType } }
await writeFile(join(chats, 's1.jsonl'), [...(authType ? [user] : []), {
uuid: 'a1', sessionId: 's1', timestamp: '2026-10-20T10:00:00.000Z', type: 'assistant', model,
message: { role: 'model', parts: [{ text: 'done' }] },
usageMetadata,
}].map(r => JSON.stringify(r)).join('\n') + '\n')
const provider = createQwenProvider(dir)
const [source] = await provider.discoverSessions()
const calls: ParsedProviderCall[] = []
for await (const call of provider.createSessionParser(source!, new Set()).parse()) calls.push(call)
return calls
}
describe('qwen provider', () => {
it('bills cached prompt tokens once, at the cache-read rate', async () => {
const calls = await parseUsage({ promptTokenCount: 100_000, candidatesTokenCount: 1_000, thoughtsTokenCount: 0, totalTokenCount: 101_000, cachedContentTokenCount: 90_000 })
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(10_000)
expect(calls[0]!.cacheReadInputTokens).toBe(90_000)
expect(calls[0]!.costUSD).toBeGreaterThan(0)
expect(calls[0]!.costUSD).toBeCloseTo(calculateCost(model, 10_000, 1_000, 0, 90_000, 0), 12)
})
it('adds thoughts reported beside candidates (Gemini backend)', async () => {
const [call] = await parseUsage({ promptTokenCount: 1_000, candidatesTokenCount: 200, thoughtsTokenCount: 50, totalTokenCount: 1_250, cachedContentTokenCount: 0 })
expect(call!.outputTokens + call!.reasoningTokens).toBe(250)
expect(call!.costUSD).toBeCloseTo(calculateCost(model, 1_000, 250, 0, 0, 0), 12)
})
it('does not add thoughts already inside candidates (OpenAI-compatible backend)', async () => {
const [call] = await parseUsage({ promptTokenCount: 1_000, candidatesTokenCount: 200, thoughtsTokenCount: 50, totalTokenCount: 1_200, cachedContentTokenCount: 0 })
expect(call!.outputTokens).toBe(150)
expect(call!.reasoningTokens).toBe(50)
expect(call!.costUSD).toBeCloseTo(calculateCost(model, 1_000, 200, 0, 0, 0), 12)
})
it('without a total, the recorded auth type decides, and an unknown one adds no thoughts', async () => {
const usage = { promptTokenCount: 1_000, candidatesTokenCount: 200, thoughtsTokenCount: 50, cachedContentTokenCount: 0 }
const [gemini] = await parseUsage(usage, 'gemini')
expect(gemini!.costUSD).toBeCloseTo(calculateCost(model, 1_000, 250, 0, 0, 0), 12)
const [openai] = await parseUsage(usage, 'openai')
expect(openai!.costUSD).toBeCloseTo(calculateCost(model, 1_000, 200, 0, 0, 0), 12)
const [unknown] = await parseUsage(usage)
expect(unknown!.costUSD).toBeCloseTo(calculateCost(model, 1_000, 200, 0, 0, 0), 12)
})
it('a user record without an auth type does not inherit the previous one', async () => {
const dir = await mkdtemp(join(tmpdir(), 'codeburn-qwen-test-'))
dirs.push(dir)
const chats = join(dir, '-tmp-demo', 'chats')
await mkdir(chats, { recursive: true })
const user = (uuid: string, executionContext?: unknown) => ({ uuid, sessionId: 's1', timestamp: '2026-10-20T09:59:59.000Z', type: 'user', message: { role: 'user', parts: [{ text: 'go' }] }, ...(executionContext ? { executionContext } : {}) })
await writeFile(join(chats, 's1.jsonl'), [user('u1', { modelId: model, authType: 'gemini' }), user('u2'), {
uuid: 'a1', sessionId: 's1', timestamp: '2026-10-20T10:00:00.000Z', type: 'assistant', model,
message: { role: 'model', parts: [{ text: 'done' }] },
usageMetadata: { promptTokenCount: 1_000, candidatesTokenCount: 200, thoughtsTokenCount: 50, cachedContentTokenCount: 0 },
}].map(r => JSON.stringify(r)).join('\n') + '\n')
const provider = createQwenProvider(dir)
const [source] = await provider.discoverSessions()
const calls: ParsedProviderCall[] = []
for await (const call of provider.createSessionParser(source!, new Set()).parse()) calls.push(call)
expect(calls[0]!.costUSD).toBeCloseTo(calculateCost(model, 1_000, 200, 0, 0, 0), 12)
})
it('never bills more cached tokens than the prompt held', async () => {
const [call] = await parseUsage({ promptTokenCount: 1_000, candidatesTokenCount: 100, thoughtsTokenCount: 0, totalTokenCount: 1_100, cachedContentTokenCount: 5_000 })
expect(call!.inputTokens).toBe(0)
expect(call!.cacheReadInputTokens).toBe(1_000)
expect(call!.costUSD).toBeCloseTo(calculateCost(model, 0, 100, 0, 1_000, 0), 12)
})
it('reads a negative cached count as none', async () => {
const [call] = await parseUsage({ promptTokenCount: 10, candidatesTokenCount: 5, thoughtsTokenCount: 0, totalTokenCount: 15, cachedContentTokenCount: -20 })
expect(call!.inputTokens).toBe(10)
expect(call!.cacheReadInputTokens).toBe(0)
})
})

View file

@ -5,8 +5,8 @@ import { join } from 'node:path'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { getModelCosts, loadPricing } from '../src/models.js'
import { buildSessionWhy, errorCause, findingText, fitWhyJson, redact, renderSessionWhyText, type SessionWhy, type WhyFinding } from '../src/session-why.js'
import { calculateCost, getModelCosts, loadPricing } from '../src/models.js'
import { buildSessionWhy, partsOf, errorCause, findingText, fitWhyJson, redact, renderSessionWhyText, type SessionWhy, type WhyFinding } from '../src/session-why.js'
import { CLEARED, REDIRECTED } from './setup/env-isolation-vars.js'
const SID = '0c4f6c1e-5a7b-4e43-9d0e-2b1f7a6c9e10'
@ -222,6 +222,34 @@ describe('session cost diagnosis: independent recomputation', () => {
})
})
describe('session cost diagnosis: long-context parts', () => {
it('a call above the long-context threshold splits into parts that add up to its cost', () => {
for (const model of ['claude-haiku-5-5', 'claude-sonnet-4-5']) {
expect(getModelCosts(model)!.longContextTier).toBeDefined()
const usage = { inputTokens: 2000, outputTokens: 1000, cacheCreationInputTokens: 3000, cacheReadInputTokens: 250_000, cachedInputTokens: 0, reasoningTokens: 0, webSearchRequests: 0 }
const costUSD = calculateCost(model, 2000, 1000, 3000, 250_000, 0, 'standard', 0, 'claude')
const p = partsOf({ model, provider: 'claude', speed: 'standard', usage, costUSD } as Parameters<typeof partsOf>[0])
expect(p.input + p.output + p.cacheRead + p.cacheWrite + p.webSearch).toBeCloseTo(costUSD, 12)
}
})
it('negative token counts do not move the parts to another tier than the call', () => {
const model = 'claude-sonnet-4-5'
const usage = { inputTokens: -100_000, outputTokens: 0, cacheCreationInputTokens: 0, cacheReadInputTokens: 250_001, cachedInputTokens: 0, reasoningTokens: 0, webSearchRequests: 0 }
const costUSD = calculateCost(model, -100_000, 0, 0, 250_001, 0, 'standard', 0, 'claude')
const p = partsOf({ model, provider: 'claude', speed: 'standard', usage, costUSD } as Parameters<typeof partsOf>[0])
expect(p.input + p.output + p.cacheRead + p.cacheWrite + p.webSearch).toBeCloseTo(costUSD, 12)
})
it('one-hour cache writes count toward the tier the parts are priced at', () => {
const model = 'claude-haiku-5-5'
const usage = { inputTokens: 1, outputTokens: 0, cacheCreationInputTokens: 0, cacheReadInputTokens: 0, cachedInputTokens: 0, reasoningTokens: 0, webSearchRequests: 0 }
const costUSD = calculateCost(model, 1, 0, 0, 0, 0, 'standard', 250_001, 'claude')
const p = partsOf({ model, provider: 'claude', speed: 'standard', usage, cacheCreationOneHourTokens: 250_001, costUSD } as Parameters<typeof partsOf>[0])
expect(p.input + p.output + p.cacheRead + p.cacheWrite + p.webSearch).toBeCloseTo(costUSD, 12)
})
})
describe('session cost diagnosis: rules', () => {
it('flags the costly prompt with its median multiple and a one-tier-down repricing', () => {
const [f] = find('hotspot')