Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/opencode/src/session/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ const layer = Layer.effect(
usage: value.usage ?? new Usage({}),
metadata: value.providerMetadata,
})
const cacheWarning = Session.cacheBustWarning({
providerID: ctx.model.providerID,
modelID: ctx.model.id,
sessionID: ctx.sessionID,
finish: value.reason,
usage,
})
if (cacheWarning) yield* Effect.logWarning("possible Anthropic prompt cache bust", cacheWarning)
ctx.assistantMessage.finish = value.reason
ctx.assistantMessage.cost += usage.cost
ctx.assistantMessage.tokens = usage.tokens
Expand Down
27 changes: 27 additions & 0 deletions packages/opencode/src/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,33 @@ export const getUsage = (input: { model: Provider.Model; usage: Usage; metadata?
}
}

export function cacheBustWarning(input: {
providerID: string
modelID: string
sessionID: string
finish: string
usage: ReturnType<typeof getUsage>
}) {
const cacheWriteTokens = input.usage.tokens.cache.write
const cacheReadTokens = input.usage.tokens.cache.read
if (cacheWriteTokens < 50_000) return undefined
if (cacheReadTokens !== 0) return undefined

const provider = input.providerID.toLowerCase()
const model = input.modelID.toLowerCase()
if (!provider.includes("anthropic") && !model.includes("claude")) return undefined

return {
providerID: input.providerID,
modelID: input.modelID,
sessionID: input.sessionID,
finish: input.finish,
cost: input.usage.cost,
cacheWriteTokens,
cacheReadTokens,
}
}

export class BusyError extends Schema.TaggedErrorClass<BusyError>()("SessionBusyError", {
sessionID: SessionID,
}) {}
Expand Down
38 changes: 38 additions & 0 deletions packages/opencode/test/session/compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,44 @@ describe("SessionNs.getUsage", () => {
expect(result.tokens.cache.write).toBe(300)
})

test("flags likely Anthropic cache busts when a step rewrites cache without reads", () => {
const warning = SessionNs.cacheBustWarning({
providerID: "anthropic",
modelID: "claude-opus-5",
sessionID: "ses_test",
finish: "tool-calls",
usage: {
cost: 1.63,
tokens: { input: 3, output: 39, reasoning: 0, cache: { read: 0, write: 259_313 }, total: 259_355 },
},
})

expect(warning).toEqual({
providerID: "anthropic",
modelID: "claude-opus-5",
sessionID: "ses_test",
finish: "tool-calls",
cost: 1.63,
cacheWriteTokens: 259_313,
cacheReadTokens: 0,
})
})

test("does not flag ordinary cache hits", () => {
const warning = SessionNs.cacheBustWarning({
providerID: "anthropic",
modelID: "claude-opus-5",
sessionID: "ses_test",
finish: "stop",
usage: {
cost: 0.03,
tokens: { input: 3, output: 39, reasoning: 0, cache: { read: 200_000, write: 0 }, total: 200_042 },
},
})

expect(warning).toBeUndefined()
})

test("subtracts cached tokens for anthropic provider", () => {
const model = createModel({ context: 100_000, output: 32_000 })
// AI SDK v6 normalizes inputTokens to include cached tokens for all providers
Expand Down
Loading