anomalyco / anomalyco/opencode

[FEATURE] Honor provider-reported cost (usage.cost) from LLM gateways (OpenRouter, LiteLLM, Manifest)

Open
#43,818 1 comment 5 reactions 1 assignee View on GitHub

@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 (with usage: {include: true})
  • LiteLLM: include_cost_in_streaming_usageusage.cost
  • Manifest (mnfst/manifest): stamps usage.cost on 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

  1. Capture usage.cost into step providerMetadata. @ai-sdk/openai-compatible parses usage into its own schema and drops unknown keys like cost, 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 extra usage fields.
  2. 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.
  3. No UI changes needed — assistantMessage.cost is 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 0 is 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.