@@ -474,6 +474,9 @@ async function runAgent(ctx) {
474474 const perTurnMax = Math . max ( 1024 , Math . min ( 32768 , ctx . maxTokens || 8192 ) ) ;
475475 const OUTPUT_TOKEN_BUDGET = perTurnMax * 8 ;
476476 let cumulativeOutputTokens = 0 ;
477+ // [LevelCode] Gateway turns report real money: this run's cost + the wallet balance, both RETAIL
478+ // micro-$ (what the customer paid). BYOK turns never report them and these stay 0/null.
479+ let runCostMicros = 0 ;
477480
478481 // --- M5 auto-verify loop ------------------------------------------------
479482 // When the model wants to finish AND it edited files this run, check its work before handing back:
@@ -578,7 +581,11 @@ async function runAgent(ctx) {
578581 // Report real context usage (input_tokens = how full the transcript is) so the UI can meter + warn.
579582 if ( turn . usage ) {
580583 cumulativeOutputTokens += ( turn . usage . output_tokens || 0 ) ;
581- dbg ( 'usage' , { input : turn . usage . input_tokens , output : turn . usage . output_tokens , cacheRead : turn . usage . cache_read_input_tokens , cumulativeOutput : cumulativeOutputTokens } ) ;
584+ // [LevelCode] The Cloud gateway's credits frame (retail micro-$): accumulate what the RUN
585+ // cost and keep the latest remaining balance for the response bar.
586+ if ( turn . usage . cost_micros != null ) { runCostMicros += turn . usage . cost_micros ; }
587+ if ( turn . usage . credits_remaining_micros != null ) { ctx . credits = turn . usage . credits_remaining_micros ; }
588+ dbg ( 'usage' , { input : turn . usage . input_tokens , output : turn . usage . output_tokens , cacheRead : turn . usage . cache_read_input_tokens , cumulativeOutput : cumulativeOutputTokens , costMicros : turn . usage . cost_micros , creditsLeftMicros : turn . usage . credits_remaining_micros } ) ;
582589 ctx . post ( { type : 'contextUsage' , input : ( turn . usage . input_tokens || 0 ) + ( turn . usage . cache_read_input_tokens || 0 ) + ( turn . usage . cache_creation_input_tokens || 0 ) , output : turn . usage . output_tokens || 0 , limit : ctx . contextLimit || 200000 , model : ctx . model , system : systemTokensEst , tools : TOOLS_TOKENS_EST } ) ;
583590 }
584591
@@ -695,9 +702,14 @@ async function runAgent(ctx) {
695702 }
696703 else { ctx . post ( { type : 'agentError' , message : msg , code } ) ; reason = 'error' ; }
697704 } finally {
698- dbg ( 'agent.done' , { reason, steps : step - 1 , edits : ctx . editCount || 0 , credits : ctx . credits != null ? ctx . credits : null } ) ;
699- // credits: null until the M10 gateway returns real usage; the response bar shows it when present.
700- ctx . post ( { type : 'agentDone' , reason, edits : ctx . editCount || 0 , credits : ctx . credits != null ? ctx . credits : null , maxSteps : ctx . maxSteps } ) ;
705+ dbg ( 'agent.done' , { reason, steps : step - 1 , edits : ctx . editCount || 0 , costMicros : runCostMicros , creditsLeftMicros : ctx . credits != null ? ctx . credits : null } ) ;
706+ // [LevelCode] Gateway runs now carry real money: costMicros = what THIS run cost, credits = the
707+ // remaining balance — both RETAIL micro-$. BYOK runs send neither (null/0) and the bar omits them.
708+ ctx . post ( {
709+ type : 'agentDone' , reason, edits : ctx . editCount || 0 , maxSteps : ctx . maxSteps ,
710+ costMicros : runCostMicros > 0 ? runCostMicros : null ,
711+ credits : ctx . credits != null ? ctx . credits : null
712+ } ) ;
701713 }
702714}
703715
0 commit comments