Repository navigation
fix: prevent double-counting of image count n in billing - #3512
Conversation
Comment out code that modifies price data based on image count. 测试发现,如果是接入阿里百炼平台的qwen-image-2.0系列模型,这边计费的时候会出现 0.2*n*倍率*n的情况,最前面的0.2*n会直接显示为模型价格。 例如: 日志详情 模型价格 $0.600000,专属倍率 0.3 其他详情 大小 1080*1920, 品质 standard, 生成数量 3, 其他倍率 n: 3.000000 计费过程 模型价格:$0.600000 * 专属倍率:0.3 = $0.180000
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
💤 Files with no reviewable changes (1)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughImage billing logic was adjusted so image count Changes
Sequence Diagram(s)sequenceDiagram
rect rgba(200,220,255,0.5)
Client->>ImageHandler: Send image request (n, size, quality)
end
rect rgba(220,255,200,0.5)
ImageHandler->>AliAdapter: Forward/convert request (adapter may provide usage.n)
AliAdapter->>ImageHandler: Return response (usage.imageCount or data length)
end
rect rgba(255,230,200,0.5)
ImageHandler->>DTO: Call GetTokenCountMeta() -> compute sizeRatio * qualityRatio (no n)
ImageHandler->>PriceData: Ensure OtherRatio("n") present (set only if missing)
PriceData->>QuotaCalculator: Compute final quota/price (applies OtherRatios including n once)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@relay/channel/ali/image.go`:
- Around line 58-60: The per-image multiplier `n` was removed from both request-
and response-side updates so PriceData.OtherRatios no longer records image
count; restore a single source-of-truth for `n` by ensuring exactly one
AddOtherRatio("n", ...) call is executed (either when reading
imageRequest.Parameters.N in the request path or when computing results in the
response path) and remove the duplicate commented write, or make both paths
explicitly overwrite the same key via info.PriceData.AddOtherRatio("n",
float64(imageRequest.Parameters.N)) / info.PriceData.AddOtherRatio("n",
float64(result.N)) so quota code (relay_task.go and service/text_quota.go) sees
the correct multiplier. Ensure you reference and update the AddOtherRatio call
used in the functions handling imageRequest and response/result in image.go so
`OtherRatios["n"]` is always set once with the correct value.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 041a4c10-2d53-4283-ab5b-37e00e5287f7
📒 Files selected for processing (1)
relay/channel/ali/image.go
|
不应该直接注释,这样会有逃费的风险,应当在注入图片数量相关的参数的时候,先检测一次是否已经注入过 |
|
@Calcium-Ion 这个应该怎么检测呢,目前的确是有重复乘次数的问题,而且也是客户发现计费不对,因为目前0.20.5倍率的图,出5张图,本应该是0.5元,最终客户被扣费0.250.55=2.5元,目前这是接的qwen-image-2.0 ,我看到这个应该之前是接入z-image,这个我没去测试这个模型有没有问题,不过qwen-image-2.0的确会出现这个问题 |
|
@Calcium-Ion 目前这我发现 情况是模型价格提前就*n了,这边我请求体是: 测试可以请求,只是计费不对,我临时先注释掉img内部的n先用用 |
The previous commit commented out AddOtherRatio("n") in the Ali
adaptor to fix double-counting but this could cause billing evasion
when n is specified via extra["parameters"] instead of request.N.
Root cause: ImagePriceRatio in GetTokenCountMeta() already included
n, AND channel adaptors added OtherRatio("n"), resulting in n²
billing.
Proper fix:
- Remove n from ImagePriceRatio (keep sizeRatio * qualityRatio only)
- In ImageHelper, add default OtherRatio("n") when adaptor hasn't
set one; set fallback tokens to 1 (base unit)
- Restore Ali adaptor's AddOtherRatio("n") — it uses actual upstream
parameters/response count, preventing billing evasion
|
我在你的修改上完善了一下,可以试试是否符合预期 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@relay/image_handler.go`:
- Around line 121-127: The code sets info.PriceData.AddOtherRatio("n",
float64(imageN)) even when imageN (derived from request.N) can be 0, which
yields a zero billing multiplier; change the guard so you only set the "n"
OtherRatio when imageN > 0 (or substitute a safe minimum like 1) — update the
conditional around info.PriceData.AddOtherRatio("n", ...) that checks
info.PriceData.OtherRatios and uses the imageN variable so adapters that didn’t
set "n" don’t introduce a zero multiplier.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 75b43b83-7814-479f-aeff-035ddfd5df46
📒 Files selected for processing (3)
dto/openai_image.gorelay/channel/ali/image.gorelay/image_handler.go
💤 Files with no reviewable changes (1)
- relay/channel/ali/image.go
fix: prevent double-counting of image count n in billing
fix: prevent double-counting of image count n in billing
fix: prevent double-counting of image count n in billing

Summary
Fix double-counting of image count
nin billing for channels that setOtherRatio("n")(e.g. Ali/Bailian qwen-image-2.0 series).Problem
The image count
nwas applied twice in quota calculation:ImagePriceRatioinGetTokenCountMeta()includedn, which multipliedmodelPricebyninModelPriceHelper(price billing), andpromptTokenswas set toninImageHelper(ratio billing).OtherRatio("n"), which multiplied the quota again incalculateTextQuotaSummary.This resulted in n² billing instead of n. For example with
n=3and base price$0.2:$0.2 × 3 = $0.6, then× OtherRatio(3)→ charged 9× base$0.2, then× OtherRatio(3)→ charged 3× base ✓Changes
dto/openai_image.go: RemovenfromImagePriceRatioinGetTokenCountMeta(). Keep onlysizeRatio * qualityRatio(DALL-E size/quality adjustments).relay/image_handler.go: Set fallback tokens to1(base unit) instead ofimageN. Add defaultOtherRatio("n", imageN)only when the adaptor hasn't already set one — ensuresnis applied exactly once across all billing paths.relay/channel/ali/image.go: KeepAddOtherRatio("n")intact (minor comment cleanup only). The Ali adaptor uses actual upstream parameters/response count, which prevents billing evasion viaextra["parameters"].Test plan
n=3: verify model price shows base price (e.g.$0.2), not$0.2 × nnspecified viaextra["parameters"]: verify billing uses actual parameter countnapplied onceSummary by CodeRabbit
Bug Fixes
Refactor