File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -91,15 +91,20 @@ const extractMetric = (value: unknown): number | null => {
9191 return value ;
9292} ;
9393
94- // 缓存命中率 = 缓存读取token / (输入token + 缓存读取token)
95- // 分母用 inputTokens + cachedTokens 而不是 totalTokens,因为 totalTokens 不含缓存token,
96- // 在缓存量很大时(如 147K cached vs 797 total),用 totalTokens 做分母会导致超过 10000% 的荒谬百分比
94+ // 缓存命中率需要兼容新旧两种后端口径:
95+ // 新口径的 inputTokens 已包含 cachedTokens,分母直接用 inputTokens;
96+ // 旧记录的 inputTokens 只包含未缓存输入,分母继续用 inputTokens + cachedTokens。
9797const calculateCacheRate = ( cachedTokens : number , inputTokens : number ) : number | null => {
98- const denominator = inputTokens + Math . max ( cachedTokens , 0 ) ;
98+ const cached = Math . max ( cachedTokens , 0 ) ;
99+ const input = Math . max ( inputTokens , 0 ) ;
100+ if ( input >= cached && input > 0 ) {
101+ return cached / input ;
102+ }
103+ const denominator = input + cached ;
99104 if ( denominator <= 0 ) {
100105 return null ;
101106 }
102- return Math . max ( cachedTokens , 0 ) / denominator ;
107+ return cached / denominator ;
103108} ;
104109
105110const getExportCacheRate = ( value : number | null ) : string => {
You can’t perform that action at this time.
0 commit comments