core: OpenRouter BYOK sessions record cost = 0 since v2.2.0 (ignores cost_details.upstream_inference_cost)
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
Research direction
The issue points to cost parsing in packages/ai and persistence in session_message and session_v2; start by tracing where OpenRouter's streaming usage cost is read. Check how cost_details.upstream_inference_cost and catalog pricing are handled when is_byok is true and cost is zero. Done means BYOK usage records the upstream or catalog cost instead of zero.
Written by the indexing model from the issue text.
Description
Summary
Since v2.2.0 (#58562), OpenRouter sessions using BYOK (is_byok: true) record cost = 0 on every assistant message and session. For BYOK requests, OpenRouter sets usage.cost: 0 (OpenRouter's own platform fee) and puts the actual upstream API cost in usage.cost_details.upstream_inference_cost. Because usage.cost is 0 (not undefined), OpenCode records $0 instead of reading upstream_inference_cost or falling back to catalog pricing.
Environment
- opencode version: 2.2.2 (regression started in 2.2.0)
- OS: Linux 7.0.0-38-generic (linux x64)
- Terminal: TERM_PROGRAM=tmux, TERM=tmux-256color, COLORTERM=truecolor
- Shell: /bin/bash
- Install/channel: latest
- Active plugins: @dietrichgebert/ponytail, superpowers@git+https://github.com/obra/superpowers.git
Reproduction
- Configure
openrouterwith BYOK enabled on OpenRouter (e.g. Google AI Studio or OpenAI key). - Run a session in OpenCode
>= 2.2.0(e.g.openrouter/google/gemini-4-argonoropenrouter/openai/gpt-6.1-sol). - Inspect the final SSE chunk from OpenRouter and the stored session cost in
opencode.db.
Expected Behavior
OpenCode should account for usage.cost_details.upstream_inference_cost (e.g. usage.cost + usage.cost_details.upstream_inference_cost) when present, or fall back to catalog pricing when is_byok: true and usage.cost === 0.
Actual Behavior
OpenRouter returns the following usage payload in the final streaming chunk:
"usage": {
"prompt_tokens": 33,
"completion_tokens": 5,
"total_tokens": 38,
"cost": 0,
"is_byok": true,
"cost_details": {
"upstream_inference_cost": 0.000232,
"upstream_inference_prompt_cost": 0.000132,
"upstream_inference_completions_cost": 0.0001
}
}
OpenCode reads usage.cost (0), treats 0 as authoritative, and persists cost = 0 in session_message and session_v2.
Additional Context
- Introduced by #58562 in
v2.2.0(packages/aionly parsesusage.cost). - Up to
v2.1.4, catalog pricing was used and recorded accurate non-zero costs for the same OpenRouter BYOK models.
- Dominant language
- TypeScript
- Stars
- 221k
- Forks
- 29.6k
- Avg merge
- 7h 41m
- Merged PRs (30d)
- 510
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from anomalyco/opencode
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
anomalyco/opencode#60541 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
anomalyco/opencode#60529 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
anomalyco/opencode#60469 · 2 comments ·
Maintainers usually reply within 1 day
-
TUI permission prompt for MCP tools doesn't show the call argumentsPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
anomalyco/opencode#60350 · 2 comments ·
Maintainers usually reply within 1 day
-
glob: literal null and undefined directory names search the workspace rootPossibly taken A pull request linked to this issue is open or already merged. Openreproduced
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
anomalyco/opencode#60306 · 1 comment ·
Maintainers usually reply within 1 day
All issues in anomalyco/opencode
Similar issues
-
area:cli bug P4
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
bug from-studio
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
esengine/DeepSeek-Reasonix#14868 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
loopbackio/loopback-next#11878 ·
Maintainers usually reply within 2 days
-
bug 🐛
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
comunica/comunica#1925 · 1 comment ·
Maintainers usually reply within 1 day