fix: include cached input tokens in OpenAI usage - #18
Conversation
Cognition's cloud API reports cached prompt tokens separately from uncached prompt tokens. OpenAI-compatible prompt_tokens should be the full input count (cached + uncached), so fold cachedInputTokens back in. This prevents fully prompt-cached requests (large system prompts + tool schemas) from appearing to consume only ~1 input token in opencode. - add buildOpenAIUsage helper that returns prompt_tokens, completion_tokens, total_tokens, and prompt_tokens_details.cached_tokens - use helper in both streaming and non-streaming completion paths - add unit tests for cached, uncached, and empty usage
📝 WalkthroughWalkthroughCached input tokens are now included in OpenAI-compatible usage accounting. Streaming and non-streaming responses use a shared exported helper that reports cached prompt tokens and updated totals, with unit tests covering cached, absent, and empty usage inputs. ChangesUsage Accounting
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/plugin.ts (1)
421-421: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd coverage for both response paths.
The current tests exercise
buildOpenAIUsagedirectly, but not the streaming SSE or non-streaming response wiring. Add focused tests that emitcachedInputTokensand assert the serialized usage in each path; otherwise either capture/serialization path could regress while the helper tests still pass.Also applies to: 599-599
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/plugin.ts` at line 421, The tests cover buildOpenAIUsage directly but not its integration with streaming SSE and non-streaming responses. Add focused tests for both response paths, emitting cachedInputTokens and asserting the serialized usage payload, including the wiring around buildOpenAIUsage in each path.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/plugin.ts`:
- Line 421: The tests cover buildOpenAIUsage directly but not its integration
with streaming SSE and non-streaming responses. Add focused tests for both
response paths, emitting cachedInputTokens and asserting the serialized usage
payload, including the wiring around buildOpenAIUsage in each path.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9f326a29-a259-4294-acac-a4a21ded7c88
📒 Files selected for processing (2)
src/plugin.tstests/unit/usage.test.ts
Cognition's cloud API reports cached prompt tokens separately from uncached prompt tokens. OpenAI-compatible "prompt_tokens" should be the full input count (cached + uncached), so fold "cachedInputTokens" back in.
This prevents fully prompt-cached requests (large system prompts + tool schemas) from appearing to consume only ~1 input token in opencode.
Changes:
Summary by CodeRabbit
New Features
prompt_tokens_details.cached_tokens.Tests