Skip to content

Commit 38fa236

Browse files
committed
fix(codex): date fallback review model pricing
Codex can emit the internal codex-auto-review label for review runs, but pricing that label as gpt-5.5 for every historical row overstates older usage. Resolve that label while parsing Codex logs, using a small release-date table from models.dev so each event falls back to the newest known Codex/OpenAI model available on the log date without extra I/O in the hot path. Remove the undated pricing alias for codex-auto-review, keep the raw alias handling for gpt-5.3-spark, and document the date-aware fallback behavior in the Codex guide.
1 parent e455f34 commit 38fa236

4 files changed

Lines changed: 191 additions & 47 deletions

File tree

‎docs/guide/codex/index.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ These views support `--json`, `--compact`, `--offline`, and `--speed auto|standa
4747

4848
- **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).
4949
- **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.
50-
- **Pricing** – Rates come from LiteLLM's pricing dataset via the shared `LiteLLMPricingFetcher`. Model aliases such as `gpt-5.5` are resolved through the pricing cache when available.
50+
- **Pricing** – Rates come from LiteLLM's pricing dataset via the shared `LiteLLMPricingFetcher`. Codex's internal review label is resolved to the newest known model for the log date before pricing is calculated.
5151
- **Speed pricing** – `--speed auto` is the default. It reads `config.toml` from each `CODEX_HOME` root and applies fast pricing when any Codex config has `service_tier = "priority"` or legacy `service_tier = "fast"` configured. Fast mode uses the model-specific LiteLLM multiplier when available and otherwise falls back to 2x pricing. Pass `--speed fast` or `--speed standard` to override config-based detection.
5252
- **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`).
5353
- **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.
@@ -60,7 +60,7 @@ These views support `--json`, `--compact`, `--offline`, and `--speed auto|standa
6060
| `CODEX_HOME` | Override the root directory, or comma-separated directories, containing Codex homes or saved `codex exec --json` JSONL files |
6161
| `LOG_LEVEL` | Adjust log verbosity (0 silent … 5 trace) |
6262

63-
When Codex emits a model alias (for example `gpt-5.5`), the CLI automatically resolves it through the LiteLLM pricing data when possible. No manual override is needed.
63+
When Codex emits a model alias, the CLI automatically resolves it through the LiteLLM pricing data when possible. For `codex-auto-review`, the CLI uses the newest known Codex/OpenAI model available on the log date. No manual override is needed.
6464

6565
## Speed Pricing
6666

‎rust/crates/ccusage/src/adapter/codex/loader.rs‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,110 @@ mod tests {
530530
assert_eq!(events[0].total_tokens, 150);
531531
}
532532

533+
#[test]
534+
fn resolves_codex_auto_review_to_latest_model_for_event_date() {
535+
let fixture = fs_fixture!({
536+
"run.jsonl": [
537+
json!({
538+
"type": "turn.completed",
539+
"timestamp": "2026-02-05T00:00:00.000Z",
540+
"model": "codex-auto-review",
541+
"usage": {
542+
"input_tokens": 10,
543+
"output_tokens": 5,
544+
"total_tokens": 15,
545+
},
546+
})
547+
.to_string(),
548+
json!({
549+
"type": "turn.completed",
550+
"timestamp": "2026-03-05T00:00:00.000Z",
551+
"model": "codex-auto-review",
552+
"usage": {
553+
"input_tokens": 20,
554+
"output_tokens": 10,
555+
"total_tokens": 30,
556+
},
557+
})
558+
.to_string(),
559+
json!({
560+
"type": "turn.completed",
561+
"timestamp": "2026-04-23T00:00:00.000Z",
562+
"model": "codex-auto-review",
563+
"usage": {
564+
"input_tokens": 30,
565+
"output_tokens": 15,
566+
"total_tokens": 45,
567+
},
568+
})
569+
.to_string(),
570+
]
571+
.join("\n"),
572+
});
573+
574+
let events = load_codex_events_from_directory(fixture.root(), true).unwrap();
575+
576+
assert_eq!(events.len(), 3);
577+
assert_eq!(events[0].model.as_deref(), Some("gpt-5.3-codex"));
578+
assert_eq!(events[1].model.as_deref(), Some("gpt-5.4"));
579+
assert_eq!(events[2].model.as_deref(), Some("gpt-5.5"));
580+
assert!(events.iter().all(|event| event.is_fallback_model));
581+
}
582+
583+
#[test]
584+
fn resolves_codex_auto_review_turn_context_for_each_event_date() {
585+
let fixture = fs_fixture!({
586+
"session.jsonl": [
587+
json!({
588+
"timestamp": "2025-12-11T00:00:00.000Z",
589+
"type": "turn_context",
590+
"payload": {
591+
"model": "codex-auto-review",
592+
},
593+
})
594+
.to_string(),
595+
json!({
596+
"timestamp": "2025-12-11T00:01:00.000Z",
597+
"type": "event_msg",
598+
"payload": {
599+
"type": "token_count",
600+
"info": {
601+
"last_token_usage": {
602+
"input_tokens": 10,
603+
"output_tokens": 5,
604+
"total_tokens": 15,
605+
},
606+
},
607+
},
608+
})
609+
.to_string(),
610+
json!({
611+
"timestamp": "2026-04-23T00:01:00.000Z",
612+
"type": "event_msg",
613+
"payload": {
614+
"type": "token_count",
615+
"info": {
616+
"last_token_usage": {
617+
"input_tokens": 20,
618+
"output_tokens": 10,
619+
"total_tokens": 30,
620+
},
621+
},
622+
},
623+
})
624+
.to_string(),
625+
]
626+
.join("\n"),
627+
});
628+
629+
let events = load_codex_events_from_directory(fixture.root(), true).unwrap();
630+
631+
assert_eq!(events.len(), 2);
632+
assert_eq!(events[0].model.as_deref(), Some("gpt-5.2-codex"));
633+
assert_eq!(events[1].model.as_deref(), Some("gpt-5.5"));
634+
assert!(events.iter().all(|event| event.is_fallback_model));
635+
}
636+
533637
#[test]
534638
fn loads_headless_usage_with_token_count_text_content() {
535639
let fixture = fs_fixture!({

‎rust/crates/ccusage/src/adapter/codex/parser.rs‎

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ static PROMPT_TOKENS_FIELD_FINDER: LazyLock<Finder<'static>> =
3434
static THREAD_SPAWN_FINDER: LazyLock<Finder<'static>> =
3535
LazyLock::new(|| Finder::new(b"thread_spawn"));
3636

37+
const CODEX_AUTO_REVIEW_MODEL: &str = "codex-auto-review";
38+
const CODEX_AUTO_REVIEW_FALLBACK_MODELS: [(&str, &str); 7] = [
39+
("2026-04-23", "gpt-5.5"),
40+
("2026-03-05", "gpt-5.4"),
41+
("2026-02-05", "gpt-5.3-codex"),
42+
("2025-12-11", "gpt-5.2-codex"),
43+
("2025-11-13", "gpt-5.1-codex"),
44+
("2025-09-15", "gpt-5-codex"),
45+
("2025-08-07", "gpt-5"),
46+
];
47+
3748
#[derive(Clone, Copy)]
3849
enum CodexLineKind {
3950
Session,
@@ -267,20 +278,12 @@ fn visit_codex_session_entry(
267278

268279
let parsed_model =
269280
codex_model_from_payload(payload).or_else(|| info.and_then(codex_model_from_info));
270-
if let Some(model) = parsed_model.clone() {
271-
*current_model = Some(model);
272-
*current_model_is_fallback = false;
273-
}
274-
let mut is_fallback_model = false;
275-
let model = parsed_model.or_else(|| current_model.clone()).or_else(|| {
276-
is_fallback_model = true;
277-
*current_model_is_fallback = true;
278-
*current_model = Some("gpt-5".to_string());
279-
current_model.clone()
280-
});
281-
if parsed_model_is_missing(&model, current_model, *current_model_is_fallback) {
282-
is_fallback_model = true;
283-
}
281+
let (model, is_fallback_model) = resolve_codex_usage_model(
282+
parsed_model,
283+
&timestamp,
284+
current_model,
285+
current_model_is_fallback,
286+
);
284287

285288
visit(CodexTokenUsageEvent {
286289
session_id: session_id.to_string(),
@@ -357,20 +360,12 @@ fn visit_codex_exec_usage_event(
357360
current_model_is_fallback: &mut bool,
358361
visit: &mut impl FnMut(CodexTokenUsageEvent) -> Result<()>,
359362
) -> Result<()> {
360-
if let Some(model) = parsed_model.clone() {
361-
*current_model = Some(model);
362-
*current_model_is_fallback = false;
363-
}
364-
let mut is_fallback_model = false;
365-
let model = parsed_model.or_else(|| current_model.clone()).or_else(|| {
366-
is_fallback_model = true;
367-
*current_model_is_fallback = true;
368-
*current_model = Some("gpt-5".to_string());
369-
current_model.clone()
370-
});
371-
if parsed_model_is_missing(&model, current_model, *current_model_is_fallback) {
372-
is_fallback_model = true;
373-
}
363+
let (model, is_fallback_model) = resolve_codex_usage_model(
364+
parsed_model,
365+
&timestamp,
366+
current_model,
367+
current_model_is_fallback,
368+
);
374369
visit(CodexTokenUsageEvent {
375370
session_id: session_id.to_string(),
376371
timestamp,
@@ -470,6 +465,64 @@ fn parsed_model_is_missing(
470465
model.is_some() && current_model.is_some() && current_model_is_fallback
471466
}
472467

468+
fn resolve_codex_usage_model(
469+
parsed_model: Option<String>,
470+
timestamp: &str,
471+
current_model: &mut Option<String>,
472+
current_model_is_fallback: &mut bool,
473+
) -> (Option<String>, bool) {
474+
if let Some(model) = parsed_model.as_ref() {
475+
*current_model = Some(model.clone());
476+
*current_model_is_fallback = false;
477+
}
478+
let mut is_fallback_model = false;
479+
let model = parsed_model.or_else(|| current_model.clone()).or_else(|| {
480+
is_fallback_model = true;
481+
*current_model_is_fallback = true;
482+
*current_model = Some("gpt-5".to_string());
483+
current_model.clone()
484+
});
485+
if parsed_model_is_missing(&model, current_model, *current_model_is_fallback) {
486+
is_fallback_model = true;
487+
}
488+
let model = model.map(|model| {
489+
codex_log_model_fallback(&model, timestamp)
490+
.map(|fallback| {
491+
is_fallback_model = true;
492+
fallback.to_string()
493+
})
494+
.unwrap_or(model)
495+
});
496+
(model, is_fallback_model)
497+
}
498+
499+
fn codex_log_model_fallback(model: &str, timestamp: &str) -> Option<&'static str> {
500+
if model != CODEX_AUTO_REVIEW_MODEL {
501+
return None;
502+
}
503+
let Some(date) = codex_timestamp_date(timestamp) else {
504+
return Some("gpt-5.5");
505+
};
506+
Some(
507+
CODEX_AUTO_REVIEW_FALLBACK_MODELS
508+
.iter()
509+
.find_map(|(released_on, fallback)| (date >= *released_on).then_some(*fallback))
510+
.unwrap_or("gpt-5"),
511+
)
512+
}
513+
514+
fn codex_timestamp_date(timestamp: &str) -> Option<&str> {
515+
let date = timestamp.get(..10)?;
516+
let bytes = date.as_bytes();
517+
(bytes.len() == 10
518+
&& bytes[0..4].iter().all(u8::is_ascii_digit)
519+
&& bytes[4] == b'-'
520+
&& bytes[5..7].iter().all(u8::is_ascii_digit)
521+
&& bytes[7] == b'-'
522+
&& bytes[8..10].iter().all(u8::is_ascii_digit))
523+
.then_some(date)
524+
}
525+
473526
fn codex_session_id(sessions_dir: &Path, path: &Path) -> String {
474527
let relative = path.strip_prefix(sessions_dir).unwrap_or(path);
475528
let mut session_id = relative

‎rust/crates/ccusage/src/pricing.rs‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,6 @@ fn normalized_pricing_key(value: &str) -> Cow<'_, str> {
11351135
/// canonical pricing keys.
11361136
fn pricing_alias(model: &str) -> Option<&'static str> {
11371137
match model {
1138-
"codex-auto-review" => Some("gpt-5.5"),
11391138
"gpt-5.3-spark" => Some("gpt-5.3-codex-spark"),
11401139
_ => None,
11411140
}
@@ -1796,23 +1795,11 @@ mod tests {
17961795
}
17971796

17981797
#[test]
1799-
fn embedded_pricing_resolves_codex_auto_review_model() {
1798+
fn embedded_pricing_does_not_resolve_undated_codex_auto_review_model() {
18001799
let pricing = PricingMap::load_embedded();
1801-
let auto_review = pricing
1802-
.find("codex-auto-review")
1803-
.expect("codex-auto-review should resolve via model alias");
1804-
let latest_codex = pricing
1805-
.find("gpt-5.5")
1806-
.expect("canonical latest Codex pricing should exist");
1807-
1808-
assert_eq!(auto_review.input, latest_codex.input);
1809-
assert_eq!(auto_review.output, latest_codex.output);
1810-
assert_eq!(auto_review.cache_read, latest_codex.cache_read);
1811-
assert_eq!(auto_review.fast_multiplier, latest_codex.fast_multiplier);
1812-
assert_eq!(
1813-
pricing.context_limit("codex-auto-review"),
1814-
pricing.context_limit("gpt-5.5")
1815-
);
1800+
1801+
assert!(pricing.find("codex-auto-review").is_none());
1802+
assert!(pricing.context_limit("codex-auto-review").is_none());
18161803
}
18171804

18181805
#[test]

0 commit comments

Comments
 (0)