Skip to content

fix: report Codex cache writes as cache writes, not as uncached input - #395

Open
mostafaseyedan wants to merge 1 commit into
microsoft:mainfrom
mostafaseyedan:fix/codex-cache-write-tokens
Open

mostafaseyedan wants to merge 1 commit into
microsoft:mainfrom
mostafaseyedan:fix/codex-cache-write-tokens

Conversation

@mostafaseyedan

Copy link
Copy Markdown
Contributor

Problem

Codex logs cache writes in total_token_usage.cache_write_input_tokens, and input_tokens includes them. The Codex parser never read that field, so cacheWriteTokens was always 0 for Codex sessions and the tokens written to the cache were counted as uncached input (#355).

Change

This follows the fix sketch in #355. Thank you, Sanjay Singh (@san360), for the very clear write-up!

  • handleTokenCountEvent also reads cache_write_input_tokens as a non-negative number. A rollout that doesn't log it gives 0.
  • computeModelUsage reports it as cacheWriteTokens, and uncached input is now max(0, input_tokens − cached_input_tokens − cache_write_input_tokens).
  • cacheTokensReported (keyed on cached_input_tokens) and the per-turn context from last_token_usage.input_tokens are unchanged.
  • CACHE_VERSION goes from 31 to 32, so cached sessions are parsed again.

Verification

  • Two new tests in src/core/parser-codex-token-types.test.ts:
    • the numbers of Codex's own parses_cache_write_token_usage test (input_tokens: 100, cached_input_tokens: 40, cache_write_input_tokens: 60) give { inputTokens: 0, cacheReadTokens: 40, cacheWriteTokens: 60 }. This test fails on main (inputTokens: 60, cacheWriteTokens: 0);
    • a rollout without the field keeps its totals (inputTokens: 60, cacheWriteTokens: 0).
  • npm run check passes (3,800 tests), and npm run build with npm run check-size gives 1.76 MB, within budget.
  • Real data: in my Codex logs (873 sessions with token counts, Sep 2025 to Nov 2026, provider openai, 130,870 token_count events with totals), the field is present and is 0 in every event. So this PR changes no number for those sessions; it matters once a provider reports cache writes.

Related Issues

Fixes #355

Checklist

  • npm run check passes (typecheck + lint + spellcheck + knip + tests)
  • Changes are covered by tests (if applicable)
  • Documentation updated (if applicable): not needed

🤖 Generated with Claude Code

https://claude.ai/code/session_01RLDXsmVD4oBtxko1vaSk1n

Codex logs cache writes in total_token_usage.cache_write_input_tokens,
and input_tokens includes them. The parser never read the field, so
cacheWriteTokens was always 0 and the written tokens were counted as
uncached input. Read the field (0 when a rollout doesn't log it),
report it as cacheWriteTokens and subtract it from uncached input.
Bump CACHE_VERSION so cached sessions are parsed again.

Fixes microsoft#355

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RLDXsmVD4oBtxko1vaSk1n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex parser ignores cache_write_input_tokens and counts cache writes as uncached input

1 participant