Merge pull request #7701 from Kilo-Org/kirillk/model-price-bug

fix(vscode): correct model price display inflated by double 1M multiplication
This commit is contained in:
Kirill Kalishev
2026-03-26 09:48:56 -04:00
committed by GitHub
3 changed files with 46 additions and 7 deletions
@@ -0,0 +1,36 @@
import { describe, it, expect } from "bun:test"
import { fmtPrice } from "../../webview-ui/src/components/shared/model-preview-utils"
// Prices arriving at fmtPrice are already in $/M tokens (converted by parseApiPrice).
// This test suite guards against the double-multiplication bug where fmtPrice was
// incorrectly multiplying by 1_000_000 again, turning $3.00/1M into $3,000,000.00/1M.
describe("fmtPrice", () => {
it("formats Claude Sonnet 4.6 input price correctly ($3/1M)", () => {
expect(fmtPrice(3)).toBe("$3.00/1M")
})
it("formats Claude Sonnet 4.6 output price correctly ($15/1M)", () => {
expect(fmtPrice(15)).toBe("$15.00/1M")
})
it("returns 'Free' for zero price", () => {
expect(fmtPrice(0)).toBe("Free")
})
it("uses 4 decimal places for sub-cent prices", () => {
expect(fmtPrice(0.005)).toBe("$0.0050/1M")
})
it("uses 2 decimal places at the $0.01 boundary", () => {
expect(fmtPrice(0.01)).toBe("$0.01/1M")
})
it("formats a typical cheap model price ($0.50/1M)", () => {
expect(fmtPrice(0.5)).toBe("$0.50/1M")
})
it("formats a high-cost model price ($75/1M)", () => {
expect(fmtPrice(75)).toBe("$75.00/1M")
})
})
@@ -2,18 +2,12 @@ import { Component, Show, For } from "solid-js"
import type { EnrichedModel } from "../../context/provider"
import { Markdown } from "@kilocode/kilo-ui/markdown"
import { sanitizeName } from "./model-selector-utils"
import { fmtPrice } from "./model-preview-utils"
interface Props {
model: EnrichedModel | null
}
function fmtPrice(n: number): string {
if (n === 0) return "Free"
const per1M = n * 1_000_000
if (per1M < 0.01) return `$${per1M.toFixed(4)}/1M`
return `$${per1M.toFixed(2)}/1M`
}
function fmtContext(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n % 1_000_000 === 0 ? 0 : 1)}M`
if (n >= 1_000) return `${(n / 1_000).toFixed(n % 1_000 === 0 ? 0 : 1)}K`
@@ -0,0 +1,9 @@
/**
* Format a model price for display.
* Expects `n` in $/M tokens (as stored in model.cost.input / model.cost.output).
*/
export function fmtPrice(n: number): string {
if (n === 0) return "Free"
if (n < 0.01) return `$${n.toFixed(4)}/1M`
return `$${n.toFixed(2)}/1M`
}