@@ -56,59 +56,80 @@ function parseClaudeRecord(parsed: unknown): readonly UsageRecord[] {
5656 messageId === null && requestId === null ? null : `${ messageId ?? "" } :${ requestId ?? "" } ` ;
5757
5858 const cost = record [ "costUSD" ] ;
59+ const iterations : readonly unknown [ ] = Array . isArray ( usageRecord [ "iterations" ] )
60+ ? usageRecord [ "iterations" ]
61+ : [ ] ;
62+ // After a server-side fallback the top-level usage can name the declined model and merge
63+ // attempts; the serving `fallback_message` iteration holds what its own model billed.
64+ const servingIndex = iterations . findLastIndex (
65+ ( iteration ) => iterationRecord ( iteration ) ?. [ "type" ] === "fallback_message" ,
66+ ) ;
67+ const serving = iterationRecord ( iterations [ servingIndex ] ) ;
5968
6069 const executor : UsageRecord = {
6170 provider : "claude" ,
6271 timestampMs,
63- model,
72+ model : ( serving === null ? null : iterationModel ( serving ) ) ?? model ,
6473 sessionId : typeof record [ "sessionId" ] === "string" ? record [ "sessionId" ] : "" ,
65- totals : {
66- uncachedInputTokens : tokenCount ( usageRecord [ "input_tokens" ] ) ,
67- cachedInputTokens : tokenCount ( usageRecord [ "cache_read_input_tokens" ] ) ,
68- cacheCreationTokens : tokenCount ( usageRecord [ "cache_creation_input_tokens" ] ) ,
69- outputTokens : tokenCount ( usageRecord [ "output_tokens" ] ) ,
70- // Anthropic folds thinking tokens into output and does not break them out.
71- reasoningTokens : 0 ,
72- } ,
74+ totals : usageTotals ( serving ?? usageRecord ) ,
7375 reportedCostUsd : typeof cost === "number" && Number . isFinite ( cost ) ? cost : null ,
7476 speed : usageRecord [ "speed" ] === "fast" ? "fast" : "standard" ,
7577 dedupeKey,
7678 } ;
7779
78- // Top-level `usage` is the executor's alone. Advisor calls run on another model at its own rates
79- // and appear only in `iterations`, so each one is its own record under that model .
80+ // Advisor calls and declined attempts bill at their own model's rates. Anthropic bills a
81+ // declined attempt that produced output; most declines before any output are free .
8082 const records = [ executor ] ;
81- const iterations : readonly unknown [ ] = Array . isArray ( usageRecord [ "iterations" ] )
82- ? usageRecord [ "iterations" ]
83- : [ ] ;
8483 for ( const [ index , iteration ] of iterations . entries ( ) ) {
85- if ( typeof iteration !== "object" || iteration === null ) continue ;
86- const advisor = iteration as Record < string , unknown > ;
87- if ( advisor [ "type" ] !== "advisor_message" ) continue ;
88-
89- const advisorModel = typeof advisor [ "model" ] === "string" ? advisor [ "model" ] : "" ;
90- if ( advisorModel . length === 0 || advisorModel === CLAUDE_SYNTHETIC_MODEL ) continue ;
84+ const entry = iterationRecord ( iteration ) ;
85+ if ( entry === null ) continue ;
86+ const kind =
87+ entry [ "type" ] === "advisor_message"
88+ ? "advisor"
89+ : entry [ "type" ] === "message" &&
90+ index < servingIndex &&
91+ tokenCount ( entry [ "output_tokens" ] ) > 0
92+ ? "attempt"
93+ : null ;
94+ const entryModel = iterationModel ( entry ) ;
95+ if ( kind === null || entryModel === null ) continue ;
9196
9297 records . push ( {
9398 provider : "claude" ,
9499 timestampMs,
95- model : advisorModel ,
100+ model : entryModel ,
96101 sessionId : executor . sessionId ,
97- totals : {
98- uncachedInputTokens : tokenCount ( advisor [ "input_tokens" ] ) ,
99- cachedInputTokens : tokenCount ( advisor [ "cache_read_input_tokens" ] ) ,
100- cacheCreationTokens : tokenCount ( advisor [ "cache_creation_input_tokens" ] ) ,
101- outputTokens : tokenCount ( advisor [ "output_tokens" ] ) ,
102- reasoningTokens : 0 ,
103- } ,
102+ totals : usageTotals ( entry ) ,
104103 reportedCostUsd : null ,
105104 speed : executor . speed ,
106- dedupeKey : dedupeKey === null ? null : `${ dedupeKey } :advisor :${ index } ` ,
105+ dedupeKey : dedupeKey === null ? null : `${ dedupeKey } :${ kind } :${ index } ` ,
107106 } ) ;
108107 }
109108 return records ;
110109}
111110
111+ function iterationRecord ( value : unknown ) : Record < string , unknown > | null {
112+ return typeof value === "object" && value !== null ? ( value as Record < string , unknown > ) : null ;
113+ }
114+
115+ function iterationModel ( iteration : Record < string , unknown > ) : string | null {
116+ const model = iteration [ "model" ] ;
117+ return typeof model === "string" && model . length > 0 && model !== CLAUDE_SYNTHETIC_MODEL
118+ ? model
119+ : null ;
120+ }
121+
122+ function usageTotals ( usage : Record < string , unknown > ) : UsageRecord [ "totals" ] {
123+ return {
124+ uncachedInputTokens : tokenCount ( usage [ "input_tokens" ] ) ,
125+ cachedInputTokens : tokenCount ( usage [ "cache_read_input_tokens" ] ) ,
126+ cacheCreationTokens : tokenCount ( usage [ "cache_creation_input_tokens" ] ) ,
127+ outputTokens : tokenCount ( usage [ "output_tokens" ] ) ,
128+ // Anthropic folds thinking tokens into output and does not break them out.
129+ reasoningTokens : 0 ,
130+ } ;
131+ }
132+
112133export const claudeUsageFormat : TranscriptUsageFormat < void > = {
113134 selectFields : {
114135 type : true ,
0 commit comments