Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit c8bda59

Browse files
authored
fix: correct cache cost for OpenRouter and other OpenAI-compatible providers (anomalyco#4256)
1 parent c857cff commit c8bda59

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

packages/opencode/src/session/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,22 @@ export namespace Session {
378378
metadata: z.custom<ProviderMetadata>().optional(),
379379
}),
380380
(input) => {
381+
const cachedInputTokens = input.usage.cachedInputTokens ?? 0
382+
const excludesCachedTokens = !!(input.metadata?.["anthropic"] || input.metadata?.["bedrock"])
383+
const adjustedInputTokens = excludesCachedTokens
384+
? (input.usage.inputTokens ?? 0)
385+
: (input.usage.inputTokens ?? 0) - cachedInputTokens
386+
381387
const tokens = {
382-
input: input.usage.inputTokens ?? 0,
388+
input: adjustedInputTokens,
383389
output: input.usage.outputTokens ?? 0,
384390
reasoning: input.usage?.reasoningTokens ?? 0,
385391
cache: {
386392
write: (input.metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
387393
// @ts-expect-error
388394
input.metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
389395
0) as number,
390-
read: input.usage.cachedInputTokens ?? 0,
396+
read: cachedInputTokens,
391397
},
392398
}
393399
return {

0 commit comments

Comments
 (0)