Skip to content

Commit dd84bda

Browse files
authored
Fix/vertex token count (RooCodeInc#2893)
* feat: add token usage metadata handling in VertexHandler * feat: add cost calculation for API usage in VertexHandler * fix: correct vertexai token count calculation
1 parent 4d8bdf2 commit dd84bda

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

.changeset/pretty-eels-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"claude-dev": patch
3+
---
4+
5+
fix vertexai token count

src/api/providers/vertex.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ApiHandler } from "../"
55
import { ApiHandlerOptions, ModelInfo, vertexDefaultModelId, VertexModelId, vertexModels } from "../../shared/api"
66
import { ApiStream } from "../transform/stream"
77
import { VertexAI } from "@google-cloud/vertexai"
8+
import { calculateApiCostOpenAI } from "../../utils/cost"
89

910
// https://docs.anthropic.com/en/api/claude-on-vertex-ai
1011
export class VertexHandler implements ApiHandler {
@@ -266,6 +267,17 @@ export class VertexHandler implements ApiHandler {
266267
}
267268
}
268269
}
270+
// Handle token usage metadata
271+
const { usageMetadata } = await streamingResult.response
272+
if (usageMetadata) {
273+
const { promptTokenCount = 0, candidatesTokenCount = 0 } = usageMetadata
274+
yield {
275+
type: "usage",
276+
inputTokens: promptTokenCount,
277+
outputTokens: candidatesTokenCount,
278+
totalCost: calculateApiCostOpenAI(model.info, promptTokenCount, candidatesTokenCount, 0, 0),
279+
}
280+
}
269281
}
270282
}
271283

0 commit comments

Comments
 (0)