Skip to content

Commit 83e2d14

Browse files
committed
codex: tighten fallback handling for legacy logs
1 parent 196afe8 commit 83e2d14

6 files changed

Lines changed: 101 additions & 23 deletions

File tree

‎apps/codex/src/_types.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,25 @@ export type TokenUsageDelta = {
99
export type TokenUsageEvent = {
1010
timestamp: string;
1111
model?: string;
12+
isFallbackModel?: boolean;
1213
} & TokenUsageDelta;
1314

15+
export type ModelUsage = TokenUsageDelta & {
16+
isFallback?: boolean;
17+
};
18+
1419
export type DailyUsageSummary = {
1520
date: string;
1621
firstTimestamp: string;
1722
costUSD: number;
18-
models: Map<string, TokenUsageDelta>;
23+
models: Map<string, ModelUsage>;
1924
} & TokenUsageDelta;
2025

2126
export type MonthlyUsageSummary = {
2227
month: string;
2328
firstTimestamp: string;
2429
costUSD: number;
25-
models: Map<string, TokenUsageDelta>;
30+
models: Map<string, ModelUsage>;
2631
} & TokenUsageDelta;
2732

2833
export type ModelPricing = {
@@ -48,7 +53,7 @@ export type DailyReportRow = {
4853
reasoningOutputTokens: number;
4954
totalTokens: number;
5055
costUSD: number;
51-
models: Record<string, TokenUsageDelta>;
56+
models: Record<string, ModelUsage>;
5257
};
5358

5459
export type MonthlyReportRow = {
@@ -59,5 +64,5 @@ export type MonthlyReportRow = {
5964
reasoningOutputTokens: number;
6065
totalTokens: number;
6166
costUSD: number;
62-
models: Record<string, TokenUsageDelta>;
67+
models: Record<string, ModelUsage>;
6368
};

‎apps/codex/src/command-utils.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ export function splitUsageTokens(usage: UsageGroup): {
1515
} {
1616
const cacheReadTokens = Math.min(usage.cachedInputTokens, usage.inputTokens);
1717
const inputTokens = Math.max(usage.inputTokens - cacheReadTokens, 0);
18-
const outputTokens = usage.outputTokens;
19-
const reasoningTokens = usage.reasoningOutputTokens;
18+
const outputTokens = Math.max(usage.outputTokens, 0);
19+
const rawReasoning = usage.reasoningOutputTokens ?? 0;
20+
const reasoningTokens = Math.max(0, Math.min(rawReasoning, outputTokens));
2021

2122
return {
2223
inputTokens,
@@ -26,7 +27,8 @@ export function splitUsageTokens(usage: UsageGroup): {
2627
};
2728
}
2829

29-
export function formatModelsList(models: Record<string, { totalTokens: number }>): string[] {
30-
return sort(Object.keys(models))
31-
.asc(model => model);
30+
export function formatModelsList(models: Record<string, { totalTokens: number; isFallback?: boolean }>): string[] {
31+
return sort(Object.entries(models))
32+
.asc(([model]) => model)
33+
.map(([model, data]) => (data.isFallback === true ? `${model} (fallback)` : model));
3234
}

‎apps/codex/src/daily-report.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DailyReportRow, DailyUsageSummary, ModelPricing, PricingSource, TokenUsageDelta, TokenUsageEvent } from './_types.ts';
1+
import type { DailyReportRow, DailyUsageSummary, ModelPricing, ModelUsage, PricingSource, TokenUsageEvent } from './_types.ts';
22
import { formatDisplayDate, isWithinRange, toDateKey } from './date-utils.ts';
33
import { addUsage, calculateCostUSD, createEmptyUsage } from './token-utils.ts';
44

@@ -53,11 +53,14 @@ export async function buildDailyReport(
5353
}
5454

5555
addUsage(summary, event);
56-
const modelUsage = summary.models.get(modelName) ?? createEmptyUsage();
56+
const modelUsage: ModelUsage = summary.models.get(modelName) ?? { ...createEmptyUsage(), isFallback: false };
5757
if (!summary.models.has(modelName)) {
5858
summary.models.set(modelName, modelUsage);
5959
}
6060
addUsage(modelUsage, event);
61+
if (event.isFallbackModel === true) {
62+
modelUsage.isFallback = true;
63+
}
6164
}
6265

6366
const uniqueModels = new Set<string>();
@@ -86,7 +89,7 @@ export async function buildDailyReport(
8689
}
8790
summary.costUSD = cost;
8891

89-
const rowModels: Record<string, TokenUsageDelta> = {};
92+
const rowModels: Record<string, ModelUsage> = {};
9093
for (const [modelName, usage] of summary.models) {
9194
rowModels[modelName] = { ...usage };
9295
}

‎apps/codex/src/data-loader.ts‎

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ function normalizeRawUsage(value: unknown): RawUsage | null {
3838
cached_input_tokens: cached,
3939
output_tokens: output,
4040
reasoning_output_tokens: reasoning,
41-
total_tokens: total > 0 ? total : input + output + reasoning,
41+
// LiteLLM pricing treats reasoning tokens as part of the normal output price. Codex
42+
// includes them as a separate field but does not add them to total_tokens, so when we
43+
// have to synthesize a total (legacy logs), we mirror that behaviour with input+output.
44+
total_tokens: total > 0 ? total : input + output,
4245
};
4346
}
4447

@@ -55,7 +58,7 @@ function subtractRawUsage(current: RawUsage, previous: RawUsage | null): RawUsag
5558
function convertToDelta(raw: RawUsage): TokenUsageDelta {
5659
const total = raw.total_tokens > 0
5760
? raw.total_tokens
58-
: raw.input_tokens + raw.output_tokens + raw.reasoning_output_tokens;
61+
: raw.input_tokens + raw.output_tokens;
5962

6063
const cached = Math.min(raw.cached_input_tokens, raw.input_tokens);
6164

@@ -69,6 +72,7 @@ function convertToDelta(raw: RawUsage): TokenUsageDelta {
6972
}
7073

7174
const recordSchema = v.record(v.string(), v.unknown());
75+
const LEGACY_FALLBACK_MODEL = 'gpt-5';
7276

7377
const entrySchema = v.object({
7478
type: v.string(),
@@ -200,6 +204,8 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
200204

201205
let previousTotals: RawUsage | null = null;
202206
let currentModel: string | undefined;
207+
let currentModelIsFallback = false;
208+
let legacyFallbackUsed = false;
203209
const lines = fileContentResult.value.split(/\r?\n/);
204210
for (const line of lines) {
205211
const trimmed = line.trim();
@@ -230,6 +236,7 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
230236
const contextModel = extractModel(contextPayload.output);
231237
if (contextModel != null) {
232238
currentModel = contextModel;
239+
currentModelIsFallback = false;
233240
}
234241
}
235242
continue;
@@ -279,21 +286,46 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
279286
const extractionSource = payloadRecordResult.success
280287
? Object.assign({}, payloadRecordResult.output, { info })
281288
: { info };
282-
const model = extractModel(extractionSource) ?? currentModel;
289+
const extractedModel = extractModel(extractionSource);
290+
let isFallbackModel = false;
291+
if (extractedModel != null) {
292+
currentModel = extractedModel;
293+
currentModelIsFallback = false;
294+
}
295+
296+
let model = extractedModel ?? currentModel;
283297
if (model == null) {
284-
logger.debug('Skipping Codex token event without model metadata', { file, timestamp });
285-
continue;
298+
model = LEGACY_FALLBACK_MODEL;
299+
isFallbackModel = true;
300+
legacyFallbackUsed = true;
301+
currentModel = model;
302+
currentModelIsFallback = true;
303+
}
304+
else if (extractedModel == null && currentModelIsFallback) {
305+
isFallbackModel = true;
286306
}
287307

288-
events.push({
308+
const event: TokenUsageEvent = {
289309
timestamp,
290310
model,
291311
inputTokens: delta.inputTokens,
292312
cachedInputTokens: delta.cachedInputTokens,
293313
outputTokens: delta.outputTokens,
294314
reasoningOutputTokens: delta.reasoningOutputTokens,
295315
totalTokens: delta.totalTokens,
296-
});
316+
};
317+
318+
if (isFallbackModel) {
319+
// Surface the fallback so both table + JSON outputs can annotate pricing that was
320+
// inferred rather than sourced from the log metadata.
321+
event.isFallbackModel = true;
322+
}
323+
324+
events.push(event);
325+
}
326+
327+
if (legacyFallbackUsed) {
328+
logger.debug('Legacy Codex session lacked model metadata; applied fallback', { file, model: LEGACY_FALLBACK_MODEL });
297329
}
298330
}
299331
}
@@ -384,5 +416,37 @@ if (import.meta.vitest != null) {
384416
expect(second.inputTokens).toBe(800);
385417
expect(second.cachedInputTokens).toBe(100);
386418
});
419+
420+
it('falls back to legacy model when metadata is missing entirely', async () => {
421+
await using fixture = await createFixture({
422+
sessions: {
423+
'legacy.jsonl': [
424+
JSON.stringify({
425+
timestamp: '2025-09-15T13:00:00.000Z',
426+
type: 'event_msg',
427+
payload: {
428+
type: 'token_count',
429+
info: {
430+
total_token_usage: {
431+
input_tokens: 5_000,
432+
cached_input_tokens: 0,
433+
output_tokens: 1_000,
434+
reasoning_output_tokens: 0,
435+
total_tokens: 6_000,
436+
},
437+
},
438+
},
439+
}),
440+
].join('\n'),
441+
},
442+
});
443+
444+
const { events } = await loadTokenUsageEvents({
445+
sessionDirs: [fixture.getPath('sessions')],
446+
});
447+
expect(events).toHaveLength(1);
448+
expect(events[0]!.model).toBe('gpt-5');
449+
expect(events[0]!.isFallbackModel).toBe(true);
450+
});
387451
});
388452
}

‎apps/codex/src/monthly-report.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type {
22
ModelPricing,
3+
ModelUsage,
34
MonthlyReportRow,
45
MonthlyUsageSummary,
56
PricingSource,
6-
TokenUsageDelta,
77
TokenUsageEvent,
88
} from './_types.ts';
99
import { formatDisplayMonth, isWithinRange, toDateKey, toMonthKey } from './date-utils.ts';
@@ -61,11 +61,14 @@ export async function buildMonthlyReport(
6161
}
6262

6363
addUsage(summary, event);
64-
const modelUsage = summary.models.get(modelName) ?? createEmptyUsage();
64+
const modelUsage: ModelUsage = summary.models.get(modelName) ?? { ...createEmptyUsage(), isFallback: false };
6565
if (!summary.models.has(modelName)) {
6666
summary.models.set(modelName, modelUsage);
6767
}
6868
addUsage(modelUsage, event);
69+
if (event.isFallbackModel === true) {
70+
modelUsage.isFallback = true;
71+
}
6972
}
7073

7174
const uniqueModels = new Set<string>();
@@ -94,7 +97,7 @@ export async function buildMonthlyReport(
9497
}
9598
summary.costUSD = cost;
9699

97-
const rowModels: Record<string, TokenUsageDelta> = {};
100+
const rowModels: Record<string, ModelUsage> = {};
98101
for (const [modelName, usage] of summary.models) {
99102
rowModels[modelName] = { ...usage };
100103
}

‎docs/guide/codex/index.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ The CLI reads Codex session JSONL files located under `CODEX_HOME` (defaults to
2525
- **Token deltas** – Each `event_msg` with `payload.type === "token_count"` reports cumulative totals. The CLI subtracts the previous totals to recover per-turn token usage (input, cached input, output, reasoning, total).
2626
- **Per-model grouping** – The `turn_context` metadata specifies the active model. We aggregate tokens per day/month and per model. Sessions lacking model metadata (seen in early September 2025 builds) are skipped.
2727
- **Pricing** – Rates come from LiteLLM's pricing dataset via the shared `LiteLLMPricingFetcher`. Aliases such as `gpt-5-codex` map to canonical entries (`gpt-5`) so cost calculations remain accurate.
28-
- **Cost formula** – Non-cached input uses the standard input price; cached input uses the cache read price (falling back to input when missing); output and reasoning tokens use the output price. All prices are per million tokens.
28+
- **Legacy fallback** – Early September 2025 logs that never recorded `turn_context` metadata are still included; the CLI assumes `gpt-5` for pricing so you can review the tokens even though the model tag is missing (the JSON output also marks these rows with `"isFallback": true`).
29+
- **Cost formula** – Non-cached input uses the standard input price; cached input uses the cache-read price (falling back to the input price when missing); and output tokens are billed at the output price. All prices are per million tokens. Reasoning tokens may be shown for reference, but they are part of the output charge and are not billed separately.
2930
- **Totals and reports** – Daily and monthly commands display per-model breakdowns, overall totals, and optional JSON for automation.
3031

3132
## Environment Variables

0 commit comments

Comments
 (0)