Skip to content

Commit 1d2b693

Browse files
committed
fix(usage): correct cache rate denominator and total tokens fallback
calculateCacheRate now uses inputTokens + cachedTokens as denominator instead of totalTokens (which excludes cache read tokens). Also fix extractTotalTokens fallback to not include cachedTokens.
1 parent 47c2782 commit 1d2b693

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/components/usage/RequestEventsDetailsCard.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@ const extractMetric = (value: unknown): number | null => {
9191
return value;
9292
};
9393

94-
const calculateCacheRate = (cachedTokens: number, totalTokens: number): number | null => {
95-
if (totalTokens <= 0) {
94+
// 缓存命中率 = 缓存读取token / (输入token + 缓存读取token)
95+
// 分母用 inputTokens + cachedTokens 而不是 totalTokens,因为 totalTokens 不含缓存token,
96+
// 在缓存量很大时(如 147K cached vs 797 total),用 totalTokens 做分母会导致超过 10000% 的荒谬百分比
97+
const calculateCacheRate = (cachedTokens: number, inputTokens: number): number | null => {
98+
const denominator = inputTokens + Math.max(cachedTokens, 0);
99+
if (denominator <= 0) {
96100
return null;
97101
}
98-
return Math.max(cachedTokens, 0) / totalTokens;
102+
return Math.max(cachedTokens, 0) / denominator;
99103
};
100104

101105
const getExportCacheRate = (value: number | null): string => {
@@ -209,7 +213,7 @@ export function RequestEventsDetailsCard({
209213
);
210214
const firstByteLatencyMs = extractFirstByteLatencyMs(detail.first_byte_latency_ms);
211215
const latencyMs = extractLatencyMs(detail);
212-
const cacheRate = calculateCacheRate(cachedTokens, totalTokens);
216+
const cacheRate = calculateCacheRate(cachedTokens, inputTokens);
213217
const chunkCount = extractMetric(detail.chunk_count);
214218
const responseBytes = extractMetric(detail.response_bytes);
215219
const apiResponseBytes = extractMetric(detail.api_response_bytes);

src/utils/usage.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,15 +711,13 @@ export function extractTotalTokens(detail: unknown): number {
711711
if (typeof tokens.total_tokens === 'number') {
712712
return tokens.total_tokens;
713713
}
714+
// total_tokens 缺失时的兜底计算:只累加 input + output + reasoning,
715+
// 不加 cachedTokens,因为缓存读取是已缓存的旧 token,不是本次消耗的新 token
714716
const inputTokens = typeof tokens.input_tokens === 'number' ? tokens.input_tokens : 0;
715717
const outputTokens = typeof tokens.output_tokens === 'number' ? tokens.output_tokens : 0;
716718
const reasoningTokens = typeof tokens.reasoning_tokens === 'number' ? tokens.reasoning_tokens : 0;
717-
const cachedTokens = Math.max(
718-
typeof tokens.cached_tokens === 'number' ? Math.max(tokens.cached_tokens, 0) : 0,
719-
typeof tokens.cache_tokens === 'number' ? Math.max(tokens.cache_tokens, 0) : 0
720-
);
721719

722-
return inputTokens + outputTokens + reasoningTokens + cachedTokens;
720+
return inputTokens + outputTokens + reasoningTokens;
723721
}
724722

725723
/**

0 commit comments

Comments
 (0)