Skip to content

codex: spawned subagent's first response is not counted when its token_count event repeats #1970

Closed

Description

@parttimediarybehavior

What happened?

ccusage codex does not count a spawned subagent's first response when Codex writes that response's token_count event twice, less than a second apart.

  • If the subagent has only one response, the whole subagent is missing from codex session and codex daily.
  • If it has more responses, only the first is missing.

There is no error or warning, so the totals are silently too low.

I found this in my own ~/.codex/sessions. One subagent rollout had one response followed by two identical token_count events, 150 ms apart, and its 15,870 tokens were missing from my reports. I maintain a small tool that runs ccusage for usage receipts, and its own cross-check flagged the gap.

I think the cause is the replay fallback for thread_spawn rollouts (#1218, #1501). The repeated event looks like a replay burst, so the real first response is skipped as replayed parent history. Codex re-emitting unchanged token_count events is already known from #876 and #1288.

Steps to reproduce

  1. Save these 4 lines as $CODEX_HOME/sessions/2026/01/05/rollout-2026-01-05T15-00-00-22222222-0000-4000-8000-000000000000.jsonl:
{"timestamp":"2026-01-05T15:00:00.000Z","type":"session_meta","payload":{"id":"22222222-0000-4000-8000-000000000000","timestamp":"2026-01-05T15:00:00.000Z","cwd":"/tmp/x","source":{"subagent":{"thread_spawn":{"parent_thread_id":"11111111-0000-4000-8000-000000000000","depth":1}}}}}
{"timestamp":"2026-01-05T15:00:01.000Z","type":"turn_context","payload":{"model":"gpt-5"}}
{"timestamp":"2026-01-05T15:00:02.000Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":1000,"cached_input_tokens":400,"output_tokens":10,"reasoning_output_tokens":0,"total_tokens":1010},"last_token_usage":{"input_tokens":1000,"cached_input_tokens":400,"output_tokens":10,"reasoning_output_tokens":0,"total_tokens":1010}}}}
{"timestamp":"2026-01-05T15:00:02.150Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":1000,"cached_input_tokens":400,"output_tokens":10,"reasoning_output_tokens":0,"total_tokens":1010},"last_token_usage":{"input_tokens":1000,"cached_input_tokens":400,"output_tokens":10,"reasoning_output_tokens":0,"total_tokens":1010}}}}
  1. Run ccusage codex daily --json --offline -z UTC.
  2. Result: totalTokens is 0 and there are no days.

Other results with the same file:

Change Result
"source":"cli" instead of thread_spawn 1010 (correct)
Second event 2 s later instead of 150 ms 1010 (correct)
Only one token_count event 1010 (correct)
A second response added, both events repeated first response missing (2020 instead of 3030)
A parent rollout in the same directory parent counted, subagent still missing

Expected behavior

The subagent's response is counted once: 1010 tokens. A repeated token_count event with an unchanged total should be treated as a duplicate, not as replayed history.

Version

20.0.38 and 20.0.44 (latest). Same result on both.

Activity

  1. pullfrog commented on Oct 26, 2026

    @pullfrog
    Contributor
  2. added
    bugSomething isn't working
    priority:mediumA normal bug or meaningful improvement.
    triage:maintainableFits the repository's maintainable scope and needs review.
    on Oct 26, 2026
  3. added
    priority:highHigh user impact or an important regression.
    and removed
    priority:mediumA normal bug or meaningful improvement.
    on Oct 26, 2026
  4. github-actions commented on Oct 26, 2026

    @github-actions
    Contributor

    Pullfrog triage: priority:high
    Kind: bug
    Maintenance fit: maintainable
    Confidence: high

    Reproducible undercount in core codex reporting: minimal fixture shows subagent first response dropped to 0 tokens when duplicate token_count arrives 150ms apart, with passing control cases, confirmed on latest release. Silently low totals directly break usage promise.

    Decision: kept open; Pullfrog will attempt a focused implementation PR.

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

    bugSomething isn't workingpriority:highHigh user impact or an important regression.triage:maintainableFits the repository's maintainable scope and needs review.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions