Skip to content

codex: cache tokens overcounted ~3.7x & input undercounted on new rollout format (streaming turn_token_usage snapshots) #1380

Closed

Description

@treepudding

Environment

  • codeburn 0.9.24 (npm), macOS 26, Node 22
  • Codex CLI (2026 builds). Rollout jsonl now contains token_usage_record entries whose payload.turn_token_usage is a streaming cumulative snapshot (written multiple times per turn, monotonically increasing).

Symptom

codeburn massively overcounts Codex cache tokens and undercounts input. Real per-day comparison on 2026-09-10:

codeburn export (09-10) exact from local logs
input 2,113,331 17,385,882
output 182,497 47,861
cache read 62,372,608 16,919,680
total 64,668,436 34,353,423

Cache 3.7x too high, input ~88% missing. For the whole month: codeburn shows ~833M codex tokens vs ~136M exact.

Root cause hypothesis

Each turn emits multiple token_usage_record lines; turn_token_usage values are cumulative for the turn/thread, e.g. observed sequence for one turn:
input_tokens: 34,823 → 70,313 → 117,411 → 172,933 → 234,696 (each ~10s apart).
event_msg/token_count with info.last_token_usage / info.total_token_usage is still emitted too, but those are snapshot values as well. Summing every event therefore adds the same turn/thread usage repeatedly (cache blows up), while the cumulative-guard logic misses input/non-cache tokens.

Suggested fix

  • Group token events by (session_id, turn_id) (or thread) and keep only the last snapshot per turn (or the max cumulative), instead of summing all events;
  • or track monotonic cumulative per thread and take deltas.

Reference

Exact parser I validated locally (per file, keep last turn_token_usage, attribute by its timestamp):

for each rollout jsonl:
    for line in file:
        u = line["payload"]["turn_token_usage"]   # if present
        last = u  # keep only the final snapshot
    total += last  # once per file

Result: 2026-09 month = 136,359,458 tokens (85 threads) vs codeburn 833M.

Happy to open a PR with the parser change + fixtures if the maintainers agree on the approach.

Activity

  1. added
    bug
    area: cli
    provider: codex
    on Sep 14, 2026
  2. iamtoruk commented on Sep 14, 2026

    @iamtoruk
    Member

    Thanks, this is real, and the cause is a step to the side of the hypothesis. Verified against the Codex source and a corpus here; fix in #1383.

    What Codex writes: on every completed model response it appends the response's own usage to the running totals (TokenUsageInfo::append_last_usage), so in token_count events last_token_usage is the per-response delta and total_token_usage the thread's cumulative. The growing numbers you saw in turn_token_usage are the turn's responses accumulating, one record per response, not streaming snapshots. codeburn's parser already sums the deltas correctly for one rollout.

    The multiplier comes from sub-agents. When Codex spawns a sub-agent thread it replays the parent's history into the child's rollout, keeping every token_count line with its original timestamp and dropping the token_usage_record items (codex-rs agent/control/spawn.rs). The child names its parent under source.subagent.thread_spawn.parent_thread_id, not forked_from_id, so codeburn's fork guard never engaged and each child re-counted its parent's prefix. Your ~3.7x is the parent counted once per spawned child plus once for itself. ccusage's Codex adapter documents and handles the same replay.

    #1383 reads the parent id from either field, after which the existing fork guard drops the replayed prefix. On a corpus here with 381 spawned children it removes exactly the replayed events, 405 calls and 403,712 cache tokens, and changes nothing for rollouts without sub-agents.

    Two notes on the comparison table: codeburn's "input" is uncached input, so an exact count that includes cached tokens under input reads higher by design; and keeping only the last turn_token_usage per file counts one turn per thread, which undercounts multi-turn threads. If you can run #1383's branch against your September logs and post the day and month totals, that would close this.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: clibugprovider: codex

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions