fix: subtract cached tokens from input in Google and Vertex cost calculation (#2588)

Google's promptTokenCount includes cachedContentTokenCount, so using it
directly as the input token count causes double-counting when calculateCost
multiplies input by the input rate AND cacheRead by the cacheRead rate.

The google-gemini-cli provider already handles this correctly (subtracting
cachedContentTokenCount from promptTokenCount), but google.ts and
google-vertex.ts were using the raw promptTokenCount.

This fix aligns both providers with the google-gemini-cli behavior.
This commit is contained in:
sparkleMing
2026-03-26 05:02:28 +08:00
committed by GitHub
parent de29f65316
commit 6d744f02ef
2 changed files with 2 additions and 2 deletions

View File

@@ -225,7 +225,7 @@ export const streamGoogleVertex: StreamFunction<"google-vertex", GoogleVertexOpt
if (chunk.usageMetadata) {
output.usage = {
input: chunk.usageMetadata.promptTokenCount || 0,
input: (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),
output:
(chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,

View File

@@ -211,7 +211,7 @@ export const streamGoogle: StreamFunction<"google-generative-ai", GoogleOptions>
if (chunk.usageMetadata) {
output.usage = {
input: chunk.usageMetadata.promptTokenCount || 0,
input: (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),
output:
(chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,