Skip to content

fix(providers): avoid codex sdk cached token double-counting - #7617

Merged
mldangelo merged 3 commits into
promptfoo:mainfrom
bishnubista:fix/codex-sdk-token-accounting-7546
Feb 11, 2026
Merged

mldangelo merged 3 commits into
promptfoo:mainfrom
bishnubista:fix/codex-sdk-token-accounting-7546

Conversation

@bishnubista

@bishnubista bishnubista commented Feb 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix token double-counting in the OpenAI Codex SDK provider. The cached_input_tokens field is a subset of input_tokens (i.e., a breakdown showing how many input tokens were served from cache), not an additional count. The previous implementation added them together, inflating prompt, total, and cost calculations.

Before (incorrect):

input_tokens=10, cached_input_tokens=5, output_tokens=20
→ prompt=15, total=35  (5 extra tokens counted)
→ cost inflated by ~33%

After (correct):

input_tokens=10, cached_input_tokens=5, output_tokens=20
→ prompt=10, total=30  (cached is a subset, not additive)
→ cost accurate

Changes

  • src/providers/openai/codex-sdk.ts — Map tokenUsage.prompt directly to input_tokens instead of input_tokens + cached_input_tokens. Compute total as input_tokens + output_tokens.
  • test/providers/openai-codex-sdk.test.ts — Update token usage assertions and cost expectations to reflect corrected accounting (e.g., prompt: 10 instead of 15, total: 30 instead of 35, cost 0.003 instead of 0.00325).

Test Plan

  • npx vitest run test/providers/openai-codex-sdk.test.ts — all pass
  • npm run l — no lint errors
  • npm run f — formatting clean

Fixes

Closes #7546

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 [email protected]

@bishnubista
bishnubista marked this pull request as ready for review February 11, 2026 03:55

@promptfoo-scanner promptfoo-scanner Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 All Clear

I reviewed this PR for LLM security vulnerabilities. The changes correct a token counting bug where cached input tokens were being double-counted in usage metrics. This is purely an accounting fix that doesn't affect data flow to/from LLMs, prompt handling, output processing, or agent capabilities.

Minimum severity threshold: 🟡 Medium | To re-scan after changes, comment @promptfoo-scanner
Learn more


Was this helpful?  👍 Yes  |  👎 No 

@coderabbitai

coderabbitai Bot commented Feb 11, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This pull request fixes a double-counting bug in the OpenAI Codex SDK provider where cached_input_tokens were being incorrectly added to input_tokens in token usage calculations. The fix removes cached_input_tokens from prompt and total token counts in the provider implementation, treating cached tokens as a subset of input tokens per Codex SDK semantics. Corresponding test expectations are updated to reflect corrected token accounting, including adjusted cost calculations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly and concisely describes the main fix: avoiding double-counting of cached tokens in the Codex SDK provider.
Description check ✅ Passed The PR description provides comprehensive context on the bug (double-counting cached tokens), explains the fix with before/after examples, lists changed files, and references a test plan and linked issue.
Linked Issues check ✅ Passed The PR satisfies all coding requirements from #7546: prompt now uses only input_tokens, total uses input_tokens + output_tokens, and cached_input_tokens is correctly treated as a subset rather than additive.
Out of Scope Changes check ✅ Passed All changes are in scope: tokenUsage calculations in codex-sdk.ts provider and corresponding test updates, directly addressing the double-counting bug described in #7546.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
src/providers/openai/codex-sdk.ts (1)

150-157: Fix looks correct; consider also using total_tokens when available.

The double-counting fix is right — cached_input_tokens is indeed a subset of input_tokens. However, issue #7546 also specifies preferring total_tokens from the API response when present:

total = turn.usage.total_tokens ?? (input_tokens + output_tokens)

This would be more resilient if the API ever includes additional token categories beyond input + output.

Suggested improvement
 const tokenUsage: ProviderResponse['tokenUsage'] = turn.usage
   ? {
       // cached_input_tokens is already included in input_tokens by the Codex SDK.
       prompt: turn.usage.input_tokens,
       completion: turn.usage.output_tokens,
-      total: turn.usage.input_tokens + turn.usage.output_tokens,
+      total: turn.usage.total_tokens ?? (turn.usage.input_tokens + turn.usage.output_tokens),
     }
   : undefined;

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

bishnubista and others added 2 commits February 10, 2026 22:23
…in codex sdk

Cached input tokens were silently discarded and all input tokens were
charged at full price. Now populates tokenUsage.cached so users can see
cache hit rates, and applies OpenAI's 90% cache discount to cost
calculation (cached tokens charged at input_price/10).

Co-Authored-By: Claude Opus 4.6 <[email protected]>

@mldangelo mldangelo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @bishnubista! Please reach out (michael @ promptfoo.dev) if you'd like some promptfoo swag!

@mldangelo
mldangelo merged commit c4059b8 into promptfoo:main Feb 11, 2026
72 of 73 checks passed
@bishnubista
bishnubista deleted the fix/codex-sdk-token-accounting-7546 branch February 12, 2026 00:48
@bishnubista

Copy link
Copy Markdown
Contributor Author

Thank you @bishnubista! Please reach out (michael @ promptfoo.dev) if you'd like some promptfoo swag!

cool, will do!

meSingh pushed a commit to meSingh/promptfoo that referenced this pull request Sep 6, 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.

OpenAI Codex SDK provider double-counts cached_input_tokens in tokenUsage

2 participants