Repository navigation
fix(core): use provider-reported costs when using OpenAI chat protocol - #52629
chris-prince wants to merge 1 commit into
Conversation
Gateways that bill per request (OpenRouter, LiteLLM, Manifest) return the actual USD amount in the response usage block as `usage.cost`. opencode ignored it for every provider except GitHub Copilot and priced each step from token counts times the models.dev catalog rate, so requests served by a different endpoint than the catalog priced showed the wrong cost. Carry the reported amount through the normalized Usage contract and prefer it in session accounting, falling back to catalog pricing when it is absent, negative, NaN or infinite. A reported zero stays authoritative. Refs anomalyco#43818
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: |
|
I think needs:issue is a false alarm. From my understanding, it's a gap between dev branch tooling and v2 branch requirements. Anyway, this Partially Fixes #43818. |
opencode 2.0 is a rewrite; the 1.x patches no longer apply. - openrouter-real-cost: still needed. Rebased onto the source half of upstream anomalyco/opencode#52629 (open): Usage.cost is filled from usage.cost and preferred by SessionUsage.record. - subagent-total-cost: dropped. Upstream 0eaa75ec0 now totals the session-family cost for root sessions in the sidebar. Co-Authored-By: Claude Opus 5.5 <[email protected]>
* chore: weekly flake update - 2026-10-18T19:38:42Z * fix(nexus): update caddy route53 plugin hash Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix: port opencode OpenRouter cost patch to 2.0.32 opencode 2.0 is a rewrite; the 1.x patches no longer apply. - openrouter-real-cost: still needed. Rebased onto the source half of upstream anomalyco/opencode#52629 (open): Usage.cost is filled from usage.cost and preferred by SessionUsage.record. - subagent-total-cost: dropped. Upstream 0eaa75ec0 now totals the session-family cost for root sessions in the sidebar. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Upstream renamed StepRecord's providerState field to native, which broke the context of both publish-llm-event.ts hunks. The change itself is unchanged; anomalyco/opencode#52629 is still unmerged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
* chore: weekly flake update - 2026-10-22T16:41:41Z * fix: rebase opencode openrouter-real-cost patch onto 2.1.1 Upstream renamed StepRecord's providerState field to native, which broke the context of both publish-llm-event.ts hunks. The change itself is unchanged; anomalyco/opencode#52629 is still unmerged. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Partial fix for #43818, covering the OpenAI chat protocol (OpenRouter, LiteLLM, Manifest in OpenAI mode).
Type of change
What does this PR do?
Gateways that bill per request (OpenRouter, LiteLLM, Manifest) already return the actual USD amount as
usage.costin the response usage block. Opencode captured the field but dropped it, so every step was priced from token counts times the models.dev catalog rate.That is wrong for router models (Opencode shows
$0.00because the catalog has no price for a varying upstream; it should show the reported cost) and for OpenRouter (Opencode bills the cheapest listed endpoint; it should bill the endpoint that served the request).This fixes the OpenAI chat protocol path. It carries
usage.costthrough the normalizedUsagecontract (packages/ai/src/schema/events.ts,packages/ai/src/protocols/openai-chat.ts) and makesSessionUsage.recordprefer it when it is finite and non-negative (packages/core/src/session/usage.ts), falling back to catalog pricing otherwise. A reported0stays authoritative. The step settlement now carries the raw usage (packages/core/src/session/runner/publish-llm-event.ts,step.ts) so the reported cost reaches accounting; title and compaction already passed it.Future PRs still need to address the Anthropic-messages path (slightly more complicated) and the other protocol mappers (open-responses, gemini, mistral-chat, bedrock-converse).
How did you verify your code works?
Ran
tsgoforpackages/aiandpackages/core(clean), plus the OpenRouter provider suite (23 pass) and the core usage, tool-events, compaction, title, step and runner suites (28 + 43 pass).Added tests for a streamed OpenRouter
cost, forrecordpreferring a reported cost and falling back on absent/NaN/negative/Infinity, and for the step settlement carryingusage.Screenshots / recordings
Not a UI change.
Checklist