Skip to content

- Codex/Claude Code fork session token overcounting — ccusage treats all fork branches as independent without dedup - #1175

Description

@solidusex

What happened?

Summary

ccusage aggregates token deltas from all session files without accounting for fork relationships, leading to 3-4x token overcounting when users frequently fork sessions. Both Codex and Claude Code are affected, though in different ways.

Evidence

Codex: fork chain token duplication (confirmed with real data)

I analyzed a real 4-level Codex fork chain on my machine (ccusage v20.0.1, Codex CLI v0.131.0):

019e648e (root, before 5/27)
  └─ 019e67b4 (fork 1, 5/27)
      ├─ 019e68e3 (fork 2, 5/27)  
      └─ 019e6a50 (fork 3, 5/28)
          └─ 019e6acf (fork 4, 5/28)

### Steps to reproduce

1. Have a Codex session with accumulated token usage (e.g., 178M tokens)
2. Fork that session (e.g., `codex fork <session_id>`)
3. The forked session's JSONL file will show `forked_from_id` in `session_meta`
4. Run `ccusage codex daily --json`
5. Both parent and forked session deltas are summed independently
6. Result: total tokens = parent_delta + fork_delta, even though fork re-sent the same context

Concrete data from my machine (ccusage v20.0.1, Codex v0.131.0-alpha.9):

019e648e (root)
  └─ 019e67b4 (fork 1, 5/27) — delta: 178M tokens
      ├─ 019e68e3 (fork 2, 5/27) — delta: 149M tokens
      └─ 019e6a50 (fork 3, 5/28) — delta: 190M tokens
          └─ 019e6acf (fork 4, 5/28) — delta: 195M tokens

Each fork child's `total_token_usage` resets to ~22,812 (system prompt baseline) rather than inheriting parent's accumulator. ccusage sums all four deltas = 712M tokens, but the actual new context processed is closer to 195M (last fork's cumulative).

### Expected behavior

When `forked_from_id` is present in `session_meta`, ccusage should recognize the session as a fork and avoid double-counting the inherited historical context. Options:

- Option A (preferred): Build fork trees and only count tokens from divergence points (after `thread_rolled_back` events)
- Option B: Expose `forkedFrom` in JSON output so users can dedup themselves  
- Option C: Add a `--dedup-forks` flag

Without dedup, heavy fork users see 3-4x inflated token counts in daily/monthly reports.

### Version

_No response_

Activity

  1. github-actions commented on May 28, 2026

    @github-actions
    Contributor

    This issue was auto-closed. Issues from new contributors are auto-closed by default.

    Maintainers review auto-closed issues and reopen worthwhile ones. Issues that do not meet the quality bar in CONTRIBUTING.md may not be reopened or receive a reply.

    Keep the issue short, concrete, and written in your own voice.

    If a maintainer replies lgtmi, your future issues will stay open. If a maintainer replies lgtm, your future issues and PRs will stay open.

    See CONTRIBUTING.md.

  2. ryoppippi commented on Aug 31, 2026

    @ryoppippi
    Member

    Historical audit: this discussion was auto-closed by the legacy contributor gate. That closure did not assess technical importance.

    Audit result: resolved. A later merged change or the current main implementation covers this request. This item is kept for history and does not need to be reopened.

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 workingtriage:resolvedResolved by a later change or current implementation.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions