Skip to content

@ccusage/codex: Forked sessions double count shared token usage across sessions #897

@astarforbae

Description

@astarforbae

Summary

When using Codex session forking, @ccusage/codex significantly overcounts token usage.

Forked sessions inherit the full history of the parent session, but ccusage aggregates each session independently. As a result, shared usage is counted multiple times.


Example

  • Session A: total usage = 100
  • Fork → Session B: total usage = 150 (includes the original 100)

Expected total:

  • 100 (A) + 50 (new in B) = 150

Actual:

  • 100 (A) + 150 (B) = 250 ❌

What I tested locally

I implemented a fork-safe counting approach based on content-level deduplication of token events:

  • Treat the token_count payload as the canonical event identity
  • Ignore outer fields such as timestamps, file paths, or session boundaries
  • Normalize the payload and compute a stable hash
  • If two events have identical payloads, they are treated as the same event and counted only once

This ensures that replayed history from forked sessions is not double counted.


Real-world Impact

After applying this approach:

  • Original total tokens: ~277M

  • Corrected total tokens: ~81M

  • Reduction: ~196.9M tokens (~70.8% lower, ~3.4× inflation)

  • Original cost: ~$135.66

  • Corrected cost: ~$37.38

  • Reduction: ~$98.28 (~72.4% lower, ~3.6× inflation)

  • Deduplicated overlapping events: 11,523


Expected Behavior

Only the incremental usage after the fork should be counted for child sessions, rather than re-counting the entire inherited history.


Possible Cause

Aggregation is performed per session without accounting for shared history between forked sessions.


Impact

This leads to substantial overestimation of usage, especially for workflows that involve frequent session forking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions