fix(codex): ignore duplicate token_count totals #824
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Codex session logs can emit duplicate
token_countevents where the cumulativetotal_token_usagedoes not advance. The loader previously trustedlast_token_usageunconditionally, which causes duplicated deltas and inflates all token totals (often close to x2). This change skipslast_token_usagewhen totals are unchanged, and only derives deltas from totals whenlastis missing.Why this happens
Codex logs contain both:
info.last_token_usage: per-event deltainfo.total_token_usage: cumulative totalsIn practice, some sessions emit the same
token_countpayload twice without advancing the cumulative totals. When we sumlast_token_usagefor every event, those duplicates are counted again even thoughtotal_token_usageshows no progress.Example from real logs (simplified)
Event A (first emission):
Event B (duplicate emission, totals unchanged):
Old behavior:
New behavior:
How the fix works
total_token_usageper sessiontotal_token_usageequals the previous total, treat theevent as a duplicate and ignore
last_token_usagelast_token_usageis missing but totals exist, derive a delta from thetotals (existing behavior)
What changed
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.