Repository navigation
fix(providers): avoid codex sdk cached token double-counting - #7617
Conversation
There was a problem hiding this comment.
👍 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
📝 WalkthroughWalkthroughThis pull request fixes a double-counting bug in the OpenAI Codex SDK provider where Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
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. Comment |
…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
left a comment
There was a problem hiding this comment.
Thank you @bishnubista! Please reach out (michael @ promptfoo.dev) if you'd like some promptfoo swag!
cool, will do! |
Summary
Fix token double-counting in the OpenAI Codex SDK provider. The
cached_input_tokensfield is a subset ofinput_tokens(i.e., a breakdown showing how many input tokens were served from cache), not an additional count. The previous implementation added them together, inflatingprompt,total, and cost calculations.Before (incorrect):
After (correct):
Changes
src/providers/openai/codex-sdk.ts— MaptokenUsage.promptdirectly toinput_tokensinstead ofinput_tokens + cached_input_tokens. Computetotalasinput_tokens + output_tokens.test/providers/openai-codex-sdk.test.ts— Update token usage assertions and cost expectations to reflect corrected accounting (e.g.,prompt: 10instead of15,total: 30instead of35, cost0.003instead of0.00325).Test Plan
npx vitest run test/providers/openai-codex-sdk.test.ts— all passnpm run l— no lint errorsnpm run f— formatting cleanFixes
Closes #7546
🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.6 [email protected]