Skip to content

fix: token accounting (cachedWriteTokens & cacheWriteInputTokens) - #510

Open
joshlarsen wants to merge 1 commit into
agentclientprotocol:mainfrom
joshlarsen:260913-cache-write-tokens-fix
Open

fix: token accounting (cachedWriteTokens & cacheWriteInputTokens)#510
joshlarsen wants to merge 1 commit into
agentclientprotocol:mainfrom
joshlarsen:260913-cache-write-tokens-fix

Conversation

@joshlarsen

@joshlarsen joshlarsen commented Sep 13, 2026

Copy link
Copy Markdown

Summary

Fix cache-write token accounting by separating cache writes from ordinary input and exposing them through ACP's cachedWriteTokens field.

Fixes #509. The cachedWriteTokens omission is also mentioned in #447.

The change also:

  • includes cacheWriteInputTokens in quota metadata;
  • displays cache reads and writes separately in /status;
  • treats a missing cache-write value as zero for older binaries supplied through CODEX_PATH;
  • leaves total, output, and reasoning counts unchanged.

Token calculation

Codex reports inputTokens inclusive of cache reads and writes. The normalized categories are therefore:

ordinary input = inputTokens - cachedInputTokens - cacheWriteInputTokens
cache read     = cachedInputTokens
cache write    = cacheWriteInputTokens
output         = outputTokens
total          = ordinary input + cache read + cache write + output

Reasoning tokens are already included in outputTokens, so reasoningOutputTokens is exposed as ACP thoughtTokens but is not added again.

For example:

Codex: input=100, cache-read=40, cache-write=30, output=10, reasoning=5, total=110
ACP:   input=30,  cache-read=40, cache-write=30, output=10, reasoning=5, total=110

The mapping is:

Codex ACP
inputTokens - cachedInputTokens - cacheWriteInputTokens inputTokens
cachedInputTokens cachedReadTokens
cacheWriteInputTokens cachedWriteTokens
outputTokens outputTokens
reasoningOutputTokens thoughtTokens
totalTokens totalTokens

Compatibility

cacheWriteInputTokens is required by the current generated app-server type. At runtime, a missing value from an older CODEX_PATH binary is normalized to zero. Invalid negative partitions are not clamped, so inconsistent upstream counters remain observable.

Validation

  • npm run typecheck
  • npm test — 609 tests passed
  • npm run build
  • Live Codex smoke test confirmed 3 ordinary input + 11,099 cache-write + 5 output = 11,107 total.

OpenAI references

@joshlarsen joshlarsen changed the title fix token accounting (cachedWriteTokens & cacheWriteInputTokens) fix: token accounting (cachedWriteTokens & cacheWriteInputTokens) Sep 13, 2026
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.

cachedWriteTokens missing leading to incorrect inputTokens accounting

1 participant