Skip to content

Skip a Codex token_count event whose running total did not move - #10607

Closed
st-eez wants to merge 1 commit into
omacom:quattrofrom
st-eez:codex-duplicate-turn
Closed

st-eez wants to merge 1 commit into
omacom:quattrofrom
st-eez:codex-duplicate-turn

Conversation

@st-eez

@st-eez st-eez commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

scan_native_codex_sessions adds last_token_usage from every token_count
event. That reproduces the session's own total_token_usage, except that some
turns are logged twice with identical usage and an unchanged running total, and
the scanner counts both.

Measured on 887 session files: 232 duplicate events among 12,675 with usage,
21,129,968 tokens (1.8%) and 181 extra prompts. In the 831 sessions whose
running total never resets, the corrected sum equals the final cumulative
total exactly. The uncorrected sum is 1.6% over.

The fix skips an event whose total_token_usage.total_tokens equals the
previous event's in the same file. A drop can indicate a resumed or compacted
session and still counts. The first event of a file has no previous total, so
the check never skips it.

Verification

Two new cases in test/shell.d/agent-usage-codex-scanner-test.sh:

  • a session with a turn logged twice, same last_token_usage and same
    total_token_usage, counts that turn once (fails on quattro)
  • a session whose running total drops after a resume counts the turn that
    follows the drop (passes on quattro and guards the fix)

All nine resets in the measured history remain counted. None of the skipped
events carried usage the running total had not already absorbed.

Each Codex turn writes one token_count event with that turn's
last_token_usage and the session's cumulative total_token_usage. Summing
last_token_usage across a session reproduces its final cumulative total,
except that some turns are written twice with identical last_token_usage
and an unchanged cumulative total. The scanner added both.

On one machine that was 232 duplicate events among 12,675, 21.1M tokens
or 1.8% of Codex usage, and 181 extra prompts. Across the 831 sessions
whose totals never reset, skipping them makes the summed turns equal the
final cumulative total exactly.

Skip an event whose total_token_usage.total_tokens equals the previous
event's in the same file. A drop in the total can indicate a resumed or
compacted session and still counts. The first event of a file has no
previous total, so this check never skips it.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: GPT-6 Astra <noreply@openai.com>
@st-eez
st-eez force-pushed the codex-duplicate-turn branch from cc20767 to 58496b9 Compare September 7, 2026 06:21
@bjarneo

bjarneo commented Sep 21, 2026

Copy link
Copy Markdown
Member

Automated duplication check: this pull request looks similar to #10531, which covers the same Codex token snapshot skip. I keep that one open and close this one to consolidate review.

If you feel this is the wrong decision, please open the PR again with a note on the difference.

@bjarneo bjarneo closed this Sep 21, 2026
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