@@ -151,6 +151,7 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
151151 }
152152
153153 let previousTotals : RawUsage | null = null ;
154+ let currentModel : string | undefined ;
154155 const lines = fileContentResult . value . split ( / \r ? \n / ) ;
155156 for ( const line of lines ) {
156157 const trimmed = line . trim ( ) ;
@@ -173,7 +174,18 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
173174 continue ;
174175 }
175176
176- if ( ( entry . type as string | undefined ) !== 'event_msg' ) {
177+ const entryType = entry . type as string | undefined ;
178+
179+ if ( entryType === 'turn_context' ) {
180+ const contextPayload = entry . payload as Record < string , unknown > | undefined ;
181+ const contextModel = extractModel ( contextPayload ) ;
182+ if ( contextModel != null ) {
183+ currentModel = contextModel ;
184+ }
185+ continue ;
186+ }
187+
188+ if ( entryType !== 'event_msg' ) {
177189 continue ;
178190 }
179191
@@ -214,7 +226,7 @@ export async function loadTokenUsageEvents(options: LoadOptions = {}): Promise<L
214226 continue ;
215227 }
216228
217- const model = extractModel ( { ...payload , info } ) ;
229+ const model = extractModel ( { ...payload , info } ) ?? currentModel ;
218230 if ( model == null ) {
219231 logger . debug ( 'Skipping Codex token event without model metadata' , { file, timestamp } ) ;
220232 continue ;
@@ -244,6 +256,13 @@ if (import.meta.vitest != null) {
244256 await using fixture = await createFixture ( {
245257 sessions : {
246258 'project-1.jsonl' : [
259+ JSON . stringify ( {
260+ timestamp : '2025-09-11T18:25:30.000Z' ,
261+ type : 'turn_context' ,
262+ payload : {
263+ model : 'gpt-5' ,
264+ } ,
265+ } ) ,
247266 JSON . stringify ( {
248267 timestamp : '2025-09-11T18:25:40.670Z' ,
249268 type : 'event_msg' ,
@@ -268,6 +287,13 @@ if (import.meta.vitest != null) {
268287 } ,
269288 } ,
270289 } ) ,
290+ JSON . stringify ( {
291+ timestamp : '2025-09-11T18:40:00.000Z' ,
292+ type : 'turn_context' ,
293+ payload : {
294+ model : 'gpt-5' ,
295+ } ,
296+ } ) ,
271297 JSON . stringify ( {
272298 timestamp : '2025-09-12T00:00:00.000Z' ,
273299 type : 'event_msg' ,
@@ -295,11 +321,15 @@ if (import.meta.vitest != null) {
295321 } ) ;
296322 expect ( missingDirectories ) . toEqual ( [ ] ) ;
297323
298- expect ( events ) . toHaveLength ( 1 ) ;
324+ expect ( events ) . toHaveLength ( 2 ) ;
299325 const first = events [ 0 ] ! ;
300326 expect ( first . model ) . toBe ( 'gpt-5' ) ;
301327 expect ( first . inputTokens ) . toBe ( 1_200 ) ;
302328 expect ( first . cachedInputTokens ) . toBe ( 200 ) ;
329+ const second = events [ 1 ] ! ;
330+ expect ( second . model ) . toBe ( 'gpt-5' ) ;
331+ expect ( second . inputTokens ) . toBe ( 800 ) ;
332+ expect ( second . cachedInputTokens ) . toBe ( 100 ) ;
303333 } ) ;
304334 } ) ;
305335}
0 commit comments