Skip to content

fix(cost): account for OpenRouter cache write tokens#18445

Closed
LucasSantana-Dev wants to merge 1 commit intoanomalyco:devfrom
LucasSantana-Dev:fix/openrouter-cache-write-token-accounting
Closed

fix(cost): account for OpenRouter cache write tokens#18445
LucasSantana-Dev wants to merge 1 commit intoanomalyco:devfrom
LucasSantana-Dev:fix/openrouter-cache-write-token-accounting

Conversation

@LucasSantana-Dev
Copy link

@LucasSantana-Dev LucasSantana-Dev commented Mar 20, 2026

Issue for this PR

Closes #18440

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

This fixes OpenRouter cost underestimation when cache write tokens are present. OpenAI-compatible responses can report prompt_tokens_details.cache_write_tokens, but we were not carrying that through for usage accounting.

Changes in this PR:

  • parse cache_write_tokens in OpenAI-compatible provider paths (stream + non-stream)
  • persist that value into provider metadata (openrouter.usage.cacheWriteInputTokens)
  • include that metadata fallback in Session.getUsage so cache write tokens are counted in token/cost totals
  • add regression coverage for the OpenRouter metadata path in Session.getUsage tests

How did you verify your code works?

  • cd packages/opencode && bun test test/session/compaction.test.ts
  • cd packages/opencode && bun run typecheck

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Mar 20, 2026
@github-actions
Copy link
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@rekram1-node
Copy link
Collaborator

I'm having a hard time trusting these PRs because if u look at the file paths u edited, it specifically says it is specifically and ONLY for copilot, so what does that have to do with openrouter?

That codepath should basically never be hit unless u have a custom provider configured to use the github copilot sdk which I kinda doubt?

@rekram1-node
Copy link
Collaborator

@LucasSantana-Dev
Copy link
Author

LucasSantana-Dev commented Mar 21, 2026

It even has a readme saying as much:

https://github.com/anomalyco/opencode/tree/dev/packages/opencode/src/provider/sdk/copilot#readme

I'll have a second look on that and figure out a more structured and fitting solution for the idea i had!

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.

Cache write tokens not accounted for when using OpenRouter, causing cost underestimation

2 participants