Skip to content

Commit 705c85f

Browse files
MaxGheniscodexclaude
committed
fix(codex): correct legacy token total fallback
Prepared with AI assistance (audit + implementation: gpt-5.6-sol; review: Claude Fable 5); reviewed and tested before filing. Co-Authored-By: gpt-5.6-sol <[email protected]> Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 7acee6c commit 705c85f

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

  • rust/crates/ccusage/src/adapter/codex

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,11 @@ impl<'de> Deserialize<'de> for CodexRawUsage {
190190
.unwrap_or(0),
191191
output_tokens: output,
192192
reasoning_output_tokens: reasoning,
193-
total_tokens: fields
194-
.total_tokens
195-
.filter(|total| *total > 0 || input + output + reasoning == 0)
196-
.unwrap_or(input + output + reasoning),
193+
total_tokens: match fields.total_tokens {
194+
None => input + output,
195+
Some(0) => input + output + reasoning,
196+
Some(total) => total,
197+
},
197198
})
198199
}
199200
}
@@ -380,3 +381,37 @@ where
380381

381382
deserializer.deserialize_any(OptionalU64Visitor)
382383
}
384+
385+
#[cfg(test)]
386+
mod tests {
387+
use super::*;
388+
389+
#[test]
390+
fn derives_total_tokens_without_double_counting_reasoning() {
391+
let usage: CodexRawUsage = serde_json::from_str(
392+
r#"{
393+
"input_tokens": 100,
394+
"output_tokens": 50,
395+
"reasoning_output_tokens": 20
396+
}"#,
397+
)
398+
.expect("usage should deserialize");
399+
400+
assert_eq!(usage.total_tokens, 150);
401+
}
402+
403+
#[test]
404+
fn derives_explicit_zero_total_tokens_from_all_nonzero_components() {
405+
let usage: CodexRawUsage = serde_json::from_str(
406+
r#"{
407+
"input_tokens": 9,
408+
"output_tokens": 4,
409+
"reasoning_output_tokens": 1,
410+
"total_tokens": 0
411+
}"#,
412+
)
413+
.expect("usage should deserialize");
414+
415+
assert_eq!(usage.total_tokens, 14);
416+
}
417+
}

0 commit comments

Comments
 (0)