r/OpenaiCodex • • 2d ago

Showcase / Highlight Built a zero-dependency local Codex usage & cache-savings auditor (fixes inclusive cached_input_tokens double-counting across Host + WSL2)

Post image

I noticed many local usage trackers sum OpenAI Codex's input_tokens and cached_input_tokens separately — even though Codex's input_tokens is inclusive of cached input (unlike Anthropic/Claude Code, where cache reads are disjoint). On heavy sessions that inflates token counts and breaks cost/cache-hit estimates.

So I built Agent Usage Radar (v1.1.2) — a pure Node.js 24 (node:sqlite, zero npm dependencies, 100% local read-only, Zero-PII) CLI, local loopback GUI, and stdio MCP server that audits Codex alongside Claude Code, Antigravity, Hermes (WSL2), Cursor, and Cline/Roo:

  • Exact inclusive-vs-disjoint cache math: subtracts cached_input_tokens + cache_write_input_tokens from Codex input_tokens and handles compaction segment resets cleanly
  • Date-range filtering: --from YYYY-MM-DD --to YYYY-MM-DD in CLI plus interactive presets (Today / 7d / 30d / This Month / Custom) in the local UI
  • Lock-free Host + WSL2 SQLite WAL reads + identity-keyed ledger that survives rolling JSONL rotation

Quick read-only try (free MIT CLI):

npx agent-usage-radar scan
  • GitHub (78/78 tests passing): https://github.com/augustfu-droid/agent-usage-radar
  • npm: https://www.npmjs.com/package/agent-usage-radar
1 Upvotes

6 comments sorted by

1

u/ChemicalExcellent463 2d ago

i counted 38 billion tokens for october through the 28th from claude code and codex logs on one machine. how much did double-counting cached input inflate the totals in the sessions you checked?

2

u/Specific_Shock_3465 2d ago

Across the 111 detailed Codex rollout sessions audited on my machine (`859.6M` actual tokens after cumulative diffing: `19.3M` uncached input, `836.9M` `cached_input_tokens`, `3.4M` output), cached reads made up 97.7% of input.

So naively summing Codex's `input_tokens + cached_input_tokens + output_tokens` inflated those sessions from `859.6M` -> `1.70B` (+97.4% / 1.97x aggregate):
• Per-session median: +94.1% (IQR +90.7% to +95.8%, peaking at +98.7% on long multi-turn runs).
• Including `443` older pruned threads (`2.51B` aggregate `tokens_used` left unsplit rather than guessed): total Codex moved from `3.37B` actual to `4.21B` (+24.8% overall).

Two quick sanity checks specifically for a 38B combined Claude Code + Codex monthly count:

  1. Inclusive cache double-counting alone caps at <=2x (+100%) on Codex (since `cached_input_tokens <= input_tokens`), and Anthropic/Claude Code logs keep `cache_read_input_tokens` and `cache_creation_input_tokens` disjoint from `input_tokens` - so fixing inclusive double-counting alone would turn 38B into ~19B+ at minimum, not a 10x drop.
  2. Check cumulative snapshots & dedup: In Codex rollout JSONLs, `event_msg` (`token_count`) logs `payload.info.total_token_usage` as a running session-cumulative counter, not a per-turn delta (`last_token_usage` is per-turn). Summing every `total_token_usage` snapshot without diffing consecutive entries blows up quadratically (on those exact 111 sessions in my DB, summing snapshots directly would turn `859.6M` into `~385.4B`!). Also check Claude Code streaming chunks dedup (`message.id + requestId`, keeping the last observation) and Codex overlap across `state_5.sqlite`, `sessions/`, and `archived_sessions/`.

If you want a quick read-only cross-check on that date window, `npx agent-usage-radar scan --from 2026-10-01 --to 2026-10-28` will show how much of the 38B comes from real cached reads vs double-counted snapshots/chunks.

1

u/ChemicalExcellent463 2d ago

859.6M turning into 385B from summing running totals is the part that jumps out. my 38B came from ccusage, have you checked how it handles those Codex snapshots?

2

u/Specific_Shock_3465 1d ago

Just checked `ryoppippi/ccusage`'s current `main` branch (specifically `rust/adapters/codex/src/parser/usage.rs`, `report/table.rs`, and `report/cost.rs`) and cross-checked its exact Codex parsing logic against `agent-usage-radar` on all 134 Codex rollout JSONLs on my machine (8,903 `token_count` events, including 668 duplicate snapshots).

Good news for your `ccusage` run -- current `ccusage` handles both issues properly:

  1. Snapshots & duplicates (`codex_token_count_usage` in `parser/usage.rs`): It tracks `previous_totals` from `total_token_usage`, checks whether `total_token_usage` actually advanced (`cumulative_advanced`), prefers `last_token_usage` when it advanced (falling back to `subtract_codex_raw_usage` if `last_token_usage` is absent), and skips identical consecutive snapshots instead of summing running totals.
  2. Inclusive cache math (`normalize_codex_raw_usage` + `non_cached_input_tokens` in `report/table.rs` & `report/cost.rs`): It clamps `cached_input_tokens <= input_tokens` and subtracts cached reads when reporting uncached input and computing cost, without double-adding cached input into `total_tokens`.

On my local cross-check across those 134 Codex rollout files:
• `agent-usage-radar` (cumulative diff on `total_token_usage`): `1,181,298,914` tokens (`25.2M` uncached input, `1.15B` cached input, `4.5M` output)
• `ccusage`'s `codex_token_count_usage` logic: `1,181,298,914` tokens (0-token diff across all 134/134 files)
• Naive sum of `total_token_usage` snapshots (what custom scripts often get wrong): `758.3B` (`641.9x` inflation)

So if your 38B came from a current build of `ccusage`, it is almost certainly not the quadratic snapshot-summing bug or inclusive-cache double counting -- heavy multi-session Claude Code + Codex workflows at ~97%+ cache hit rate genuinely rack up tens of billions of *total* tokens (mostly cache reads), while actual uncached input + output is typically only a few percent of that headline number. (The only edge cases worth checking are if you ran an older `ccusage` build back in October or manually merged overlapping `sessions/` and `archived_sessions/` logs.)

1

u/ChemicalExcellent463 1d ago

uncached input plus output was only about 2.5% of your total, thanks for digging into that. which number do you actually watch day to day?