Skip to content

Trace: define cache accounting and prevent token overcounting across providers #3706

Open

Description

@migmartri

Problem

Chainloop Trace reports inflated token usage and estimated cost for a Codex session. Review cache reporting and token accounting across all supported providers. Define and test the rules so this error cannot recur.

Session: 01a1657c-0f4f-7603-96e9-b99f4ee939d7

Evidence digest: sha256:d729cade5c9f2c7eef4ca720290133db119e291445a70e21175ef4901f819391

Download with:

chainloop artifact download --digest sha256:d729cade5c9f2c7eef4ca720290133db119e291445a70e21175ef4901f819391 --output session.json

Verified evidence

Metric Reported Sum of 275 unique response records
Input tokens 5,242,914,636 51,803,136
Cached input tokens, included in input 5,160,057,426 51,113,536
Output tokens 17,901,845 168,426
Total tokens 5,260,816,481 51,971,562

The response sums match the final token_usage_record.thread_token_usage counters. Replaying the current parser reproduces the reported numbers exactly, with 147 reset warnings.

The Codex parser consumes two cumulative streams: token_usage_record.thread_token_usage and event_msg.token_count.info.total_token_usage. They diverge after compaction in this transcript. Lower snapshots trigger the reset branch, which adds entire cumulative totals again. The artifact's $900.8926 cost estimate also uses inflated counts. It does not establish billed spend.

Scope and acceptance criteria

  • Decide whether cache usage belongs in the reported total, separate metrics, or both. Document the decision and metric names.
  • Audit Codex, Claude, Cursor, Pi, and OpenCode. Record whether input includes cache reads or writes, and whether output includes reasoning tokens.
  • Encode each provider's semantics in parsing and normalization. Count cache and reasoning subsets only once.
  • Define how to handle duplicate events, multiple usage streams, cumulative counters, per-response usage, compaction, resets, and resumed sessions.
  • Add regression fixtures and assertions for those cases. The supplied artifact must produce the response totals above, without false reset warnings.
  • Verify cost calculations use the same normalized counts and each provider's cache semantics.
  • Document any required evidence-schema or display changes, including the treatment of existing inflated records.

Starting points

  • app/cli/internal/trace/codex/parse.go: consumeUsage
  • app/cli/internal/trace/codex/pricing.go
  • Provider parsers under app/cli/internal/trace/{claude,cursor,pi,opencode}/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions