Repository navigation
fix(billing): split image input tokens for gpt-image-2 #2176
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
Changes from 1 commit
59c4efb
913926b
2d29c96
44729de
c40ab32
75d6d88
feb9cbd
b608d99
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 |
|---|---|---|
|
|
@@ -101,6 +101,8 @@ export async function calculateCosts( | |
| webSearchCount: number | null = null, | ||
| organizationId: string | null = null, | ||
|
Comment on lines
115
to
116
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.
Because the new split pricing depends on Useful? React with 👍 / 👎. |
||
| imageQuality?: string, | ||
| reportedImageInputTokens: number | null = null, | ||
| reportedImageOutputTokens: number | null = null, | ||
| ) { | ||
|
Comment on lines
99
to
124
|
||
| // Find the model info - try both base model name and provider model name | ||
| // Strip :region suffix if present (e.g., "deepseek-v3.2:cn-beijing" → "deepseek-v3.2") | ||
|
|
@@ -290,18 +292,30 @@ export async function calculateCosts( | |
| return byResolution[size ?? "default"] ?? byResolution["default"]; | ||
| } | ||
|
|
||
| // Track image input tokens separately (for Google image generation models). | ||
| // Uses imageInputTokensByResolution for per-resolution token counts and | ||
| // imageInputPrice for the per-token price. Falls back to 560 tokens/image | ||
| // with imageInputPrice if no resolution map is present. | ||
| // Track image input tokens separately. For image-output models (e.g. | ||
| // gpt-image-2) we prefer the upstream-reported image_tokens count, since | ||
| // the provider tokenises the input image and bills against it directly. | ||
| // For Google image-generation models we fall back to inputImageCount * | ||
| // imageInputTokensByResolution[size] (or 560/image legacy default). | ||
| const imageInputTokensPerImage = resolveTokensPerImage( | ||
| providerInfo.imageInputTokensByResolution, | ||
| imageSize, | ||
| ); | ||
| const imageInputPricePerToken = providerInfo.imageInputPrice; | ||
| const isImageOutputModel = modelInfo.output?.includes("image") ?? false; | ||
| let imageInputTokens: number | null = null; | ||
| let imageInputCost: Decimal | null = null; | ||
| if (imageInputPricePerToken && inputImageCount > 0) { | ||
| if ( | ||
| imageInputPricePerToken && | ||
| isImageOutputModel && | ||
| reportedImageInputTokens && | ||
| reportedImageInputTokens > 0 | ||
|
Comment on lines
+351
to
+352
|
||
| ) { | ||
| imageInputTokens = reportedImageInputTokens; | ||
| imageInputCost = new Decimal(imageInputTokens) | ||
| .times(imageInputPricePerToken) | ||
| .times(discountMultiplier); | ||
|
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.
When OpenAI/Azure returns Useful? React with 👍 / 👎. |
||
| } else if (imageInputPricePerToken && inputImageCount > 0) { | ||
| const LEGACY_TOKENS_PER_INPUT_IMAGE = 560; | ||
| const tokensPerImage = | ||
| imageInputTokensPerImage ?? LEGACY_TOKENS_PER_INPUT_IMAGE; | ||
|
|
@@ -318,8 +332,18 @@ export async function calculateCosts( | |
| const uncachedPromptTokens = cachedTokens | ||
| ? calculatedPromptTokens - cachedTokens | ||
| : calculatedPromptTokens; | ||
| // For providers whose upstream usage already folds image tokens into | ||
| // prompt_tokens (OpenAI/Azure/xAI on image-output models), subtract them | ||
| // so the image portion isn't billed at both inputPrice and imageInputPrice. | ||
| const promptIncludesImageTokens = | ||
| isImageOutputModel && | ||
| (provider === "openai" || provider === "azure" || provider === "xai"); | ||
| const billableTextPromptTokens = | ||
| promptIncludesImageTokens && imageInputTokens | ||
| ? Math.max(0, uncachedPromptTokens - imageInputTokens) | ||
| : uncachedPromptTokens; | ||
| // inputCost includes both text and image input costs when applicable | ||
| const inputCost = new Decimal(uncachedPromptTokens) | ||
| const inputCost = new Decimal(billableTextPromptTokens) | ||
| .times(inputPrice) | ||
| .times(discountMultiplier) | ||
| .plus(imageInputCost ?? 0); | ||
|
|
@@ -349,11 +373,15 @@ export async function calculateCosts( | |
| if (imageOutputPricePerToken && outputImageCount > 0) { | ||
| const LEGACY_DEFAULT_TOKENS_PER_IMAGE = 1120; | ||
| imageOutputTokens = | ||
| imageOutputTokensPerImage !== undefined | ||
| ? outputImageCount * imageOutputTokensPerImage | ||
| : totalOutputTokens > 0 | ||
| ? totalOutputTokens | ||
| : outputImageCount * LEGACY_DEFAULT_TOKENS_PER_IMAGE; | ||
| isImageOutputModel && | ||
| reportedImageOutputTokens && | ||
| reportedImageOutputTokens > 0 | ||
| ? reportedImageOutputTokens | ||
| : imageOutputTokensPerImage !== undefined | ||
| ? outputImageCount * imageOutputTokensPerImage | ||
| : totalOutputTokens > 0 | ||
| ? totalOutputTokens | ||
| : outputImageCount * LEGACY_DEFAULT_TOKENS_PER_IMAGE; | ||
|
Comment on lines
445
to
+454
|
||
| const textTokens = Math.max(0, totalOutputTokens - imageOutputTokens); | ||
|
|
||
| imageOutputCost = new Decimal(imageOutputTokens) | ||
|
|
||
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.
Azure test is missing
imageOutputTokens,imageOutputCost, andtotalCostassertions.The companion OpenAI test (lines 507–517) checks all six cost breakdown fields plus
totalCost, but the Azure variant only checksimageInputTokens,imageInputCost,inputCost, andoutputCost. This leaves the discount-appliedimageOutputCostand the aggregatetotalCostunverified — which is exactly what the PR description says this test exists to confirm ("the gpt-image-2 image output portion usesreportedImageOutputTokensfor its discounted image-output cost").🛡️ Proposed additions
🤖 Prompt for AI Agents