@@ -152,8 +152,19 @@ function cooldownTriggering(entry: PersistedUsageEntry | null, status: number):
152152 return attempts . some ( attempt => attempt . recoveryKinds . some ( kind => COOLDOWN_RECOVERY_KINDS . has ( kind ) ) ) ;
153153}
154154
155- function isSuccessStatus ( status : number ) : boolean {
156- return status >= 200 && status < 400 ;
155+ function successCostUsd (
156+ row : Pick < ScannedRow , "provider" | "model" > ,
157+ entry : PersistedUsageEntry ,
158+ ) : number | null {
159+ if ( ! entry . usage ) return null ;
160+ const estimate = estimateRequestCost ( {
161+ provider : row . provider ,
162+ model : row . model ,
163+ usage : entry . usage ,
164+ usageStatus : entry . usageStatus ,
165+ serviceTier : serviceTierContext ( entry ) ,
166+ } ) ;
167+ return estimate ? estimate . cost . total : null ;
157168}
158169
159170export async function computeRoutingAnalytics (
@@ -227,16 +238,11 @@ export async function computeRoutingAnalytics(
227238 const entry = kind === "success" || row . status >= 400 ? parseEntry ( row . rowJson ) : null ;
228239 if ( cooldownTriggering ( entry , row . status ) ) cooldownFailures += 1 ;
229240
230- if ( kind === "success" && entry ?. usage ) {
231- const estimate = estimateRequestCost ( {
232- provider : row . provider ,
233- model : row . model ,
234- usage : entry . usage ,
235- usageStatus : entry . usageStatus ,
236- serviceTier : serviceTierContext ( entry ) ,
237- } ) ;
238- if ( estimate ) {
239- costTotalUsd += estimate . cost . total ;
241+ let rowCostUsd : number | null = null ;
242+ if ( kind === "success" && entry ) {
243+ rowCostUsd = successCostUsd ( row , entry ) ;
244+ if ( rowCostUsd !== null ) {
245+ costTotalUsd += rowCostUsd ;
240246 costCount += 1 ;
241247 }
242248 }
@@ -265,18 +271,9 @@ export async function computeRoutingAnalytics(
265271 else if ( kind === "failure" ) bucket . failures += 1 ;
266272 else bucket . cancelled += 1 ;
267273 bucket . durations . push ( row . durationMs ) ;
268- if ( kind === "success" && entry ?. usage ) {
269- const estimate = estimateRequestCost ( {
270- provider : row . provider ,
271- model : row . model ,
272- usage : entry . usage ,
273- usageStatus : entry . usageStatus ,
274- serviceTier : serviceTierContext ( entry ) ,
275- } ) ;
276- if ( estimate ) {
277- bucket . costUsdSum += estimate . cost . total ;
278- bucket . costRows += 1 ;
279- }
274+ if ( rowCostUsd !== null ) {
275+ bucket . costUsdSum += rowCostUsd ;
276+ bucket . costRows += 1 ;
280277 }
281278
282279 if ( row . profileId ) {
@@ -307,6 +304,7 @@ export async function computeRoutingAnalytics(
307304
308305 const breakdown : AnalyticsBreakdownRow [ ] = [ ...byKey . values ( ) ] . map ( bucket => {
309306 const sorted = bucket . durations . sort ( ( a , b ) => a - b ) ;
307+ const p50DurationMs = percentile ( sorted , 50 ) ;
310308 return {
311309 provider : bucket . provider ,
312310 model : bucket . model ,
@@ -317,7 +315,7 @@ export async function computeRoutingAnalytics(
317315 failures : bucket . failures ,
318316 cancelled : bucket . cancelled ,
319317 successRate : bucket . requests > 0 ? bucket . successes / bucket . requests : null ,
320- ...( percentile ( sorted , 50 ) !== undefined ? { p50DurationMs : percentile ( sorted , 50 ) } : { } ) ,
318+ ...( p50DurationMs !== undefined ? { p50DurationMs } : { } ) ,
321319 ...( bucket . requests > 0
322320 ? { estimatedCostUsdPerSuccessfulRequest : bucket . costRows > 0
323321 ? bucket . costUsdSum / bucket . costRows
0 commit comments