feat(billing): map gpt-image-2 quality (low/medium/high) to 1K/2K/4K tier#2269
Open
yingcheng1026 wants to merge 1 commit intoWei-Shaw:mainfrom
Open
feat(billing): map gpt-image-2 quality (low/medium/high) to 1K/2K/4K tier#2269yingcheng1026 wants to merge 1 commit intoWei-Shaw:mainfrom
yingcheng1026 wants to merge 1 commit intoWei-Shaw:mainfrom
Conversation
…tier
The billing layer already reads Group.ImagePrice1K/2K/4K via
BillingService.CalculateImageCost, but normalizeOpenAIImageSizeTier
classified strictly by pixel size, so the 4K price slot was unreachable
for typical 1024x1024 requests even when callers asked for high quality.
For gpt-image-2, image_tokens scale primarily with `quality`
(low ≈ 1k tokens, medium ≈ 2k, high ≈ 4k), not pixel dimensions:
$ curl … -d '{"size":"1024x1024","quality":"low"}'
→ output_tokens=196 (≈ 1K tier)
$ curl … -d '{"size":"1024x1024","quality":"high"}'
→ output_tokens=1756 (≈ 4K tier)
This change makes normalizeOpenAIImageSizeTier accept (size, quality)
and prefer quality-based classification. When quality is empty,
"auto", or unrecognized it falls back to the existing size-dimension
classification, preserving behaviour for older gpt-image-1 / dall-e
flows and for the OpenAI Responses API image_generation tool path
(image_generation_intent.go now also threads tool["quality"] through).
Tests:
- go build ./...
- go test -tags unit ./internal/service/...
- existing JSON parse case (size=1024x1024 + quality=high) updated
to expect "4K" — its previous "1K" was the billing bug
Verified end-to-end against a live deployment (HTTP 200, usage_logs
shows image_size="4K" total_cost=0.6 for quality=high; image_size="1K"
total_cost=0.2 for quality=low when Group.ImagePrice1K/2K/4K are set).
Contributor
|
Thank you for your contribution! Before we can merge this PR, we need you to sign our Contributor License Agreement (CLA). To sign, please reply with the following comment:
You only need to sign once — it will be valid for all your future contributions to this project. I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The billing layer already reads
Group.ImagePrice1K/2K/4KviaBillingService.CalculateImageCost, butnormalizeOpenAIImageSizeTierclassified strictly by pixel size, so the 4K price slot was unreachable for typical1024x1024requests even when callers asked forquality=high.For
gpt-image-2,image_tokensscale primarily withquality, not pixel dimensions:This PR makes
normalizeOpenAIImageSizeTieraccept(size, quality)and prefer quality-based classification. Whenqualityis empty,"auto", or unrecognized it falls back to the existing size-dimension classification, preserving behaviour for oldergpt-image-1/ dall-e flows and the OpenAI Responses APIimage_generationtool (image_generation_intent.gonow also threadstool[\"quality\"]through).quality=low→1Kquality=medium/auto/ empty →2K(or size-based if size matches)quality=high→4KWhy now
A user reported that requesting
quality=highon gpt-image-2 (which costs 4× the tokens upstream) was billed at 1K tier in their group'sImagePrice1K, undercharging by 3×. Inspectingusage_logsconfirmedimage_size = "1K"for those rows, so the 4K column ongroupswas effectively dead code.Test plan
go build ./...go test -tags unit ./internal/service/...— all greensize=1024x1024+quality=high) updated to expect"4K"(the previous"1K"was the bug)quality=low→usage_logs.image_size = "1K",total_cost = 0.20(groupImagePrice1K = 0.2)quality=high→usage_logs.image_size = "4K",total_cost = 0.60(groupImagePrice4K = 0.6)🤖 Generated with Claude Code