Skip to content

Prefer Codex last token usage for cost scanning#968

Merged
steipete merged 3 commits into
steipete:mainfrom
Ifan24:codex/fix-codex-cost-last-token-usage
May 16, 2026
Merged

Prefer Codex last token usage for cost scanning#968
steipete merged 3 commits into
steipete:mainfrom
Ifan24:codex/fix-codex-cost-last-token-usage

Conversation

@Ifan24
Copy link
Copy Markdown
Contributor

@Ifan24 Ifan24 commented May 16, 2026

Summary

  • prefer last_token_usage when Codex token-count events include both last and total usage
  • keep incremental baselines synced to the counted usage instead of persisting divergent raw totals
  • disable Codex incremental cache reuse when raw totals remain divergent, and bump the Codex cost cache artifact to rescan old cached files
  • add regressions for mixed last/total streams, total-only continuation after divergence, last-only to total-only fallback, and forked parent inheritance

Context

I hit a long-running Codex session where token_count events carried both total_token_usage and last_token_usage, but the total counters could jump/diverge across the session stream. Diffing the totals as one monotonic counter inflated the local cost estimate dramatically. The last_token_usage values appear to be the stable incremental values for these events.

For the local repro log, the old total-diff accounting produced about 32.8B tokens on the spike day. The patched scanner reports 998,214,939 tokens for that same day, matching the sum of last_token_usage increments.

Verification

  • swift test --filter CostUsageScannerBreakdownTests
  • swift test
  • make check
  • git diff --check
  • local-only replay against the real spike JSONL with the patched scanner; not committed because it references a private session log

@Ifan24 Ifan24 force-pushed the codex/fix-codex-cost-last-token-usage branch from c781c3d to ea940ed Compare May 16, 2026 00:45
@Ifan24 Ifan24 marked this pull request as ready for review May 16, 2026 00:50
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ea940ed544

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +846 to +853
previousTotals = if let inheritedTotals {
CostUsageCodexTotals(
input: max(0, rawTotals.input - inheritedTotals.input),
cached: max(0, rawTotals.cached - inheritedTotals.cached),
output: max(0, rawTotals.output - inheritedTotals.output))
} else {
rawTotals
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the incremental baseline in sync with counted usage

When a session has both fields and total_token_usage has already diverged, this branch now charges from last_token_usage but still persists the raw total as previousTotals/lastTotals. If the same file later gets appended with a token_count that only has total_token_usage (a format this parser still supports), incremental parsing resumes from the inflated raw baseline and max(0, currentTotals - prev) drops the new usage until the raw total catches up. In that mixed-format scenario, use a last-based running baseline or avoid incremental reuse after seeing divergent totals.

Useful? React with 👍 / 👎.

@Ifan24 Ifan24 force-pushed the codex/fix-codex-cost-last-token-usage branch from ea940ed to 6d5945e Compare May 16, 2026 01:12
@steipete steipete merged commit 60f4a4e into steipete:main May 16, 2026
1 check passed
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f8977bcd11

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 696 to 699
snapshots.append(CodexTimestampedTotals(
timestamp: timestamp,
date: parsedSnapshotDate(timestamp: timestamp),
totals: next))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve raw parent totals in fork snapshot baseline

When parent token_count events include both last_token_usage and a divergent total_token_usage, this snapshot path now stores totals: next (the last-based counted total). Fork inheritance later subtracts these snapshots from child total_token_usage, so if the child continues the raw parent counter (as in existing total-only fork flows), the child delta is overstated by the parent raw/count difference (e.g. parent raw 1000 vs counted 140 makes a child total of 1050 look like +910). This can create large false spikes in forked child usage.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants