Skip to content

fix(vscode): show upstream inference cost for OpenRouter BYOK sessions - #13841

Merged
iscekic merged 1 commit into
mainfrom
kwf/surface-the-vs-code-extensio-5184
Sep 7, 2026
Merged

iscekic merged 1 commit into
mainfrom
kwf/surface-the-vs-code-extensio-5184

Conversation

@iscekic

@iscekic iscekic commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

kwf trial run. This PR came from a workflow trial request, not from a tracked work item. Review it as you would any other PR; the label kwf-trial marks where it came from.

Request

Surface: the VS Code extension (Kilo-Org/kilocode issue 13829).

OpenRouter BYOK sessions always show $0, because providerCost reads usage.cost and ignores usage.cost_details.upstream_inference_cost.

OpenRouter returns, for every response it routes through a BYOK provider key:

"usage": { "cost": 0, "is_byok": true,
           "cost_details": { "upstream_inference_cost": 0.00000445 } }

cost is what OpenRouter charged the account, which is 0 by definition for BYOK routing. The real spend is cost_details.upstream_inference_cost. Reported on 7.5.14 and 7.5.15 with openrouter/z-ai/glm-5.3-flash.

Expected: a BYOK session shows the upstream inference cost, and a non-BYOK session keeps showing usage.cost exactly as it does today.

Rule: reproduce first. If it does not reproduce on main, answer no_change with the evidence.

Changelog for users

Changelog for maintainers

  • The OpenRouter chat-completions branch of the session providerCost helper now prefers costDetails.upstreamInferenceCost over the billed cost whenever the upstream figure is strictly greater; previously upstream was preferred only for the Kilo provider. That one expression is the whole fix — start the review there.
  • Non-BYOK safety: a non-BYOK response always bills the account at least the upstream cost, so upstream > regular is false and usage.cost is returned untouched; unit tests pin both the BYOK and non-BYOK payload shapes.
  • Edge case to review: when a response carries costDetails but no top-level cost, the new expression (regular ?? -Infinity) now returns the upstream figure, where the old code fell through to the token-based estimate. Only reachable if OpenRouter ever omits cost while still reporting cost details; not covered by a test.
  • New unit suite for the OpenRouter chat-completions path covers five cases: BYOK with cost: 0, BYOK where cost is only the routing fee, non-BYOK unchanged, no costDetails unchanged, and the Kilo-provider preference.
  • The transport doc comment in the session cost path now documents the BYOK semantics (account billed $0 or a routing fee; true spend on the user's own key).
  • Verification: the reproduce-first rule was proved live; the run's decisive log excerpts are appended below under their own heading.

E2E proof — log excerpts

[e1] Rule: reproduce first. If it does not reproduce on main, answer no_change w -> pass :: Baseline (e1-baseline.log) fails with Expected: 0.00000445 Received: 0 and Expected: 0.06418625 Received: 0.0032093125; packed (e1-packed.log) reports 10 pass a
/Users/igor/.local/share/kwf/sections/surface-the-vs-code-extensio-5184/e2e-cli/e1-baseline.log
155 |     expect(result.cost).toBe(0.00000445)
                              ^
error: expect(received).toBe(expected)
Expected: 0.00000445
Received: 0
      at <anonymous> (/Users/igor/.local/share/kwf/wt/surface-the-vs-code-extensio-5184/packages/opencode/test/kilocode/provider-cost.test.ts:155:25)
(fail) KiloSession.providerCost — OpenRouter chat completions > uses upstream_inference_cost when OpenRouter reports $0 for BYOK routing [1.51ms]
169 |           },
170 |         },
171 |       },
172 |     })
173 |
174 |     expect(result.cost).toBe(0.06418625)
                              ^
error: expect(received).toBe(expected)
Expected: 0.06418625
Received: 0.0032093125
      at <anonymous> (/Users/igor/.local/share/kwf/wt/surface-the-vs-code-extensio-5184/packages/opencode/test/kilocode/provider-cost.test.ts:174:25)
(fail) KiloSession.providerCost — OpenRouter chat completions > uses upstream_inference_cost when OpenRouter bills only its BYOK routing fee [3.19ms]
 3 pass
 5 filtered out
 2 fail
 5 expect() calls
Ran 5 tests across 1 file. [2.15s]
/Users/igor/.local/share/kwf/sections/surface-the-vs-code-extensio-5184/e2e-cli/e1-packed.log
bun test v1.4.0 (34cbb9a40)
 10 pass
 0 fail
 10 expect() calls
Ran 10 tests across 1 file. [2.10s]

@iscekic iscekic added the kwf-trial Opened by a kwf workflow trial request, not a tracked work item label Sep 7, 2026
@kilo-code-bot

kilo-code-bot Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/opencode/src/kilocode/session/index.ts
  • packages/opencode/test/kilocode/provider-cost.test.ts
Previous Review Summaries (4 snapshots, latest commit 6e32cd0)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 6e32cd0)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/opencode/src/kilocode/session/index.ts
  • packages/opencode/test/kilocode/provider-cost.test.ts

Previous review (commit b57e37c)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/opencode/src/kilocode/session/index.ts
  • packages/opencode/test/kilocode/provider-cost.test.ts

Previous review (commit cb2326d)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/opencode/src/kilocode/session/index.ts
  • packages/opencode/test/kilocode/provider-cost.test.ts

Previous review (commit 04ae572)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/opencode/src/kilocode/session/index.ts
  • packages/opencode/test/kilocode/provider-cost.test.ts

Reviewed by grok-4.6 · Input: 50.5K · Output: 9.6K · Cached: 468.5K

Review guidance: REVIEW.md from base branch main

@iscekic
iscekic marked this pull request as draft September 7, 2026 02:32
@iscekic
iscekic force-pushed the kwf/surface-the-vs-code-extensio-5184 branch from 04ae572 to cb2326d Compare September 7, 2026 02:33
@iscekic iscekic changed the title fix(vscode): show upstream inference cost for OpenRouter BYOK sessions fix(session): show upstream inference cost for OpenRouter BYOK sessions Sep 7, 2026
@iscekic
iscekic marked this pull request as ready for review September 7, 2026 02:48
@iscekic
iscekic marked this pull request as draft September 7, 2026 02:54
@iscekic
iscekic force-pushed the kwf/surface-the-vs-code-extensio-5184 branch from cb2326d to 01f3f7f Compare September 7, 2026 02:54
@iscekic iscekic changed the title fix(session): show upstream inference cost for OpenRouter BYOK sessions fix(cost): prefer upstream inference cost for OpenRouter BYOK sessions Sep 7, 2026
@iscekic
iscekic force-pushed the kwf/surface-the-vs-code-extensio-5184 branch from 01f3f7f to d628dd8 Compare September 7, 2026 02:58
@iscekic iscekic changed the title fix(cost): prefer upstream inference cost for OpenRouter BYOK sessions fix(opencode): show upstream inference cost for OpenRouter BYOK sessions Sep 7, 2026
@iscekic
iscekic force-pushed the kwf/surface-the-vs-code-extensio-5184 branch from d628dd8 to b57e37c Compare September 7, 2026 03:00
@iscekic iscekic changed the title fix(opencode): show upstream inference cost for OpenRouter BYOK sessions fix(vscode): show upstream inference cost for OpenRouter BYOK sessions Sep 7, 2026
@iscekic
iscekic marked this pull request as ready for review September 7, 2026 03:01
@iscekic iscekic added the human-ready PR is ready for a human review. label Sep 7, 2026
@iscekic
iscekic requested a review from chrarnoldus September 7, 2026 03:13
// charged to the user's own key. A non-BYOK response always bills at least the
// upstream cost, so preferring upstream only when it exceeds the billed amount
// never changes non-BYOK sessions.
const cost = upstream !== undefined && (isKilo || upstream > (regular ?? -Infinity)) ? upstream : regular

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think technically it would have to be upstream+regular for OpenRouter BYOK

@iscekic
iscekic force-pushed the kwf/surface-the-vs-code-extensio-5184 branch from 6e32cd0 to 3fe90e8 Compare September 7, 2026 13:00
@iscekic
iscekic enabled auto-merge (squash) September 7, 2026 13:01
@iscekic
iscekic merged commit 91ff1fa into main Sep 7, 2026
31 checks passed
@iscekic
iscekic deleted the kwf/surface-the-vs-code-extensio-5184 branch September 7, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human-ready PR is ready for a human review. kwf-trial Opened by a kwf workflow trial request, not a tracked work item

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants