fix: prevent OpenRouter cache calculation with custom model ratios - #1590
Conversation
WalkthroughAdds a helper to detect non-default model ratios, uses it to set an isUsingCustomSettings flag in PostClaudeConsumeQuota, skips OpenRouter cache-creation token calculation when custom settings are used, and guards assignment to avoid negative or excessive cache token deductions. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant QuotaService
participant OpenRouterCache
Client->>QuotaService: PostClaudeConsumeQuota(modelName, priceData, promptTokens)
activate QuotaService
QuotaService->>QuotaService: hasCustomModelRatio(modelName, priceData.ModelRatio)
QuotaService->>QuotaService: isUsingCustomSettings = priceData.UsePrice || hasCustomModelRatio(...)
alt Default settings
QuotaService->>OpenRouterCache: compute maybeCacheCreationTokens
OpenRouterCache-->>QuotaService: maybeCacheCreationTokens
QuotaService->>QuotaService: if maybeCacheCreationTokens >= 0 and promptTokens >= maybeCacheCreationTokens then assign cacheCreationTokens
else Custom settings
QuotaService->>QuotaService: skip cacheCreationTokens calculation
end
QuotaService-->>Client: updated quota accounting
deactivate QuotaService
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~15–20 minutes Assessment against linked issues
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: CodeRabbit UI 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
service/quota.go (1)
256-266: Fix: assert usage.Cost's numeric type before comparing and clamp back-calculated tokensBrief: usage.Cost is declared as
any(dto/openai_response.go:229), so comparingusage.Cost != 0is unreliable. rg shows the only direct comparison is in service/quota.go (around line 259). Fix by safely converting to a numeric value before the comparison and clamp the computed tokens.Locations to update
- dto/openai_response.go:229 —
Cost any \json:"cost,omitempty"`(consider changing tofloat64` or adding a helper accessor)- service/quota.go: ~256-266 — replace the direct
usage.Cost != 0comparison with a safe type assertion/conversion and clamp the resultSuggested patch for service/quota.go:
if relayInfo.ChannelType == constant.ChannelTypeOpenRouter { promptTokens -= cacheTokens isUsingCustomSettings := priceData.UsePrice || hasCustomModelRatio(modelName, priceData.ModelRatio) - if cacheCreationTokens == 0 && priceData.CacheCreationRatio != 1 && usage.Cost != 0 && !isUsingCustomSettings { - maybeCacheCreationTokens := CalcOpenRouterCacheCreateTokens(*usage, priceData) - if maybeCacheCreationTokens >= 0 && promptTokens >= maybeCacheCreationTokens { - cacheCreationTokens = maybeCacheCreationTokens - } - } + // Safely convert usage.Cost (declared as `any`) to a float for the zero check. + var costFloat float64 + switch v := usage.Cost.(type) { + case float64: + costFloat = v + case float32: + costFloat = float64(v) + case int: + costFloat = float64(v) + case int64: + costFloat = float64(v) + case string: + if f, err := strconv.ParseFloat(v, 64); err == nil { + costFloat = f + } + case json.Number: + if f, err := v.Float64(); err == nil { + costFloat = f + } + default: + costFloat = 0 + } + + if cacheCreationTokens == 0 && priceData.CacheCreationRatio != 1 && costFloat != 0 && !isUsingCustomSettings { + maybeCacheCreationTokens := CalcOpenRouterCacheCreateTokens(*usage, priceData) + // Clamp to [0, promptTokens] to avoid negative or excessive deductions + if maybeCacheCreationTokens < 0 { + maybeCacheCreationTokens = 0 + } + if maybeCacheCreationTokens > promptTokens { + maybeCacheCreationTokens = promptTokens + } + cacheCreationTokens = maybeCacheCreationTokens + } promptTokens -= cacheCreationTokens }Notes
- Add imports if needed: strconv and encoding/json.
- Prefer a stronger type for Cost (e.g., change the DTO to
float64or add a centralCostFloat()accessor) to avoid repeated runtime checks.Evidence from verification
- dto/openai_response.go:229 shows
Cost any \json:"cost,omitempty"``- rg found the direct comparison only at service/quota.go:259
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these settings in your CodeRabbit configuration.
📒 Files selected for processing (1)
service/quota.go(2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-06-18T12:20:25.779Z
Learnt from: neotf
PR: QuantumNous/new-api#1120
File: service/quota.go:0-0
Timestamp: 2025-06-18T12:20:25.779Z
Learning: For OpenRouter integration: cacheTokens returned from upstream always belong to [0, promptTokens], meaning cacheTokens will never exceed the original promptTokens value. This constraint ensures that operations like `promptTokens -= cacheTokens` will not result in negative values.
Applied to files:
service/quota.go
🧬 Code Graph Analysis (1)
service/quota.go (1)
setting/ratio_setting/model_ratio.go (1)
GetDefaultModelRatioMap(398-400)
🔇 Additional comments (1)
service/quota.go (1)
258-258: LGTM: Correctly skips OpenRouter cache-creation back-calculation under custom settingsThis aligns with the PR goal to avoid billing conflicts when custom price/model ratios are in effect.
| func hasCustomModelRatio(modelName string, currentRatio float64) bool { | ||
| defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName] | ||
| if !exists { | ||
| return true | ||
| } | ||
| return currentRatio != defaultRatio | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Avoid fragile float equality when detecting custom ratios
Direct float equality can misclassify defaults due to precision drift. Use a tolerance.
func hasCustomModelRatio(modelName string, currentRatio float64) bool {
defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName]
if !exists {
return true
}
- return currentRatio != defaultRatio
+ // Avoid false positives caused by floating-point rounding
+ return math.Abs(currentRatio-defaultRatio) > 1e-9
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func hasCustomModelRatio(modelName string, currentRatio float64) bool { | |
| defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName] | |
| if !exists { | |
| return true | |
| } | |
| return currentRatio != defaultRatio | |
| } | |
| func hasCustomModelRatio(modelName string, currentRatio float64) bool { | |
| defaultRatio, exists := ratio_setting.GetDefaultModelRatioMap()[modelName] | |
| if !exists { | |
| return true | |
| } | |
| // Avoid false positives caused by floating-point rounding | |
| return math.Abs(currentRatio-defaultRatio) > 1e-9 | |
| } |
🤖 Prompt for AI Agents
In service/quota.go around lines 40 to 46, the function uses direct float
equality to detect custom ratios which is fragile; change the comparison to use
a tolerance (epsilon) and treat the ratio as custom when math.Abs(currentRatio -
defaultRatio) > epsilon (choose a small constant like 1e-9 or make it
configurable), ensure math is imported and consider handling NaN/Inf by treating
them as custom if encountered.
|
可以解决一下冲突嘛 |
|
@Calcium-Ion 已解决冲突 |
…stom-ratio-billing fix: prevent OpenRouter cache calculation with custom model ratios
close #1585
fix OpenRouter billing conflict with custom model ratios
the current logic is as follows:
Summary by CodeRabbit
Bug Fixes
Improvements