Skip to content

Commit 02d507b

Browse files
Anand Guptaclaude
authored andcommitted
fix(session): count cache writes in the long-context price tier and read detail-only cache reads
Review follow-ups on the cached-token accounting: - the `experimentalOver200K` tier check now uses the whole prompt (uncached + cache read + cache write); with `noCacheTokens` on the Anthropic/Bedrock branch a prompt made of cache writes would otherwise select the base price - read `inputTokenDetails.cacheReadTokens` when `cachedInputTokens` is absent - fail the build with an actionable error if the serde plugin cannot resolve - the build-plugin test reads the probe's stderr and asserts its exit code Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5b02f21 commit 02d507b

4 files changed

Lines changed: 67 additions & 12 deletions

File tree

‎packages/opencode/script/smithy-node-serde-plugin.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ export function smithyNodeSerdePlugin(): BunPlugin {
2626
build.onResolve({ filter: /^@smithy\/core\/serde$/ }, (args) => {
2727
if (!IMPORTER.test(args.importer)) return undefined
2828
// Resolve from the importer's directory with Bun's default (non-browser) conditions.
29-
return { path: Bun.resolveSync("@smithy/core/serde", path.dirname(args.importer)) }
29+
try {
30+
return { path: Bun.resolveSync("@smithy/core/serde", path.dirname(args.importer)) }
31+
} catch (cause) {
32+
throw new Error(
33+
`smithy-node-serde plugin: cannot resolve @smithy/core/serde from ${args.importer}; Bedrock streaming would be empty in the built binary`,
34+
{ cause },
35+
)
36+
}
3037
})
3138
},
3239
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,11 @@ export namespace Session {
856856
const outputTokens = safe(input.usage.outputTokens ?? 0)
857857
const reasoningTokens = safe(input.usage.reasoningTokens ?? 0)
858858

859-
const cacheReadInputTokens = safe(input.usage.cachedInputTokens ?? 0)
859+
// altimate_change start — upstream_fix: SDK usage may carry the read count only in inputTokenDetails.
860+
const sdkDetails = (input.usage as { inputTokenDetails?: { noCacheTokens?: number; cacheReadTokens?: number } })
861+
.inputTokenDetails
862+
const cacheReadInputTokens = safe(input.usage.cachedInputTokens ?? sdkDetails?.cacheReadTokens ?? 0)
863+
// altimate_change end
860864
const cacheWriteInputTokens = safe(
861865
(input.metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
862866
// @ts-expect-error
@@ -886,8 +890,7 @@ export namespace Session {
886890
// convention above) bills every cached token twice. Prefer the explicit uncached count when
887891
// the SDK supplies it, only on the Anthropic/Bedrock branch; every other provider and raw counts
888892
// (no details) keep the previous arithmetic.
889-
const noCacheTokens = (input.usage as { inputTokenDetails?: { noCacheTokens?: number } }).inputTokenDetails
890-
?.noCacheTokens
893+
const noCacheTokens = sdkDetails?.noCacheTokens
891894
const adjustedInputTokens =
892895
excludesCachedTokens && typeof noCacheTokens === "number" && Number.isFinite(noCacheTokens)
893896
? Math.max(0, noCacheTokens)
@@ -924,10 +927,12 @@ export namespace Session {
924927
},
925928
}
926929

930+
// altimate_change start — upstream_fix: the long-context threshold counts the whole prompt, cache writes included.
927931
const costInfo =
928-
input.model.cost?.experimentalOver200K && tokens.input + tokens.cache.read > 200_000
932+
input.model.cost?.experimentalOver200K && tokens.inputTotal > 200_000
929933
? input.model.cost.experimentalOver200K
930934
: input.model.cost
935+
// altimate_change end
931936
return {
932937
cost: safe(
933938
new Decimal(0)

‎packages/opencode/test/script/bedrock-eventstream-build.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ async function bundleAndRun(mode: "plugin" | "none") {
2727
stdout: "pipe",
2828
stderr: "pipe",
2929
})
30-
const out = await new Response(run.stdout).text()
31-
await run.exited
30+
const [out, err] = await Promise.all([new Response(run.stdout).text(), new Response(run.stderr).text()])
31+
expect(await run.exited, err).toBe(0)
3232
return JSON.parse(out.trim().split("\n").pop()!)
3333
} finally {
3434
fs.rmSync(outdir, { recursive: true, force: true })

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

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,57 @@ describe("Session.getUsage - other providers are not changed", () => {
128128
expect(r.tokens.input).toBe(3000)
129129
})
130130

131-
test("raw exclusive counts with no inputTokenDetails (learn/usage path) keep the old behaviour", () => {
131+
test("learn accountUsage without inputTokenDetails keeps the raw-count arithmetic", async () => {
132+
const accounted = await accountUsage(
133+
model("@ai-sdk/amazon-bedrock"),
134+
{ inputTokens: 800, outputTokens: 40, cachedInputTokens: 2000 },
135+
{ bedrock: { usage: { cacheWriteInputTokens: 600 } } } as any,
136+
)
137+
expect(accounted.inputTokens).toBe(3400)
138+
expect(accounted.estimatedCost).toBeCloseTo((800 * 3 + 40 * 15 + 2000 * 0.3 + 600 * 3.75) / 1e6, 12)
139+
})
140+
})
141+
142+
describe("Session.getUsage - details-only cache reads and the long-context tier", () => {
143+
test("cache reads present only in inputTokenDetails are counted", () => {
132144
const r = Session.getUsage({
133145
model: model("@ai-sdk/amazon-bedrock"),
134-
usage: { inputTokens: 800, outputTokens: 40, cachedInputTokens: 2000 } as any,
135-
metadata: { bedrock: { usage: { cacheWriteInputTokens: 600 } } } as any,
146+
usage: {
147+
inputTokens: 61361,
148+
outputTokens: 10,
149+
inputTokenDetails: { noCacheTokens: 2, cacheReadTokens: 61359, cacheWriteTokens: 0 },
150+
} as any,
151+
metadata: { bedrock: { usage: { cacheWriteInputTokens: 0 } } } as any,
152+
})
153+
expect(r.tokens.cache.read).toBe(61359)
154+
expect(r.tokens.inputTotal).toBe(61361)
155+
expect(r.cost).toBeCloseTo((2 * 3 + 10 * 15 + 61359 * 0.3) / 1e6, 12)
156+
})
157+
158+
test("a prompt over 200K made of cache writes selects the over-200K price", () => {
159+
const tiered: any = {
160+
...model("@ai-sdk/anthropic"),
161+
cost: { ...PRICE, experimentalOver200K: { input: 6, output: 22.5, cache: { read: 0.6, write: 7.5 } } },
162+
}
163+
const r = Session.getUsage({
164+
model: tiered,
165+
usage: inclusiveUsage(1, 0, 210_000, 5),
166+
metadata: { anthropic: { cacheCreationInputTokens: 210_000 } } as any,
167+
})
168+
expect(r.cost).toBeCloseTo((1 * 6 + 5 * 22.5 + 210_000 * 7.5) / 1e6, 12)
169+
})
170+
171+
test("a prompt at or under 200K keeps the base price", () => {
172+
const tiered: any = {
173+
...model("@ai-sdk/anthropic"),
174+
cost: { ...PRICE, experimentalOver200K: { input: 6, output: 22.5, cache: { read: 0.6, write: 7.5 } } },
175+
}
176+
const r = Session.getUsage({
177+
model: tiered,
178+
usage: inclusiveUsage(0, 0, 200_000, 5),
179+
metadata: { anthropic: { cacheCreationInputTokens: 200_000 } } as any,
136180
})
137-
expect(r.tokens.input).toBe(800)
138-
expect(r.tokens.inputTotal).toBe(3400)
181+
expect(r.cost).toBeCloseTo((5 * 15 + 200_000 * 3.75) / 1e6, 12)
139182
})
140183
})
141184

0 commit comments

Comments
 (0)