Skip to content

fix: include cached input tokens in OpenAI usage - #18

Open
nmzpy wants to merge 1 commit into
rsvedant:masterfrom
nmzpy:fix/cached-token-usage
Open

fix: include cached input tokens in OpenAI usage#18
nmzpy wants to merge 1 commit into
rsvedant:masterfrom
nmzpy:fix/cached-token-usage

Conversation

@nmzpy

@nmzpy nmzpy commented Jul 10, 2026

Copy link
Copy Markdown

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:

  • Added "buildOpenAIUsage" helper that returns prompt_tokens, completion_tokens, total_tokens, and prompt_tokens_details.cached_tokens.
  • Used the helper in both streaming and non-streaming completion paths.
  • Added unit tests for cached, uncached, and empty usage.

Summary by CodeRabbit

  • New Features

    • Token usage reporting now includes cached input tokens.
    • Streaming and non-streaming responses expose cached token counts through prompt_tokens_details.cached_tokens.
    • Prompt and total token counts now accurately reflect cached input usage.
  • Tests

    • Added coverage for cached, uncached, and missing token usage data.

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
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Cached 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.

Changes

Usage Accounting

Layer / File(s) Summary
OpenAI usage builder
src/plugin.ts
Adds buildOpenAIUsage to combine prompt and cached input tokens, calculate totals, and expose prompt_tokens_details.cached_tokens.
Streaming and non-streaming integration
src/plugin.ts, tests/unit/usage.test.ts
Both usage paths capture cached input tokens and build responses through the shared helper; tests cover cached, missing, and empty token values.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: including cached input tokens in OpenAI usage reporting.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/plugin.ts (1)

421-421: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add coverage for both response paths.

The current tests exercise buildOpenAIUsage directly, but not the streaming SSE or non-streaming response wiring. Add focused tests that emit cachedInputTokens and 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

📥 Commits

Reviewing files that changed from the base of the PR and between c331611 and c21ba5c.

📒 Files selected for processing (2)
  • src/plugin.ts
  • tests/unit/usage.test.ts

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.

1 participant