Repository navigation
fix: report Codex cache writes as cache writes, not as uncached input - #395
Open
mostafaseyedan wants to merge 1 commit into
Open
mostafaseyedan wants to merge 1 commit into
mostafaseyedan wants to merge 1 commit into
Conversation
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
github-actions
Bot
requested review from
TamasBoncz,
Aymen (aymenfurter) and
Sanjay Singh (san360)
November 25, 2026 22:41
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Codex logs cache writes in
total_token_usage.cache_write_input_tokens, andinput_tokensincludes them. The Codex parser never read that field, socacheWriteTokenswas 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!
handleTokenCountEventalso readscache_write_input_tokensas a non-negative number. A rollout that doesn't log it gives 0.computeModelUsagereports it ascacheWriteTokens, and uncached input is nowmax(0, input_tokens − cached_input_tokens − cache_write_input_tokens).cacheTokensReported(keyed oncached_input_tokens) and the per-turn context fromlast_token_usage.input_tokensare unchanged.CACHE_VERSIONgoes from 31 to 32, so cached sessions are parsed again.Verification
src/core/parser-codex-token-types.test.ts:parses_cache_write_token_usagetest (input_tokens: 100,cached_input_tokens: 40,cache_write_input_tokens: 60) give{ inputTokens: 0, cacheReadTokens: 40, cacheWriteTokens: 60 }. This test fails onmain(inputTokens: 60,cacheWriteTokens: 0);inputTokens: 60,cacheWriteTokens: 0).npm run checkpasses (3,800 tests), andnpm run buildwithnpm run check-sizegives 1.76 MB, within budget.openai, 130,870token_countevents 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 checkpasses (typecheck + lint + spellcheck + knip + tests)🤖 Generated with Claude Code
https://claude.ai/code/session_01RLDXsmVD4oBtxko1vaSk1n