core: OpenRouter BYOK sessions record cost = 0 since v2.2.0 (ignores cost_details.upstream_inference_cost)

Offen Anfängerfreundlich
#60,558 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@tneemo arbeitet bereits daran.

Seit 30.10.2026.

  • #60588 von @tneemo — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
72/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
typescript
Bereich
backend, databases

Rechercherichtung

Das Issue verweist auf das Parsen der Kosten in packages/ai und ihre Speicherung in session_message und session_v2; beginne damit nachzuverfolgen, wo die Streaming-Nutzungsangabe cost von OpenRouter gelesen wird. Prüfe, wie cost_details.upstream_inference_cost und die Katalogpreise behandelt werden, wenn is_byok true und cost null ist. Erledigt ist die Aufgabe, wenn bei BYOK-Nutzung die Upstream- oder Katalogkosten statt null erfasst werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

reproduced

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

  1. Configure openrouter with BYOK enabled on OpenRouter (e.g. Google AI Studio or OpenAI key).
  2. Run a session in OpenCode >= 2.2.0 (e.g. openrouter/google/gemini-4-argon or openrouter/openai/gpt-6.1-sol).
  3. 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/ai only parses usage.cost).
  • Up to v2.1.4, catalog pricing was used and recorded accurate non-zero costs for the same OpenRouter BYOK models.
Vorherrschende Sprache
TypeScript
Sterne
224k
Forks
30k
Ø Merge
7 Std. 30 Min.
Gemergte PRs (30 T.)
489

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus anomalyco/opencode

Alle Issues in anomalyco/opencode

Ähnliche Issues

Weitere Issues zu TypeScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.