Skip to content

Codex subagent traces double-count inherited token history #17

Description

@gregb122

Summary

Codex child rollout files contain copied parent token history followed by the child's new work. Token Meter groups these files under the shared session_id and prices every token_count.last_token_usage row, so inherited history is counted repeatedly.

Tested against main at 79f1759d854c84ecda4858b3e350755bf765c894 on macOS.

Evidence

  • At one fixed checkpoint, Token Meter displayed $2,503.041764 and 3.63B priced tokens.
  • Removing only exact inherited prefixes and 39 adjacent duplicate snapshots produced $156.084712 and 200.91M priced tokens: 16.04× inflation.
  • The checkpoint contained 54 physical traces for five logical tasks; 49 traces were children.
  • token_meter/runtimes/codex.py:190-195 prefers the shared session_id as the source ID.
  • token_meter/runtimes/codex.py:227-229 selects payload.session_id before the unique payload.id.
  • token_meter/runtimes/codex.py:878-905 prices every token-count row without accounting for lineage.
  • Related issue #2 covers duplicate Claude sources, but choosing one canonical Codex file would lose legitimate child work. Codex children require inherited-prefix removal.

Impact

Daily, session, model, and budget totals can be overstated by an order of magnitude when Codex uses multiple or nested subagents. Users may also interpret the API-equivalent estimate as actual subscription spend.

Suggested fix

  1. Preserve separate physical trace ID, logical session ID, forked_from_id, and parent_thread_id fields.
  2. For each child, remove only the longest exact token-event prefix shared with forked_from_id, falling back to parent_thread_id.
  3. Compare model plus numeric last_token_usage fields and ignore timestamps because copied history is re-stamped.
  4. After prefix removal, skip only adjacent events whose model, last_token_usage, and total_token_usage are all identical.
  5. Continue pricing last_token_usage; do not price cumulative-total deltas.
  6. Label Codex dollars as estimated public API equivalent.

Acceptance criteria

  • Root, direct-child, and nested-child fixtures count inherited history once.
  • Children without a resolvable exact prefix retain all events.
  • Adjacent exact snapshots are removed only after lineage matching.
  • Session, Daily, Models, and budget totals agree after correction.
  • The reproduced checkpoint is approximately $156.08, not $2,503.04.

If accepted I can work on fixing this

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions