anomalyco / anomalyco/opencode
[FEATURE] Honor provider-reported cost (usage.cost) from LLM gateways (OpenRouter, LiteLLM, Manifest)
@rekram1-node is already working on this.
Since Aug 21, 2026.
- Dominant language
- TypeScript
- Stars
- 209k
- Forks
- 27.5k
- PR merge metrics
- PR metrics pending
Description
Problem
opencode computes message cost client-side: token counts × models.dev catalog rates in getUsage (packages/opencode/src/session/session.ts). The only provider-reported cost honored today is the hardcoded Copilot providerMetadata.copilot.totalNanoAiu special case.
LLM gateways/routers report the actual USD cost per request in the response usage block, and opencode ignores it:
- OpenRouter:
usage.cost+usage.cost_details(withusage: {include: true}) - LiteLLM:
include_cost_in_streaming_usage→usage.cost - Manifest (mnfst/manifest): stamps
usage.coston proxied OpenAI/Anthropic responses, streaming and non-streaming
For router models (openrouter/auto, manifest/auto, DigitalOcean Inference Routers) local catalog rates can never price the request correctly — the routed upstream model varies per request, so cost shows $0.00 or a wrong amount. Related: #39869, #34877, #40835.
Proposal
- Capture
usage.costinto stepproviderMetadata.@ai-sdk/openai-compatibleparsesusageinto its own schema and drops unknown keys likecost, so this needs either a custom fetch wrapper in opencode's provider layer (same pattern as existing per-provider fetch customizations) or an upstream AI SDK change to forward extrausagefields. - Consume it in
getUsage: generalize the Copilot branch — when provider metadata carries a reported USD cost, prefer it over catalog rates; fall back to local computation otherwise. - No UI changes needed —
assistantMessage.costis already rendered in the TUI; it just needs the right value.
Notes
- Works for streaming (final usage chunk) and non-streaming responses.
- A reported cost of
0is meaningful (flat-fee subscription routed request) and should still win over catalog rates. - Fixes cost display for OpenRouter users today, not just new gateways.
Contributor 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.
Assessment
This issue has not been assessed yet.