-
-
Notifications
You must be signed in to change notification settings - Fork 181
fix(billing): 修复 Gemini 图片生成模型的 IMAGE modality token 计费问题 #664
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
Merged
ding113
merged 1 commit into
ding113:dev
from
sususu98:fix/gemini-3-pro-image-preview-billing
Jan 28, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,9 @@ export type UsageMetrics = { | |
| cache_creation_1h_input_tokens?: number; | ||
| cache_ttl?: "5m" | "1h" | "mixed"; | ||
| cache_read_input_tokens?: number; | ||
| // 图片 modality tokens(从 candidatesTokensDetails/promptTokensDetails 提取) | ||
| input_image_tokens?: number; | ||
| output_image_tokens?: number; | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -1288,6 +1291,71 @@ function extractUsageMetrics(value: unknown): UsageMetrics | null { | |
| hasAny = true; | ||
| } | ||
|
|
||
| // Gemini modality-specific token details (IMAGE/TEXT) | ||
| // candidatesTokensDetails: 输出 token 按 modality 分类 | ||
| const candidatesDetails = usage.candidatesTokensDetails as | ||
| | Array<{ modality?: string; tokenCount?: number }> | ||
| | undefined; | ||
| if (Array.isArray(candidatesDetails) && candidatesDetails.length > 0) { | ||
| let imageTokens = 0; | ||
| let textTokens = 0; | ||
| let hasValidToken = false; | ||
| for (const detail of candidatesDetails) { | ||
| if (typeof detail.tokenCount === "number" && detail.tokenCount > 0) { | ||
| hasValidToken = true; | ||
| const modalityUpper = detail.modality?.toUpperCase(); | ||
| if (modalityUpper === "IMAGE") { | ||
| imageTokens += detail.tokenCount; | ||
| } else { | ||
| textTokens += detail.tokenCount; | ||
| } | ||
| } | ||
| } | ||
| if (imageTokens > 0) { | ||
| result.output_image_tokens = imageTokens; | ||
| hasAny = true; | ||
| } | ||
| if (hasValidToken) { | ||
| // 计算未分类的 TEXT tokens: candidatesTokenCount - details总和 | ||
| // 这些可能是图片生成的内部开销,按 TEXT 价格计费 | ||
| const detailsSum = imageTokens + textTokens; | ||
| const candidatesTotal = | ||
| typeof usage.candidatesTokenCount === "number" ? usage.candidatesTokenCount : 0; | ||
| const unaccountedTokens = Math.max(candidatesTotal - detailsSum, 0); | ||
| result.output_tokens = textTokens + unaccountedTokens; | ||
| hasAny = true; | ||
| } | ||
| } | ||
|
|
||
| // promptTokensDetails: 输入 token 按 modality 分类 | ||
| const promptDetails = usage.promptTokensDetails as | ||
| | Array<{ modality?: string; tokenCount?: number }> | ||
| | undefined; | ||
| if (Array.isArray(promptDetails) && promptDetails.length > 0) { | ||
| let imageTokens = 0; | ||
| let textTokens = 0; | ||
| let hasValidToken = false; | ||
| for (const detail of promptDetails) { | ||
| if (typeof detail.tokenCount === "number" && detail.tokenCount > 0) { | ||
| hasValidToken = true; | ||
| const modalityUpper = detail.modality?.toUpperCase(); | ||
| if (modalityUpper === "IMAGE") { | ||
| imageTokens += detail.tokenCount; | ||
| } else { | ||
| textTokens += detail.tokenCount; | ||
| } | ||
| } | ||
| } | ||
| if (imageTokens > 0) { | ||
| result.input_image_tokens = imageTokens; | ||
| hasAny = true; | ||
| } | ||
| if (hasValidToken) { | ||
| result.input_tokens = textTokens; | ||
| hasAny = true; | ||
| } | ||
| } | ||
|
Comment on lines
+1299
to
+1357
Contributor
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. |
||
|
|
||
| if (typeof usage.output_tokens === "number") { | ||
| result.output_tokens = usage.output_tokens; | ||
| hasAny = true; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { calculateRequestCost } from "@/lib/utils/cost-calculation"; | ||
|
|
||
| describe("calculateRequestCost: image token pricing (Gemini image generation)", () => { | ||
| test("output_image_tokens 应使用 output_cost_per_image_token 计费", () => { | ||
| const cost = calculateRequestCost( | ||
| { output_image_tokens: 2000 }, | ||
| { | ||
| output_cost_per_token: 0.000012, | ||
| output_cost_per_image_token: 0.00012, | ||
| } | ||
| ); | ||
|
|
||
| // 2000 * 0.00012 = 0.24 | ||
| expect(cost.toString()).toBe("0.24"); | ||
| }); | ||
|
|
||
| test("output_image_tokens 未配置 image 价格时应回退到 output_cost_per_token", () => { | ||
| const cost = calculateRequestCost( | ||
| { output_image_tokens: 2000 }, | ||
| { | ||
| output_cost_per_token: 0.000012, | ||
| } | ||
| ); | ||
|
|
||
| // 2000 * 0.000012 = 0.024 | ||
| expect(cost.toString()).toBe("0.024"); | ||
| }); | ||
|
|
||
| test("input_image_tokens 应使用 input_cost_per_image_token 计费", () => { | ||
| const cost = calculateRequestCost( | ||
| { input_image_tokens: 560 }, | ||
| { | ||
| input_cost_per_token: 0.000002, | ||
| input_cost_per_image_token: 0.00000196, | ||
| } | ||
| ); | ||
|
|
||
| // 560 * 0.00000196 = 0.0010976 | ||
| expect(cost.toNumber()).toBeCloseTo(0.0010976, 6); | ||
| }); | ||
|
|
||
| test("input_image_tokens 未配置 image 价格时应回退到 input_cost_per_token", () => { | ||
| const cost = calculateRequestCost( | ||
| { input_image_tokens: 560 }, | ||
| { | ||
| input_cost_per_token: 0.000002, | ||
| } | ||
| ); | ||
|
|
||
| // 560 * 0.000002 = 0.00112 | ||
| expect(cost.toString()).toBe("0.00112"); | ||
| }); | ||
|
|
||
| test("混合响应:text + image tokens 应分别计费", () => { | ||
| const cost = calculateRequestCost( | ||
| { | ||
| input_tokens: 326, | ||
| output_tokens: 340, | ||
| output_image_tokens: 2000, | ||
| }, | ||
| { | ||
| input_cost_per_token: 0.000002, | ||
| output_cost_per_token: 0.000012, | ||
| output_cost_per_image_token: 0.00012, | ||
| } | ||
| ); | ||
|
|
||
| // input: 326 * 0.000002 = 0.000652 | ||
| // output text: 340 * 0.000012 = 0.00408 | ||
| // output image: 2000 * 0.00012 = 0.24 | ||
| // total: 0.000652 + 0.00408 + 0.24 = 0.244732 | ||
| expect(cost.toNumber()).toBeCloseTo(0.244732, 6); | ||
| }); | ||
|
|
||
| test("完整 Gemini image 响应计费示例", () => { | ||
| const cost = calculateRequestCost( | ||
| { | ||
| input_tokens: 326, | ||
| output_tokens: 340, | ||
| output_image_tokens: 2000, | ||
| }, | ||
| { | ||
| input_cost_per_token: 0.000002, | ||
| output_cost_per_token: 0.000012, | ||
| output_cost_per_image_token: 0.00012, | ||
| } | ||
| ); | ||
|
|
||
| // Google 官方价格验证 | ||
| // input: 326 * $0.000002 = $0.000652 | ||
| // output text: 340 * $0.000012 = $0.00408 | ||
| // output image: 2000 * $0.00012 = $0.24 (4K image = 2000 tokens) | ||
| // total: $0.244732 | ||
| expect(cost.toNumber()).toBeCloseTo(0.244732, 6); | ||
| }); | ||
|
Comment on lines
+76
to
+96
Contributor
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. |
||
|
|
||
| test("倍率应同时作用于 image token 费用", () => { | ||
| const cost = calculateRequestCost( | ||
| { output_image_tokens: 2000 }, | ||
| { | ||
| output_cost_per_image_token: 0.00012, | ||
| }, | ||
| 2 | ||
| ); | ||
|
|
||
| // 2000 * 0.00012 * 2 = 0.48 | ||
| expect(cost.toString()).toBe("0.48"); | ||
| }); | ||
|
|
||
| test("output_image_tokens 为 0 时不应产生费用", () => { | ||
| const cost = calculateRequestCost( | ||
| { output_image_tokens: 0 }, | ||
| { | ||
| output_cost_per_image_token: 0.00012, | ||
| } | ||
| ); | ||
|
|
||
| expect(cost.toString()).toBe("0"); | ||
| }); | ||
|
|
||
| test("output_image_tokens 为 undefined 时不应产生费用", () => { | ||
| const cost = calculateRequestCost( | ||
| { output_tokens: 1000 }, | ||
| { | ||
| output_cost_per_token: 0.000012, | ||
| output_cost_per_image_token: 0.00012, | ||
| } | ||
| ); | ||
|
|
||
| // 只计算 output_tokens: 1000 * 0.000012 = 0.012 | ||
| expect(cost.toString()).toBe("0.012"); | ||
| }); | ||
|
|
||
| test("同时有 input_image_tokens 和 output_image_tokens", () => { | ||
| const cost = calculateRequestCost( | ||
| { | ||
| input_image_tokens: 560, | ||
| output_image_tokens: 2000, | ||
| }, | ||
| { | ||
| input_cost_per_image_token: 0.00000196, | ||
| output_cost_per_image_token: 0.00012, | ||
| } | ||
| ); | ||
|
|
||
| // input: 560 * 0.00000196 = 0.0010976 | ||
| // output: 2000 * 0.00012 = 0.24 | ||
| // total: 0.2410976 | ||
| expect(cost.toNumber()).toBeCloseTo(0.2410976, 6); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这里从
candidatesTokensDetails计算出的output_tokens(在 1325 行)是正确的。但是,如果usage对象中也存在output_tokens字段,那么在 1359 行的现有逻辑会无条件地覆盖掉这里计算出的值。这是一个 Bug,会导致计费错误。1359 行的逻辑应该只在candidatesTokensDetails不存在或无效时才执行。请调整代码以修复这个优先级问题。