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:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user