Skip to content

Commit 3267982

Browse files
anandgupta42claude
andcommitted
fix: count cache writes toward the over-200K price tier
With cache writes no longer folded into `tokens.input`, the long-context tier check missed requests that crossed 200K mostly through cache writes. The threshold now sums uncached input, cache reads and cache writes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent aa24e4d commit 3267982

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎packages/opencode/src/session/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,9 @@ export namespace Session {
938938
}
939939

940940
const costInfo =
941-
input.model.cost?.experimentalOver200K && tokens.input + tokens.cache.read > 200_000
941+
// altimate_change start — upstream_fix: the long-context tier counts all prompt tokens, cache writes included
942+
input.model.cost?.experimentalOver200K && tokens.input + tokens.cache.read + tokens.cache.write > 200_000
943+
// altimate_change end
942944
? input.model.cost.experimentalOver200K
943945
: input.model.cost
944946
return {

‎packages/opencode/test/session/session-getusage.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ describe("Session.getUsage — AI SDK v6 usage (inclusive input with inputTokenD
158158
expect(result.tokens.inputTotal).toBe(31_767)
159159
})
160160

161+
test("cache writes count toward the over-200K price tier", () => {
162+
const tiered = { input: 1, output: 1, cache: { read: 0, write: 1 }, experimentalOver200K: { input: 2, output: 2, cache: { read: 0, write: 2 } } }
163+
const result = Session.getUsage({
164+
model: { ...fakeModel("@ai-sdk/anthropic"), cost: tiered },
165+
usage: { inputTokens: 210_000, inputTokenDetails: { noCacheTokens: 10_000, cacheReadTokens: 0, cacheWriteTokens: 200_000 }, outputTokens: 0 } as any,
166+
metadata: { anthropic: { cacheCreationInputTokens: 200_000 } } as any,
167+
})
168+
// 10K uncached + 200K cache write at the over-200K rates of $2/M each.
169+
expect(result.cost).toBeCloseTo(0.42, 10)
170+
})
171+
161172
test("full cache hit reports zero uncached input", () => {
162173
const result = Session.getUsage({
163174
model: fakeModel("@ai-sdk/anthropic"),

0 commit comments

Comments
 (0)