Prefer Codex last token usage for cost scanning#968
Conversation
c781c3d to
ea940ed
Compare
There was a problem hiding this comment.
💡 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".
| 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 | ||
| } |
There was a problem hiding this comment.
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 👍 / 👎.
ea940ed to
6d5945e
Compare
There was a problem hiding this comment.
💡 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".
| snapshots.append(CodexTimestampedTotals( | ||
| timestamp: timestamp, | ||
| date: parsedSnapshotDate(timestamp: timestamp), | ||
| totals: next)) |
There was a problem hiding this comment.
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 👍 / 👎.
Summary
last_token_usagewhen Codex token-count events include both last and total usageContext
I hit a long-running Codex session where
token_countevents carried bothtotal_token_usageandlast_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. Thelast_token_usagevalues 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_usageincrements.Verification
swift test --filter CostUsageScannerBreakdownTestsswift testmake checkgit diff --check