@@ -57,8 +57,6 @@ interface MutableBucket {
5757 costUsd : number ;
5858 cacheSavingsUsd : number ;
5959 categoryCostUsd : UsageCategoryCost | null ;
60- /** A zero-cost record can still supply a category breakdown. */
61- categorizedRecords : number ;
6260 fastCostUsd : number ;
6361 ultrafastCostUsd : number ;
6462 speedPremiumUsd : number ;
@@ -186,13 +184,7 @@ export class UsageAggregator {
186184 const held = this . #seen. get ( record . dedupeKey ) ;
187185 if ( held !== undefined ) {
188186 this . #duplicatesDropped += 1 ;
189- if ( held . bucket === null ) return false ;
190- const merged = mergeUsageCopies ( held . record , record ) ;
191- if ( merged !== held . record ) {
192- this . #accumulate( held . bucket , held . record , - 1 ) ;
193- this . #accumulate( held . bucket , merged , 1 ) ;
194- held . record = merged ;
195- }
187+ if ( held . bucket !== null ) held . record = mergeUsageCopies ( held . record , record ) ;
196188 return false ;
197189 }
198190 }
@@ -203,7 +195,8 @@ export class UsageAggregator {
203195 this . #outOfWindow += 1 ;
204196 return false ;
205197 }
206- this . #accumulate( bucket , record , 1 ) ;
198+ // Keyed usage is counted in finish(), once its copies are merged.
199+ if ( record . dedupeKey === null ) this . #accumulate( bucket , record ) ;
207200 if ( record . sessionId . length > 0 ) bucket . sessions . add ( record . sessionId ) ;
208201 return true ;
209202 }
@@ -232,40 +225,40 @@ export class UsageAggregator {
232225 return this . #bucketFor( day , hourIndex , record . provider , record . model , source ) ;
233226 }
234227
235- #accumulate( bucket : MutableBucket , record : UsageRecord , sign : 1 | - 1 ) : void {
228+ #accumulate( bucket : MutableBucket , record : UsageRecord ) : void {
236229 const priced = priceUsage ( this . #options. rates , record , this . #options. priceOverrides ) ;
237230
238231 const totals = bucket . totals ;
239- totals . uncachedInputTokens += sign * record . totals . uncachedInputTokens ;
240- totals . cachedInputTokens += sign * record . totals . cachedInputTokens ;
241- totals . cacheCreationTokens += sign * record . totals . cacheCreationTokens ;
242- totals . outputTokens += sign * record . totals . outputTokens ;
243- totals . reasoningTokens += sign * record . totals . reasoningTokens ;
244- bucket . costUsd += sign * priced . costUsd ;
232+ totals . uncachedInputTokens += record . totals . uncachedInputTokens ;
233+ totals . cachedInputTokens += record . totals . cachedInputTokens ;
234+ totals . cacheCreationTokens += record . totals . cacheCreationTokens ;
235+ totals . outputTokens += record . totals . outputTokens ;
236+ totals . reasoningTokens += record . totals . reasoningTokens ;
237+ bucket . costUsd += priced . costUsd ;
245238 if ( priced . categoryCostUsd !== null ) {
246- bucket . categorizedRecords += sign ;
247239 const sum = bucket . categoryCostUsd ;
248240 const add = priced . categoryCostUsd ;
249241 bucket . categoryCostUsd =
250- bucket . categorizedRecords === 0
251- ? null
252- : sum === null
253- ? add
254- : {
255- input : sum . input + sign * add . input ,
256- cacheRead : sum . cacheRead + sign * add . cacheRead ,
257- cacheWrite : sum . cacheWrite + sign * add . cacheWrite ,
258- output : sum . output + sign * add . output ,
259- } ;
242+ sum === null
243+ ? add
244+ : {
245+ input : sum . input + add . input ,
246+ cacheRead : sum . cacheRead + add . cacheRead ,
247+ cacheWrite : sum . cacheWrite + add . cacheWrite ,
248+ output : sum . output + add . output ,
249+ } ;
260250 }
261- if ( record . speed === "fast" ) bucket . fastCostUsd += sign * priced . costUsd ;
262- if ( record . speed === "ultrafast" ) bucket . ultrafastCostUsd += sign * priced . costUsd ;
263- bucket . speedPremiumUsd += sign * priced . speedPremiumUsd ;
264- bucket . cacheSavingsUsd +=
265- sign * cacheSavingsUsd ( this . #options. rates , record , this . #options. priceOverrides ) ;
266- bucket . records += sign ;
267- if ( priced . costSource === "unpriced" ) bucket . unpricedRecords += sign ;
268- if ( priced . costSource === "providerReported" ) bucket . providerReportedRecords += sign ;
251+ if ( record . speed === "fast" ) bucket . fastCostUsd += priced . costUsd ;
252+ if ( record . speed === "ultrafast" ) bucket . ultrafastCostUsd += priced . costUsd ;
253+ bucket . speedPremiumUsd += priced . speedPremiumUsd ;
254+ bucket . cacheSavingsUsd += cacheSavingsUsd (
255+ this . #options. rates ,
256+ record ,
257+ this . #options. priceOverrides ,
258+ ) ;
259+ bucket . records += 1 ;
260+ if ( priced . costSource === "unpriced" ) bucket . unpricedRecords += 1 ;
261+ if ( priced . costSource === "providerReported" ) bucket . providerReportedRecords += 1 ;
269262 }
270263
271264 /** The target's own rate applies, so a provider-specific `rateModel` is dropped. */
@@ -310,7 +303,6 @@ export class UsageAggregator {
310303 costUsd : 0 ,
311304 cacheSavingsUsd : 0 ,
312305 categoryCostUsd : null ,
313- categorizedRecords : 0 ,
314306 fastCostUsd : 0 ,
315307 ultrafastCostUsd : 0 ,
316308 speedPremiumUsd : 0 ,
@@ -326,8 +318,19 @@ export class UsageAggregator {
326318 }
327319
328320 finish ( ) : AggregateResult {
321+ // Accumulate into snapshots so repeated finish() calls do not count keys again.
322+ const snapshots = new Map (
323+ Array . from ( this . #buckets. values ( ) , ( bucket ) => [
324+ bucket ,
325+ { ...bucket , totals : { ...bucket . totals } } ,
326+ ] ) ,
327+ ) ;
328+ for ( const { record, bucket } of this . #seen. values ( ) ) {
329+ if ( bucket !== null ) this . #accumulate( snapshots . get ( bucket ) ! , record ) ;
330+ }
329331 const buckets : UsageBucket [ ] = [ ] ;
330- for ( const [ key , bucket ] of this . #buckets) {
332+ for ( const [ key , original ] of this . #buckets) {
333+ const bucket = snapshots . get ( original ) ! ;
331334 const [ day = "" , hourStart = "" , provider = "" , model = "" , sourcePath = "" ] =
332335 key . split ( "\u0000" ) ;
333336 const category = bucket . categoryCostUsd ;
0 commit comments