-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(gemini): correct token usage accounting #3034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,14 +94,26 @@ public ChatResponse parseResponse(GenerateContentResponse response, Instant star | |
| if (response.usageMetadata().isPresent()) { | ||
| GenerateContentResponseUsageMetadata metadata = response.usageMetadata().get(); | ||
|
|
||
| int inputTokens = metadata.promptTokenCount().orElse(0); | ||
| int inputTokens = | ||
| metadata.promptTokenCount().orElse(0) | ||
| + metadata.toolUsePromptTokenCount().orElse(0); | ||
| int cachedTokens = metadata.cachedContentTokenCount().orElse(0); | ||
| int totalOutputTokens = metadata.candidatesTokenCount().orElse(0); | ||
| int thinkingTokens = metadata.thoughtsTokenCount().orElse(0); | ||
|
|
||
| // Output tokens exclude thinking tokens (following DashScope behavior) | ||
| // In Gemini, candidatesTokenCount includes thinking, so we subtract it | ||
| int outputTokens = totalOutputTokens - thinkingTokens; | ||
| int outputTokens = | ||
| metadata.candidatesTokenCount() | ||
| .map(candidateTokens -> candidateTokens + thinkingTokens) | ||
| .orElseGet( | ||
| () -> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This flips the sign of the previous intentional behaviour (thinking tokens used to be excluded from output). Two consequences worth a explicit note: (1) stored historical usage for Gemini sessions is not comparable across this boundary, and (2) any user-side cost/budget math that added |
||
| metadata.totalTokenCount() | ||
| .map( | ||
| total -> | ||
| Math.max( | ||
| 0, | ||
| total | ||
| - inputTokens)) | ||
| // Without candidate or total counts, | ||
| // thoughts are the only reported output. | ||
| .orElse(thinkingTokens)); | ||
|
|
||
| usage = | ||
| ChatUsage.builder() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Classifying
toolUsePromptTokenCountas input looks right and matches how the other providers bill server-side tool turns. Please confirm the same assumption holds forcachedContentTokenCount: in Gemini the cached count is a subset ofpromptTokenCount, soinputTokensstays correct, but a comment here would prevent a future "fix" from subtracting it twice.