refactor: isolate claude max response usage simulation by group toggle

This commit is contained in:
erio
2026-02-27 16:14:07 +08:00
parent e71be7e0f1
commit 61ef73cb12
5 changed files with 356 additions and 48 deletions

View File

@@ -31,19 +31,7 @@ func applyClaudeMaxCacheBillingPolicy(input *RecordUsageInput) claudeMaxCacheBil
}
if hasCacheCreationTokens(*usage) {
before5m := usage.CacheCreation5mTokens
before1h := usage.CacheCreation1hTokens
out.ForcedCache1H = safelyForceCacheCreationTo1H(usage)
if out.ForcedCache1H {
logger.LegacyPrintf("service.gateway", "force_claude_max_cache_1h: model=%s account=%d cache_creation_5m:%d->%d cache_creation_1h:%d->%d",
result.Model,
accountID,
before5m,
usage.CacheCreation5mTokens,
before1h,
usage.CacheCreation1hTokens,
)
}
// Upstream already returned cache creation usage; keep original usage.
return out
}
@@ -72,7 +60,7 @@ func detectClaudeMaxCacheBillingOutcomeForUsage(usage ClaudeUsage, parsed *Parse
return out
}
if hasCacheCreationTokens(usage) {
out.ForcedCache1H = true
// Upstream already returned cache creation usage; keep original usage.
return out
}
if shouldSimulateClaudeMaxUsageForUsage(usage, parsed) {
@@ -93,21 +81,7 @@ func applyClaudeMaxCacheBillingPolicyToUsage(usage *ClaudeUsage, parsed *ParsedR
}
if hasCacheCreationTokens(*usage) {
before5m := usage.CacheCreation5mTokens
before1h := usage.CacheCreation1hTokens
changed := safelyForceCacheCreationTo1H(usage)
// Even when value is already 1h, still mark forced to skip account TTL override.
out.ForcedCache1H = true
if changed {
logger.LegacyPrintf("service.gateway", "force_claude_max_cache_1h: model=%s account=%d cache_creation_5m:%d->%d cache_creation_1h:%d->%d",
resolvedModel,
accountID,
before5m,
usage.CacheCreation5mTokens,
before1h,
usage.CacheCreation1hTokens,
)
}
// Upstream already returned cache creation usage; keep original usage.
return out
}