Skip to content

Commit f740f7a

Browse files
committed
fix(usage): bill cache misses as input, not as cache writes
What: - chat-usage-normalization.ts: split the single write-alias list into CACHE_WRITE_ALIASES (prompt_tokens_details.cache_write_tokens, cache_creation_input_tokens) and CACHE_MISS_ALIASES (prompt_cache_miss_tokens, prompt_tokens_details.cache_miss_tokens), and report the miss counter as a new NormalizedChatUsage.cacheMissTokens field instead of folding it into cacheWriteTokens. - deepseek.ts: DeepSeekHandler.processUsageMetrics opts in to the miss counter (cacheWriteTokens ?? cacheMissTokens). It is the only handler that does, because every deepSeekModels row sets inputPrice: 0 by design. - cost.ts: cache prices fall back to inputPrice when the catalog omits them, via (cacheWritesPrice ?? inputPrice) || 0. The inner ?? honors an explicit zero price; the outer || 0 keeps a fully unpriced model at 0 instead of NaN. Why: prompt_cache_miss_tokens was aliased to cacheWriteTokens for every provider. calculateApiCostOpenAI subtracts cache writes from the input pool, so a miss left the input bucket and was re-priced at a cache-write rate most catalogs leave unset, billing it at zero. The rule already existed in-repo: openai-native.ts:154 states "missFromDetails is NOT used as fallback for cache writes". This makes the shared normalizer agree. The alias was introduced inside this stack by c6773df; origin/main has no chat-usage-normalization.ts and its openai.ts reads only cache_creation_input_tokens, so this is a regression this PR created. Impact: - Non-DeepSeek providers (baseten, fireworks, friendli, sambanova, zai, openai, kimi-code) bill misses as regular input again. - DeepSeek billing is unchanged: its miss-to-write mapping is preserved. - Custom models with a priced input and no cache price stop under-billing cache tokens (11.7% on the F-01 fixture). - Catalog rows with an explicit cacheWritesPrice: 0 are byte-identical. - Tests now assert totalCost, not just token labels, so a green suite can no longer hide a pricing regression.
1 parent 55922d9 commit f740f7a

8 files changed

Lines changed: 468 additions & 88 deletions

File tree

‎src/api/providers/__tests__/base-openai-compatible-provider.spec.ts‎

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ class TestOpenAiCompatibleProvider extends BaseOpenAiCompatibleProvider<"test-mo
5151
}
5252
}
5353

54+
// A provider whose catalog row publishes an explicit zero cache-write price,
55+
// the shape friendli and moonshot ship. It exists so the miss-token path can be
56+
// priced end to end rather than only asserted as a token label.
57+
const ZERO_WRITE_PRICE_MODEL_INFO: ModelInfo = {
58+
maxTokens: 4096,
59+
contextWindow: 128000,
60+
supportsImages: false,
61+
supportsPromptCache: true,
62+
inputPrice: 1.4,
63+
outputPrice: 4.4,
64+
cacheWritesPrice: 0,
65+
cacheReadsPrice: 0.26,
66+
}
67+
68+
class ZeroWritePriceProvider extends BaseOpenAiCompatibleProvider<"zero-write"> {
69+
constructor(apiKey: string) {
70+
super({
71+
providerName: "ZeroWriteProvider",
72+
baseURL: "https://test.example.com/v1",
73+
defaultProviderModelId: "zero-write",
74+
providerModels: { "zero-write": ZERO_WRITE_PRICE_MODEL_INFO },
75+
apiKey,
76+
})
77+
}
78+
}
79+
5480
describe("BaseOpenAiCompatibleProvider", () => {
5581
let handler: TestOpenAiCompatibleProvider
5682

@@ -354,23 +380,29 @@ describe("BaseOpenAiCompatibleProvider", () => {
354380
])
355381
})
356382

357-
it("reads the DeepSeek top-level hit and miss counters", async () => {
358-
expect(
359-
await usageChunksFor({
360-
prompt_tokens: 100,
361-
completion_tokens: 50,
362-
prompt_cache_hit_tokens: 20,
363-
prompt_cache_miss_tokens: 30,
364-
}),
365-
).toEqual([
366-
expect.objectContaining({
367-
type: "usage",
368-
inputTokens: 100,
369-
outputTokens: 50,
370-
cacheReadTokens: 20,
371-
cacheWriteTokens: 30,
372-
}),
373-
])
383+
it("bills a DeepSeek-shaped miss counter as ordinary input, not as a cache write", async () => {
384+
// The base class serves baseten/fireworks/friendli/sambanova/zai, none of
385+
// which price misses through the cache-write field. A miss is input that
386+
// `prompt_tokens` already covers, so it must stay in the input bucket.
387+
const [chunk] = await usageChunksFor({
388+
prompt_tokens: 100,
389+
completion_tokens: 50,
390+
prompt_cache_hit_tokens: 20,
391+
prompt_cache_miss_tokens: 30,
392+
})
393+
394+
expect(chunk).toMatchObject({
395+
type: "usage",
396+
inputTokens: 100,
397+
outputTokens: 50,
398+
cacheReadTokens: 20,
399+
})
400+
expect(chunk.cacheWriteTokens).toBeUndefined()
401+
402+
// TEST_MODEL_INFO prices input at 0.5 and output at 1.5 with no cache
403+
// prices, so both the 80 uncached tokens and the 20 reads bill at the
404+
// input price: (100 * 0.5 + 50 * 1.5) / 1e6.
405+
expect(chunk.totalCost).toBeCloseTo(0.000125, 9)
374406
})
375407

376408
it("never sums two shapes that report the same quantity", async () => {
@@ -397,6 +429,41 @@ describe("BaseOpenAiCompatibleProvider", () => {
397429
])
398430
})
399431

432+
it("bills a miss on an explicit-zero cache-write row at the input price", async () => {
433+
// friendli and moonshot publish `cacheWritesPrice: 0`, so the kernel fix
434+
// alone cannot repair this shape: a miss routed to the write bucket is
435+
// still billed at zero. Only keeping misses out of that bucket bills them.
436+
mockCreate.mockImplementationOnce(() =>
437+
asyncStreamFrom([
438+
{
439+
choices: [{ delta: {} }],
440+
usage: {
441+
prompt_tokens: 1000,
442+
completion_tokens: 500,
443+
prompt_cache_hit_tokens: 100,
444+
prompt_cache_miss_tokens: 400,
445+
},
446+
},
447+
]),
448+
)
449+
450+
const chunks = await collectStream(new ZeroWritePriceProvider("test-api-key").createMessage("s", []))
451+
const [chunk] = chunks.filter((c) => c.type === "usage")
452+
453+
expect(chunk.cacheWriteTokens).toBeUndefined()
454+
455+
// The 400 misses stay inside prompt_tokens, so they are priced as input.
456+
// Uncached input: (1.4 / 1_000_000) * 900 = 0.00126
457+
// Cache reads: (0.26 / 1_000_000) * 100 = 0.000026
458+
// Output cost: (4.4 / 1_000_000) * 500 = 0.0022
459+
// Total: 0.00126 + 0.000026 + 0.0022 = 0.003486
460+
expect(chunk.totalCost).toBeCloseTo(0.003486, 9)
461+
462+
// The defect moved those 400 tokens onto the row's zero write price and
463+
// out of the input pool, billing 0.002926: 16.1% short.
464+
expect(chunk.totalCost).not.toBeCloseTo(0.002926, 9)
465+
})
466+
400467
it("reports no cache counters when the provider reports none", async () => {
401468
const [chunk] = await usageChunksFor({ prompt_tokens: 100, completion_tokens: 50 })
402469

‎src/api/providers/__tests__/deepseek.spec.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,16 @@ vi.mock("openai", () => {
131131
import OpenAI from "openai"
132132
import type { Anthropic } from "@anthropic-ai/sdk"
133133

134-
import { deepSeekDefaultModelId, DEEP_SEEK_DEFAULT_TEMPERATURE, type ModelInfo, DeepSeekModelId } from "@roo-code/types"
134+
import {
135+
deepSeekDefaultModelId,
136+
deepSeekModels,
137+
DEEP_SEEK_DEFAULT_TEMPERATURE,
138+
type ModelInfo,
139+
DeepSeekModelId,
140+
} from "@roo-code/types"
135141

136142
import type { ApiHandlerOptions } from "../../../shared/api"
143+
import { calculateApiCostOpenAI } from "../../../shared/cost"
137144

138145
import { DeepSeekHandler, normalizeDeepSeekReasoningEffort } from "../deepseek"
139146

@@ -613,6 +620,62 @@ describe("DeepSeekHandler", () => {
613620
expect(result.cacheWriteTokens).toBeUndefined()
614621
expect(result.cacheReadTokens).toBeUndefined()
615622
})
623+
624+
it("bills a cache miss as a cache write, because the catalog prices input at zero", () => {
625+
// This is the one handler that opts in to the normalizer's miss counter.
626+
// Every deepSeekModels row sets `inputPrice: 0` on purpose ("the inputs
627+
// are priced as cache read/write"), so a miss left in the input bucket
628+
// would be billed at zero. The money is asserted, not just the label:
629+
// a token-label assertion passes whether or not the price is right.
630+
const usage = {
631+
prompt_tokens: 1000,
632+
completion_tokens: 500,
633+
prompt_cache_hit_tokens: 100,
634+
prompt_cache_miss_tokens: 400,
635+
}
636+
637+
const result = processUsage(usage)
638+
639+
expect(result.cacheWriteTokens).toBe(400)
640+
641+
const { totalCost } = calculateApiCostOpenAI(
642+
deepSeekModels["deepseek-flash"],
643+
result.inputTokens,
644+
result.outputTokens,
645+
result.cacheWriteTokens,
646+
result.cacheReadTokens,
647+
)
648+
649+
// Cache writes: (0.3 / 1_000_000) * 400 = 0.00012
650+
// Cache reads: (0.006 / 1_000_000) * 100 = 0.0000006
651+
// Uncached input: (0 / 1_000_000) * 500 = 0
652+
// Output cost: (1.2 / 1_000_000) * 500 = 0.0006
653+
expect(totalCost).toBeCloseTo(0.0007206, 9)
654+
})
655+
656+
it("preserves a reported zero miss counter instead of treating it as unreported", () => {
657+
const result = processUsage({
658+
prompt_tokens: 100,
659+
completion_tokens: 50,
660+
prompt_cache_hit_tokens: 100,
661+
prompt_cache_miss_tokens: 0,
662+
})
663+
664+
expect(result.cacheWriteTokens).toBe(0)
665+
})
666+
667+
it("keeps a genuine write counter ahead of the miss counter", () => {
668+
// A response can report both concepts. The write counter is the more
669+
// specific one, so it wins rather than being shadowed by the miss count.
670+
const result = processUsage({
671+
prompt_tokens: 100,
672+
completion_tokens: 50,
673+
prompt_tokens_details: { cache_write_tokens: 30 },
674+
prompt_cache_miss_tokens: 80,
675+
})
676+
677+
expect(result.cacheWriteTokens).toBe(30)
678+
})
616679
})
617680

618681
describe("interleaved thinking mode", () => {

‎src/api/providers/__tests__/openai-usage-tracking.spec.ts‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import { Anthropic } from "@anthropic-ai/sdk"
44

5+
import type { ModelInfo } from "@roo-code/types"
6+
57
import { ApiHandlerOptions } from "../../../shared/api"
8+
import { calculateApiCostOpenAI } from "../../../shared/cost"
69
import { OpenAiHandler } from "../openai"
710
import { makeApiHandlerOptions } from "../../../test-utils/api"
811
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
@@ -466,14 +469,56 @@ describe("OpenAiHandler with usage tracking fix", () => {
466469
).toEqual([{ type: "usage", inputTokens: 100, outputTokens: 5, cacheWriteTokens: 7 }])
467470
})
468471

469-
it("reads the top-level prompt_cache_miss_tokens shape", async () => {
472+
it("does not bill a top-level prompt_cache_miss_tokens counter as a cache write", async () => {
473+
// A miss is a token that was looked up in the cache and not found, so it
474+
// is ordinary input that prompt_tokens already covers. Reporting it as a
475+
// write would move it off the input price line onto the cache-write line,
476+
// where most catalogs charge nothing. openai-native.ts states the same
477+
// rule: "missFromDetails is NOT used as fallback for cache writes".
470478
expect(
471479
await usageChunksFor({
472-
prompt_tokens: 100,
480+
prompt_tokens: 1000,
473481
completion_tokens: 5,
474-
prompt_cache_miss_tokens: 12,
482+
prompt_cache_miss_tokens: 400,
475483
}),
476-
).toEqual([{ type: "usage", inputTokens: 100, outputTokens: 5, cacheWriteTokens: 12 }])
484+
).toEqual([{ type: "usage", inputTokens: 1000, outputTokens: 5 }])
485+
})
486+
487+
it("leaves the miss tokens in the input pool that Task.ts prices", async () => {
488+
// This handler emits no totalCost; Task.ts recomputes it from the counters
489+
// in the chunk. The chunk is therefore the contract, and the money is
490+
// asserted against it so a label-only regression cannot pass.
491+
const [chunk] = await usageChunksFor({
492+
prompt_tokens: 1000,
493+
completion_tokens: 5,
494+
prompt_cache_miss_tokens: 400,
495+
})
496+
497+
const customModel: ModelInfo = {
498+
maxTokens: 8192,
499+
contextWindow: 200_000,
500+
supportsPromptCache: true,
501+
inputPrice: 3.0,
502+
outputPrice: 15.0,
503+
}
504+
505+
const { totalCost } = calculateApiCostOpenAI(
506+
customModel,
507+
chunk.inputTokens,
508+
chunk.outputTokens,
509+
chunk.cacheWriteTokens,
510+
chunk.cacheReadTokens,
511+
)
512+
513+
// All 1000 prompt tokens bill at the input price, including the 400 misses:
514+
// (3.0 / 1_000_000) * 1000 = 0.003
515+
// Output: (15.0 / 1_000_000) * 5 = 0.000075
516+
// Total: 0.003075
517+
expect(totalCost).toBeCloseTo(0.003075, 9)
518+
519+
// The defect subtracted the 400 misses from the input pool and priced them
520+
// at a cache-write rate the model never declared, billing 0.001875.
521+
expect(totalCost).not.toBeCloseTo(0.001875, 9)
477522
})
478523

479524
it("never sums two write shapes that report the same quantity", async () => {
@@ -490,12 +535,12 @@ describe("OpenAiHandler with usage tracking fix", () => {
490535
expect(chunks).toEqual([{ type: "usage", inputTokens: 100, outputTokens: 5, cacheWriteTokens: 30 }])
491536
})
492537

493-
it("keeps the documented DeepSeek write counter ahead of the Anthropic-style one", async () => {
538+
it("keeps a genuine write counter ahead of the Anthropic-style one", async () => {
494539
expect(
495540
await usageChunksFor({
496541
prompt_tokens: 100,
497542
completion_tokens: 5,
498-
prompt_cache_miss_tokens: 30,
543+
prompt_tokens_details: { cache_write_tokens: 30 },
499544
cache_creation_input_tokens: 99,
500545
}),
501546
).toEqual([{ type: "usage", inputTokens: 100, outputTokens: 5, cacheWriteTokens: 30 }])

‎src/api/providers/deepseek.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ export class DeepSeekHandler extends OpenAiHandler {
232232
// quantities: top-level `prompt_cache_hit_tokens` / `prompt_cache_miss_tokens`
233233
// and nested `prompt_tokens_details.cached_tokens`. The normalizer resolves
234234
// one value per field and never sums the shapes.
235+
//
236+
// This is the only handler that bills cache misses as cache writes, and it
237+
// is the reason the normalizer keeps the two counters apart. Every
238+
// `deepSeekModels` row sets `inputPrice: 0` on purpose ("the inputs are
239+
// priced as cache read/write"), so a miss left in the input bucket would be
240+
// billed at zero. Every other provider prices misses through `inputPrice`,
241+
// which is why they must not opt in here.
235242
// https://api-docs.deepseek.com/api/create-chat-completion
236243
protected override processUsageMetrics(usage: any, _modelInfo?: any): ApiStreamUsageChunk {
237244
const normalized = normalizeChatUsage(usage)
@@ -240,7 +247,7 @@ export class DeepSeekHandler extends OpenAiHandler {
240247
type: "usage",
241248
inputTokens: normalized?.inputTokens ?? 0,
242249
outputTokens: normalized?.outputTokens ?? 0,
243-
cacheWriteTokens: normalized?.cacheWriteTokens,
250+
cacheWriteTokens: normalized?.cacheWriteTokens ?? normalized?.cacheMissTokens,
244251
cacheReadTokens: normalized?.cacheReadTokens,
245252
}
246253
}

0 commit comments

Comments
 (0)