You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
core: OpenRouter BYOK sessions record cost = 0 since v2.2.0 (ignores cost_details.upstream_inference_cost) #60558
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)
Configure openrouter with 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-argon or openrouter/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:
Thanks for the detailed report. I can confirm this on the latest dev (92b2ca0).
The test script below sends OpenRouter's provider in @opencode/ai a mocked stream that ends with your exact BYOK usage chunk (cost: 0, is_byok: true, cost_details.upstream_inference_cost: 0.000232). The parsed usage comes back with cost: 0. cost_details and is_byok are never read.
Session accounting then treats a reported 0 as final. It skips catalog pricing, so the message and session are stored at $0, as you saw. The same code is in v2.2.2; v2.1.4 didn't read usage.cost, which fits your observation that it used catalog prices.
I couldn't run 2.2.2 end to end against a mock OpenRouter endpoint, so this was checked at the protocol level.
Related: #58562 introduced recording the reported cost, and #52629 is an open PR about provider-reported costs on Chat Completions.
Reproduction script (openrouter-byok-repro.ts)
// Run from a checkout of anomalyco/opencode (dev): save as packages/ai/test/provider/openrouter-byok-repro.test.ts and run// `cd packages/ai && bun test test/provider/openrouter-byok-repro.test.ts`. It fails today: Usage.cost is 0.// Reproduction for anomalyco/opencode#60558: OpenRouter BYOK usage reports cost 0 with the real spend in// cost_details.upstream_inference_cost; @opencode/ai surfaces Usage.cost = 0, which core treats as authoritative.import{describe,expect}from"bun:test"import{Effect}from"effect"import{LLM}from"../../src/effect.js"import{LLMClient}from"../../src/route.js"import*asOpenRouterfrom"../../src/providers/openrouter.js"import{it}from"../lib/effect.js"import{fixedResponse}from"../lib/http.js"import{sseEvents}from"../lib/sse.js"describe("OpenRouter BYOK cost (#60558)",()=>{it.effect("usage.cost for a BYOK response",()=>Effect.gen(function*(){constmodel=OpenRouter.configure({apiKey: "test-key"}).model("google/gemini-2.5-flash")constresponse=yield*LLMClient.generate(LLM.request({ model,prompt: "Say hello."})).pipe(Effect.provide(fixedResponse(sseEvents({choices: [{delta: {role: "assistant",content: "Hello"},finish_reason: null}]},{choices: [{delta: {},finish_reason: "stop"}]},{choices: [],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,},},},),),),)console.log("Usage:",JSON.stringify({input: response.usage?.inputTokens,output: response.usage?.outputTokens,cost: response.usage?.cost}))// Expected: the BYOK upstream spend (0.000232) or undefined (so core falls back to catalog pricing).expect(response.usage?.cost).not.toBe(0)}),)})
Activity
opencodejr commentedon Oct 30, 2026
Thanks for the detailed report. I can confirm this on the latest
dev(92b2ca0).The test script below sends OpenRouter's provider in
@opencode/aia mocked stream that ends with your exact BYOK usage chunk (cost: 0,is_byok: true,cost_details.upstream_inference_cost: 0.000232). The parsed usage comes back withcost: 0.cost_detailsandis_byokare never read.Session accounting then treats a reported
0as final. It skips catalog pricing, so the message and session are stored at $0, as you saw. The same code is in v2.2.2; v2.1.4 didn't readusage.cost, which fits your observation that it used catalog prices.I couldn't run 2.2.2 end to end against a mock OpenRouter endpoint, so this was checked at the protocol level.
Related: #58562 introduced recording the reported cost, and #52629 is an open PR about provider-reported costs on Chat Completions.
Reproduction script (openrouter-byok-repro.ts)
Run it:
bun openrouter-byok-repro.ts