Skip to content

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

Open

Description

@moritzknorr

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

Activity

  1. opencodejr commented on Oct 30, 2026

    @opencodejr
    Contributor

    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 * as OpenRouter from "../../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* () {
          const model = OpenRouter.configure({ apiKey: "test-key" }).model("google/gemini-2.5-flash")
          const response = 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)
        }),
      )
    })

    Run it: bun openrouter-byok-repro.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions