fix(codex): stop double-counting reasoning tokens in Tok/s throughput

The same reasoning-inclusion bug #1075/#1078 fixed for cost also affected
the throughput display: activeGeneratedTokens/taskGeneratedTokens in the
Codex parser and generatedTokens in the live codex-tps reader summed
outputTokens + reasoningTokens, but reasoning is already inside output.
All three sites now route through the billableOutputTokens('codex', ...)
helper #1078 introduced, so the throughput numerator can never drift from
the billed one.

Cost and every token count are unchanged (verified byte-identical on a
real 51,753-call corpus); Tok/s drops 20-42% depending on how
reasoning-heavy the model is.

activeGeneratedTokens/activeDurationMs/toolWaitMs are stored verbatim in
both the Codex result cache and the session cache rather than re-derived
on read, so neither self-heals: CODEX_CACHE_VERSION moves 11 -> 13 (12 is
claimed by feat/core-extraction's own port of this feature) and
PROVIDER_PARSE_VERSIONS.codex gains a codex-tps-v1 suffix, forcing Codex
sessions to re-parse once.

Closes #1079
This commit is contained in:
iamtoruk 2026-08-21 14:42:04 -07:00
parent aaf9a4b780
commit bfcf63ce3a
8 changed files with 63 additions and 10 deletions

View file

@ -27,7 +27,14 @@ import type { ParsedProviderCall } from './providers/types.js'
// output, and cache_write_input_tokens is carved out of the input bucket. This
// file stores each call's costUSD and token buckets verbatim, so entries
// written by v10 carry the old (overstated) cost and must be re-derived.
export const CODEX_CACHE_VERSION = 11
// v13: codex throughput fix (#1079) - activeGeneratedTokens was summing
// output + reasoning, the same double-count Fix A removed from cost. This
// file stores activeGeneratedTokens/activeDurationMs/toolWaitMs verbatim (not
// re-derived on read), so v11 entries carry the overstated numerator and must
// re-parse. Not 12: v12 is claimed by feat/core-extraction's own port of this
// throughput feature (PR #1086), so reusing it would let two incompatible
// schemas share a filename.
export const CODEX_CACHE_VERSION = 13
export const CODEX_LEGACY_CACHE_FILE = 'codex-results.json'
export function codexCacheFileName(version = CODEX_CACHE_VERSION): string {
return `codex-results.v${version}.json`

View file

@ -1,6 +1,8 @@
import { open, stat } from 'node:fs/promises'
import { StringDecoder } from 'node:string_decoder'
import { billableOutputTokens } from './models.js'
export type CodexThroughputPoint = {
timestamp: string
model?: string
@ -400,7 +402,9 @@ export class CodexThroughputReader {
state.previousOutput = total?.output_tokens ?? state.previousOutput
state.previousReasoning = total?.reasoning_output_tokens ?? state.previousReasoning
}
const generatedTokens = outputTokens + reasoningTokens
// Reasoning is already inside output_tokens (#1075/#1078); same numerator
// as the cost path so live Tok/s can't drift from billed tokens (#1079).
const generatedTokens = billableOutputTokens('codex', outputTokens, reasoningTokens)
if (generatedTokens <= 0) return
const timestampMs = Date.parse(entry.timestamp)
if (!Number.isFinite(timestampMs)) return

View file

@ -1768,7 +1768,7 @@ function buildSessionSummary(
modelBreakdown[modelKey].tokens.reasoningTokens += call.usage.reasoningTokens
if (call.activeDurationMs !== undefined) {
modelBreakdown[modelKey].activeDurationMs = (modelBreakdown[modelKey].activeDurationMs ?? 0) + call.activeDurationMs
modelBreakdown[modelKey].activeGeneratedTokens = (modelBreakdown[modelKey].activeGeneratedTokens ?? 0) + (call.activeGeneratedTokens ?? call.usage.outputTokens + call.usage.reasoningTokens)
modelBreakdown[modelKey].activeGeneratedTokens = (modelBreakdown[modelKey].activeGeneratedTokens ?? 0) + (call.activeGeneratedTokens ?? billableOutputTokens(call.provider, call.usage.outputTokens, call.usage.reasoningTokens))
modelBreakdown[modelKey].toolWaitMs = (modelBreakdown[modelKey].toolWaitMs ?? 0) + (call.toolWaitMs ?? 0)
}

View file

@ -879,7 +879,10 @@ function createParser(source: SessionSource, seenKeys: Set<string>, capture?: {
const activeMs = durationMs - toolWaitMs
if (activeMs <= 0) continue
for (const call of pendingTaskCalls) {
const generated = call.outputTokens + call.reasoningTokens
// Reasoning is already inside output_tokens (#1075/#1078); the
// throughput numerator must agree with the cost numerator or
// Tok/s reads high for reasoning-heavy calls (#1079).
const generated = billableOutputTokens('codex', call.outputTokens, call.reasoningTokens)
if (generated <= 0) continue
call.activeGeneratedTokens = generated
call.activeDurationMs = activeMs * (generated / taskGeneratedTokens)
@ -1140,7 +1143,7 @@ function createParser(source: SessionSource, seenKeys: Set<string>, capture?: {
...(pendingLocRemoved ? { locRemoved: pendingLocRemoved } : {}),
...(pendingEditFailed ? { editFailed: pendingEditFailed } : {}),
})
taskGeneratedTokens += outputTokens + reasoningTokens
taskGeneratedTokens += billableOutputTokens('codex', outputTokens, reasoningTokens)
pendingTools = []
pendingToolSequence = []

View file

@ -286,7 +286,11 @@ export const PROVIDER_PARSE_VERSIONS: Record<string, string> = {
// models with an explicit cache-write rate. The bucket move does NOT self-heal
// on read (cached entries store the buckets, not the raw event), so cached
// sessions must re-parse.
codex: 'mcp-attribution-v5-est-cost-active-timing-mcp-wait-rich-capture-v1-cross-provider-pr-v1-session-meta-model-v1-session-meta-fields-v1-codex-pricing-v1',
// codex-tps-v1 (#1079): activeGeneratedTokens summed output + reasoning, the
// same double-count codex-pricing-v1 removed from cost. Cached entries store
// activeGeneratedTokens/activeDurationMs/toolWaitMs verbatim (cachedCallToApiCall
// passes them through without recomputing), so this does NOT self-heal either.
codex: 'mcp-attribution-v5-est-cost-active-timing-mcp-wait-rich-capture-v1-cross-provider-pr-v1-session-meta-model-v1-session-meta-fields-v1-codex-pricing-v1-codex-tps-v1',
cursor: 'composer-anchored-crediting-v1-est-cost',
'cursor-agent': 'workspaceless-transcript-v1',
// source-provenance-v1 (#944): CLI sessions were misread as VS Code