Skip to content

Commit 2faec24

Browse files
committed
codex: drop default model fallback
1 parent e244f2d commit 2faec24

9 files changed

Lines changed: 30 additions & 68 deletions

File tree

‎apps/codex/src/_consts.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export const CODEX_HOME_ENV = 'CODEX_HOME';
55
export const DEFAULT_CODEX_DIR = path.join(os.homedir(), '.codex');
66
export const DEFAULT_SESSION_SUBDIR = 'sessions';
77
export const SESSION_GLOB = '**/*.jsonl';
8-
export const MODEL_ENV_VAR = 'CODEX_USAGE_MODEL';
9-
export const DEFAULT_MODEL = 'gpt-5';
108
export const DEFAULT_TIMEZONE = Intl.DateTimeFormat().resolvedOptions().timeZone ?? 'UTC';
119
export const DEFAULT_LOCALE = 'en-CA';
1210
export const DEFAULT_PRECISION = 2;

‎apps/codex/src/_shared-args.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Args } from 'gunshi';
2-
import { DEFAULT_LOCALE, DEFAULT_MODEL, DEFAULT_TIMEZONE } from './_consts.ts';
2+
import { DEFAULT_LOCALE, DEFAULT_TIMEZONE } from './_consts.ts';
33

44
export const sharedArgs = {
55
json: {
@@ -30,12 +30,6 @@ export const sharedArgs = {
3030
description: 'Locale for formatting',
3131
default: DEFAULT_LOCALE,
3232
},
33-
model: {
34-
type: 'string',
35-
short: 'm',
36-
description: `Default model name when Codex log lacks model metadata (defaults to ${DEFAULT_MODEL}, or CODEX_USAGE_MODEL if set)`,
37-
default: DEFAULT_MODEL,
38-
},
3933
offline: {
4034
type: 'boolean',
4135
short: 'O',

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ export function splitUsageTokens(usage: UsageGroup): {
2626
};
2727
}
2828

29-
export function isOptionExplicit(tokens: ReadonlyArray<unknown>, optionName: string): boolean {
30-
for (const token of tokens) {
31-
if (typeof token === 'object' && token != null) {
32-
const candidate = token as { kind?: string; name?: string };
33-
if (candidate.kind === 'option' && candidate.name === optionName) {
34-
return true;
35-
}
36-
}
37-
}
38-
return false;
39-
}
40-
4129
export function formatModelsList(models: Record<string, { totalTokens: number }>): string[] {
4230
return sort(Object.keys(models))
4331
.asc(model => model);

‎apps/codex/src/commands/daily.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
} from '@ccusage/terminal/table';
99
import { define } from 'gunshi';
1010
import pc from 'picocolors';
11-
import { DEFAULT_MODEL, DEFAULT_TIMEZONE, MODEL_ENV_VAR } from '../_consts.ts';
11+
import { DEFAULT_TIMEZONE } from '../_consts.ts';
1212
import { sharedArgs } from '../_shared-args.ts';
13-
import { formatModelsList, isOptionExplicit, splitUsageTokens } from '../command-utils.ts';
13+
import { formatModelsList, splitUsageTokens } from '../command-utils.ts';
1414
import { buildDailyReport } from '../daily-report.ts';
1515
import { loadTokenUsageEvents } from '../data-loader.ts';
1616
import { normalizeFilterDate } from '../date-utils.ts';
@@ -41,16 +41,7 @@ export const dailyCommand = define({
4141
process.exit(1);
4242
}
4343

44-
const modelFromEnv = process.env[MODEL_ENV_VAR];
45-
const modelProvidedViaCli = isOptionExplicit(ctx.tokens, 'model');
46-
let defaultModel = ctx.values.model ?? DEFAULT_MODEL;
47-
if (!modelProvidedViaCli && modelFromEnv != null && modelFromEnv !== '') {
48-
defaultModel = modelFromEnv;
49-
}
50-
51-
const { events, missingDirectories } = await loadTokenUsageEvents({
52-
defaultModel,
53-
});
44+
const { events, missingDirectories } = await loadTokenUsageEvents();
5445

5546
for (const missing of missingDirectories) {
5647
logger.warn(`Codex session directory not found: ${missing}`);
@@ -69,7 +60,6 @@ export const dailyCommand = define({
6960
pricingSource,
7061
timezone: ctx.values.timezone,
7162
locale: ctx.values.locale,
72-
defaultModel,
7363
since,
7464
until,
7565
});

‎apps/codex/src/commands/monthly.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
} from '@ccusage/terminal/table';
99
import { define } from 'gunshi';
1010
import pc from 'picocolors';
11-
import { DEFAULT_MODEL, DEFAULT_TIMEZONE, MODEL_ENV_VAR } from '../_consts.ts';
11+
import { DEFAULT_TIMEZONE } from '../_consts.ts';
1212
import { sharedArgs } from '../_shared-args.ts';
13-
import { formatModelsList, isOptionExplicit, splitUsageTokens } from '../command-utils.ts';
13+
import { formatModelsList, splitUsageTokens } from '../command-utils.ts';
1414
import { loadTokenUsageEvents } from '../data-loader.ts';
1515
import { normalizeFilterDate } from '../date-utils.ts';
1616
import { log, logger } from '../logger.ts';
@@ -41,16 +41,7 @@ export const monthlyCommand = define({
4141
process.exit(1);
4242
}
4343

44-
const modelFromEnv = process.env[MODEL_ENV_VAR];
45-
const modelProvidedViaCli = isOptionExplicit(ctx.tokens, 'model');
46-
let defaultModel = ctx.values.model ?? DEFAULT_MODEL;
47-
if (!modelProvidedViaCli && modelFromEnv != null && modelFromEnv !== '') {
48-
defaultModel = modelFromEnv;
49-
}
50-
51-
const { events, missingDirectories } = await loadTokenUsageEvents({
52-
defaultModel,
53-
});
44+
const { events, missingDirectories } = await loadTokenUsageEvents();
5445

5546
for (const missing of missingDirectories) {
5647
logger.warn(`Codex session directory not found: ${missing}`);
@@ -69,7 +60,6 @@ export const monthlyCommand = define({
6960
pricingSource,
7061
timezone: ctx.values.timezone,
7162
locale: ctx.values.locale,
72-
defaultModel,
7363
since,
7464
until,
7565
});

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type { DailyReportRow, DailyUsageSummary, ModelPricing, PricingSource, TokenUsageDelta, TokenUsageEvent } from './_types.ts';
2-
import { DEFAULT_MODEL } from './_consts.ts';
32
import { formatDisplayDate, isWithinRange, toDateKey } from './date-utils.ts';
43
import { addUsage, calculateCostUSD, createEmptyUsage } from './token-utils.ts';
54

65
export type DailyReportOptions = {
76
timezone?: string;
87
locale?: string;
9-
defaultModel?: string;
108
since?: string;
119
until?: string;
1210
pricingSource: PricingSource;
@@ -32,14 +30,18 @@ export async function buildDailyReport(
3230
): Promise<DailyReportRow[]> {
3331
const timezone = options.timezone;
3432
const locale = options.locale;
35-
const defaultModel = options.defaultModel ?? DEFAULT_MODEL;
3633
const since = options.since;
3734
const until = options.until;
3835
const pricingSource = options.pricingSource;
3936

4037
const summaries = new Map<string, DailyUsageSummary>();
4138

4239
for (const event of events) {
40+
const modelName = event.model?.trim();
41+
if (modelName == null || modelName === '') {
42+
continue;
43+
}
44+
4345
const dateKey = toDateKey(event.timestamp, timezone);
4446
if (!isWithinRange(dateKey, since, until)) {
4547
continue;
@@ -51,8 +53,6 @@ export async function buildDailyReport(
5153
}
5254

5355
addUsage(summary, event);
54-
55-
const modelName = event.model ?? defaultModel;
5656
const modelUsage = summary.models.get(modelName) ?? createEmptyUsage();
5757
if (!summary.models.has(modelName)) {
5858
summary.models.set(modelName, modelUsage);

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import process from 'node:process';
55
import { Result } from '@praha/byethrow';
66
import { createFixture } from 'fs-fixture';
77
import { glob } from 'tinyglobby';
8-
import { CODEX_HOME_ENV, DEFAULT_CODEX_DIR, DEFAULT_MODEL, DEFAULT_SESSION_SUBDIR, SESSION_GLOB } from './_consts.ts';
8+
import { CODEX_HOME_ENV, DEFAULT_CODEX_DIR, DEFAULT_SESSION_SUBDIR, SESSION_GLOB } from './_consts.ts';
99
import { logger } from './logger.ts';
1010

1111
type RawUsage = {
@@ -95,7 +95,6 @@ function extractModel(value: unknown): string | undefined {
9595

9696
export type LoadOptions = {
9797
sessionDirs?: string[];
98-
defaultModel?: string;
9998
};
10099

101100
export type LoadResult = {
@@ -114,7 +113,6 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
114113
: DEFAULT_CODEX_DIR;
115114
const defaultSessionsDir = path.join(codexHome, DEFAULT_SESSION_SUBDIR);
116115
const sessionDirs = providedDirs ?? [defaultSessionsDir];
117-
const defaultModel = options.defaultModel ?? DEFAULT_MODEL;
118116

119117
const events: TokenUsageEvent[] = [];
120118
const missingDirectories: string[] = [];
@@ -216,7 +214,11 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
216214
continue;
217215
}
218216

219-
const model = extractModel({ ...payload, info }) ?? defaultModel;
217+
const model = extractModel({ ...payload, info });
218+
if (model == null) {
219+
logger.debug('Skipping Codex token event without model metadata', { file, timestamp });
220+
continue;
221+
}
220222

221223
events.push({
222224
timestamp,
@@ -238,7 +240,7 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
238240

239241
if (import.meta.vitest != null) {
240242
describe('loadTokenUsageEvents', () => {
241-
it('parses token_count events and falls back to default model', async () => {
243+
it('parses token_count events and skips entries without model metadata', async () => {
242244
await using fixture = await createFixture({
243245
sessions: {
244246
'project-1.jsonl': [
@@ -290,17 +292,14 @@ if (import.meta.vitest != null) {
290292

291293
const { events, missingDirectories } = await loadTokenUsageEvents({
292294
sessionDirs: [fixture.getPath('sessions')],
293-
defaultModel: 'gpt-5',
294295
});
295296
expect(missingDirectories).toEqual([]);
296297

297-
expect(events).toHaveLength(2);
298+
expect(events).toHaveLength(1);
298299
const first = events[0]!;
299-
const second = events[1]!;
300300
expect(first.model).toBe('gpt-5');
301301
expect(first.inputTokens).toBe(1_200);
302-
expect(second.inputTokens).toBe(800);
303-
expect(second.cachedInputTokens).toBe(100);
302+
expect(first.cachedInputTokens).toBe(200);
304303
});
305304
});
306305
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import type {
66
TokenUsageDelta,
77
TokenUsageEvent,
88
} from './_types.ts';
9-
import { DEFAULT_MODEL } from './_consts.ts';
109
import { formatDisplayMonth, isWithinRange, toDateKey, toMonthKey } from './date-utils.ts';
1110
import { addUsage, calculateCostUSD, createEmptyUsage } from './token-utils.ts';
1211

1312
export type MonthlyReportOptions = {
1413
timezone?: string;
1514
locale?: string;
16-
defaultModel?: string;
1715
since?: string;
1816
until?: string;
1917
pricingSource: PricingSource;
@@ -39,14 +37,18 @@ export async function buildMonthlyReport(
3937
): Promise<MonthlyReportRow[]> {
4038
const timezone = options.timezone;
4139
const locale = options.locale;
42-
const defaultModel = options.defaultModel ?? DEFAULT_MODEL;
4340
const since = options.since;
4441
const until = options.until;
4542
const pricingSource = options.pricingSource;
4643

4744
const summaries = new Map<string, MonthlyUsageSummary>();
4845

4946
for (const event of events) {
47+
const modelName = event.model?.trim();
48+
if (modelName == null || modelName === '') {
49+
continue;
50+
}
51+
5052
const dateKey = toDateKey(event.timestamp, timezone);
5153
if (!isWithinRange(dateKey, since, until)) {
5254
continue;
@@ -59,8 +61,6 @@ export async function buildMonthlyReport(
5961
}
6062

6163
addUsage(summary, event);
62-
63-
const modelName = event.model ?? defaultModel;
6464
const modelUsage = summary.models.get(modelName) ?? createEmptyUsage();
6565
if (!summary.models.has(modelName)) {
6666
summary.models.set(modelName, modelUsage);

‎docs/guide/codex/index.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ The CLI reads Codex session JSONL files located under `CODEX_HOME` (defaults to
2525
| Variable | Description |
2626
| --- | --- |
2727
| `CODEX_HOME` | Override the root directory containing Codex session folders |
28-
| `CODEX_USAGE_MODEL` | Default model when metadata is missing (overridden by `--model`; falls back to `gpt-5` otherwise) |
2928
| `LOG_LEVEL` | Adjust consola verbosity (0 silent … 5 trace) |
3029

3130
## Next Steps
@@ -41,3 +40,7 @@ Have feedback or ideas? [Open an issue](https://github.com/ryoppippi/ccusage/iss
4140
::: details Why are there no entries before September 2025?
4241
OpenAI's Codex CLI started emitting `token_count` events in [commit 0269096](https://github.com/openai/codex/commit/0269096229e8c8bd95185173706807dc10838c7a) (2025-09-06). Earlier session logs simply don't contain token usage metrics, so `@ccusage/codex` has nothing to aggregate. If you need historic data, rerun those sessions after that Codex update.
4342
:::
43+
44+
::: details What if some September 2025 sessions still get skipped?
45+
During the 2025-09 rollouts a few Codex builds emitted `token_count` events without the matching `turn_context` metadata, so the CLI could not determine which model generated the tokens. Those entries are ignored to avoid mispriced reports. If you encounter this, relaunch the Codex CLI to generate fresh logs—the current builds restore the missing metadata.
46+
:::

0 commit comments

Comments
 (0)