@@ -189,13 +189,18 @@ export function CodexConfigButton({
189189
190190 const contextWindow =
191191 usageSummary ?. contextWindow ?? getModelContextWindow ( selectedModelValue ) ;
192- const usageTotal =
193- usageSummary ?. totalTokens != null ? usageSummary . totalTokens : 0 ;
192+ const usedTokens =
193+ usageSummary ?. usedTokens ??
194+ ( usageSummary ?. totalTokens != null ? usageSummary . totalTokens : 0 ) ;
195+ const cappedUsedTokens =
196+ contextWindow != null ? Math . min ( usedTokens , contextWindow ) : usedTokens ;
194197 const remainingPercent =
195198 usageSummary != null && contextWindow != null
196199 ? Math . max (
197200 0 ,
198- Math . round ( ( ( contextWindow - usageTotal ) / contextWindow ) * 100 ) ,
201+ Math . round (
202+ ( ( contextWindow - cappedUsedTokens ) / contextWindow ) * 100 ,
203+ ) ,
199204 )
200205 : null ;
201206
@@ -453,6 +458,7 @@ export default CodexConfigButton;
453458type UsageSummary = {
454459 latest ?: any ;
455460 totalTokens : number ;
461+ usedTokens ?: number ;
456462 contextWindow ?: number ;
457463} ;
458464
@@ -485,6 +491,7 @@ function getCodexUsageSummary(
485491 } ) ;
486492 let latest ;
487493 let totalTokens = 0 ;
494+ let usedTokens : number | undefined ;
488495 let contextWindow : number | undefined ;
489496 let hasAggregate = false ;
490497 for ( const entry of sortedMessages ) {
@@ -501,12 +508,16 @@ function getCodexUsageSummary(
501508 if ( usageData ?. model_context_window != null ) {
502509 contextWindow = usageData . model_context_window ;
503510 }
511+ const turnUsed = calcUsedTokens ( usageData ) ;
512+ if ( turnUsed != null ) {
513+ usedTokens = turnUsed ;
514+ }
504515 latest = usageData ;
505516 }
506517 if ( ! latest && totalTokens === 0 ) {
507518 return undefined ;
508519 }
509- return { latest, totalTokens, contextWindow } ;
520+ return { latest, totalTokens, usedTokens , contextWindow } ;
510521}
511522
512523function getMessageByKey ( map , key : string ) : ChatMessageTyped | undefined {
@@ -586,6 +597,24 @@ function renderOptionWithDescription({
586597 ) ;
587598}
588599
600+ function calcUsedTokens ( usage : any ) : number | undefined {
601+ if ( ! usage || typeof usage !== "object" ) return undefined ;
602+ const keys = [
603+ "input_tokens" ,
604+ "cached_input_tokens" ,
605+ "output_tokens" ,
606+ "reasoning_output_tokens" ,
607+ ] as const ;
608+ let total = 0 ;
609+ for ( const key of keys ) {
610+ const value = usage [ key ] ;
611+ if ( typeof value === "number" && Number . isFinite ( value ) ) {
612+ total += value ;
613+ }
614+ }
615+ return total > 0 ? total : undefined ;
616+ }
617+
589618function toMsSafe ( value : any ) : number {
590619 if ( value instanceof Date ) {
591620 const ms = value . valueOf ( ) ;
0 commit comments