@@ -3516,7 +3516,16 @@ func duckUsageCTEFromRaw(
35163516 COALESCE(message_ordinal, -1) ASC
35173517 )
35183518 ELSE 1
3519- END AS snapshot_rank
3519+ END AS snapshot_rank,
3520+ CASE
3521+ WHEN claude_message_id != '' AND claude_request_id != ''
3522+ THEN SUM(output_tokens_norm) OVER (
3523+ PARTITION BY session_id, claude_message_id, claude_request_id
3524+ ) - MAX(output_tokens_norm) OVER (
3525+ PARTITION BY session_id, claude_message_id, claude_request_id
3526+ )
3527+ ELSE 0
3528+ END AS snapshot_deduplicated_output_tokens
35203529 FROM usage_windowed
35213530 ),
35223531 usage_snapshot_survivors AS (
@@ -3640,6 +3649,7 @@ type duckUsageAggregateRow struct {
36403649 reportedCostRows int
36413650 authoritativeCost int64
36423651 authoritativeCostRows int
3652+ snapshotDedupOutput int
36433653}
36443654
36453655type duckSessionUsageRow struct {
@@ -4279,6 +4289,7 @@ func (s *Store) forEachSessionUsageAggregateRow(
42794289 display_name, started_at,
42804290 input_tokens_norm AS input_tokens,
42814291 output_tokens_norm AS output_tokens,
4292+ snapshot_deduplicated_output_tokens,
42824293 cache_create_norm AS cache_creation_tokens,
42834294 cache_read_norm AS cache_read_tokens,
42844295 CASE WHEN cost_microdollars IS NULL OR cost_source = 'copilot-reported' THEN input_tokens_norm ELSE 0 END AS billable_input_tokens,
@@ -4310,7 +4321,8 @@ func (s *Store) forEachSessionUsageAggregateRow(
43104321 & r .sessionID , & r .project , & r .agent , & r .model ,
43114322 & r .priceModel , & r .source , & r .messageOrdinal ,
43124323 & r .displayName , & startedAt ,
4313- & r .inputTok , & r .outputTok , & r .cacheCr , & r .cacheRd ,
4324+ & r .inputTok , & r .outputTok , & r .snapshotDedupOutput ,
4325+ & r .cacheCr , & r .cacheRd ,
43144326 & r .billableInput , & r .billableOutput , & r .billableReason ,
43154327 & r .billableCacheCr , & r .billableCacheRd ,
43164328 & r .billableWebSearch ,
@@ -4617,10 +4629,12 @@ func (s *Store) GetSessionUsage(
46174629 var totalCost money.Money
46184630 var authoritativeCost * money.Money
46194631 var hasComputedCost , hasReportedCost bool
4632+ deduplicatedOutputTokens := 0
46204633 hasRows := false
46214634 err = s .forEachSessionUsageAggregateRow (
46224635 ctx , db.UsageFilter {}, sessionID ,
46234636 func (r duckUsageAggregateRow ) error {
4637+ deduplicatedOutputTokens += r .snapshotDedupOutput
46244638 if r .authoritativeCostRows > 0 {
46254639 v := money.Money {Microdollars : r .authoritativeCost }
46264640 authoritativeCost = & v
@@ -4693,7 +4707,7 @@ func (s *Store) GetSessionUsage(
46934707 }
46944708 out := & db.SessionUsage {
46954709 SessionID : sessionID , Agent : sess .Agent , Project : sess .Project ,
4696- TotalOutputTokens : sess .TotalOutputTokens ,
4710+ TotalOutputTokens : max ( sess .TotalOutputTokens - deduplicatedOutputTokens , 0 ) ,
46974711 PeakContextTokens : sess .PeakContextTokens ,
46984712 HasTokenData : hasRows || sess .HasTotalOutputTokens || sess .HasPeakContextTokens ,
46994713 Models : sortedBoolKeys (models ),
0 commit comments