@@ -171,7 +171,7 @@ public AgentResult finalizeOutput(ChatMessage terminal, String outputSchema, Usa
171171 ROOT_PATH , "Configured output schema is not a valid JSON Schema" )));
172172 }
173173
174- AgentResult first = validateAgainst (terminal , schema , usage );
174+ AgentResult first = validateAgainst (terminal , schema , usage , null );
175175 if (first .status () != AgentResult .Status .VALIDATION_FAILED || this .repairClient == null ) {
176176 return first ;
177177 }
@@ -180,39 +180,51 @@ public AgentResult finalizeOutput(ChatMessage terminal, String outputSchema, Usa
180180 // exceeded and ownership lost all propagate as execution stops.
181181 control .assertActive ();
182182 }
183- ChatMessage repaired = attemptRepair (terminal , outputSchema , first .issues (), control );
184- if (repaired == null ) {
185- // The repair re-ask could not be performed: keep the original failure and
186- // mark the distinction from a repair that ran but stayed invalid.
187- List <AgentResult .OutputValidationIssue > marked = new ArrayList <>();
188- marked .add (new AgentResult .OutputValidationIssue (AgentResult .ERROR_CODE_REPAIR_CALL_FAILED , ROOT_PATH ,
189- "The bounded repair re-ask could not be performed or returned no assistant message" ));
190- marked .addAll (first .issues ());
191- return AgentResult .validationFailed (first .message (), first .usage (), List .copyOf (marked ));
183+ RepairAttempt repair = attemptRepair (terminal , outputSchema , first .issues (), control );
184+ if (repair == null ) {
185+ // The repair re-ask could not be performed at all: keep the original failure
186+ // and mark the distinction from a repair that ran but stayed invalid. The
187+ // call was issued yet no response proves what it consumed, so neither the
188+ // segment nor the delivery total may claim it spent nothing: both stay
189+ // unknown instead of the pre-repair usage standing in as an exact total.
190+ return AgentResult .validationFailed (first .message (), Usage .unknown (), Usage .unknown (),
191+ repairCallFailedIssues (first .issues ()));
192+ }
193+ // The repair re-ask is part of the delivery: its reported usage folds into the
194+ // delivered total per field, and an unknown field on either side stays unknown
195+ // instead of being summed as zero — including a main usage that was never
196+ // reported at all.
197+ Usage deliveryUsage = (usage == null ? Usage .unknown () : usage ).plus (repair .reportedUsage ());
198+ if (repair .message () == null ) {
199+ // A response arrived without an assistant message: the call happened and its
200+ // reported usage counts, but there is nothing to re-validate.
201+ return AgentResult .validationFailed (first .message (), deliveryUsage , repair .reportedUsage (),
202+ repairCallFailedIssues (first .issues ()));
192203 }
193204 if (control != null ) {
194205 // Deadline checks are synchronous: a deadline that expired during the
195206 // re-ask stops the delivery here instead of publishing a late output.
196207 control .assertActive ();
197208 }
198- AgentResult second = validateAgainst (repaired , schema , usage );
209+ AgentResult second = validateAgainst (repair . message () , schema , deliveryUsage , repair . reportedUsage () );
199210 if (second .status () != AgentResult .Status .VALIDATION_FAILED ) {
200211 return second ;
201212 }
202213 List <AgentResult .OutputValidationIssue > marked = new ArrayList <>();
203214 marked .add (new AgentResult .OutputValidationIssue (AgentResult .ERROR_CODE_REPAIRED_STILL_INVALID , ROOT_PATH ,
204215 "One bounded repair attempt did not produce a schema-valid output" ));
205216 marked .addAll (second .issues ());
206- return AgentResult .validationFailed (repaired , usage , List .copyOf (marked ));
217+ return AgentResult .validationFailed (repair .message (), deliveryUsage , repair .reportedUsage (),
218+ List .copyOf (marked ));
207219 }
208220
209- private AgentResult validateAgainst (ChatMessage terminal , Schema schema , Usage usage ) {
221+ private AgentResult validateAgainst (ChatMessage terminal , Schema schema , Usage usage , Usage repairUsage ) {
210222 JsonNode root ;
211223 try {
212224 root = MAPPER .readTree (terminal .text ());
213225 }
214226 catch (RuntimeException ex ) {
215- return AgentResult .validationFailed (terminal , usage ,
227+ return AgentResult .validationFailed (terminal , usage , repairUsage ,
216228 List .of (new AgentResult .OutputValidationIssue (AgentResult .ERROR_CODE_INVALID_JSON , ROOT_PATH ,
217229 "Terminal output is not valid JSON" )));
218230 }
@@ -224,17 +236,20 @@ private AgentResult validateAgainst(ChatMessage terminal, Schema schema, Usage u
224236 issues .add (new AgentResult .OutputValidationIssue (AgentResult .ERROR_CODE_SCHEMA_VIOLATION ,
225237 error .getInstanceLocation ().toString (), error .getMessage ()));
226238 }
227- return AgentResult .validationFailed (terminal , usage , List .copyOf (issues ));
239+ return AgentResult .validationFailed (terminal , usage , repairUsage , List .copyOf (issues ));
228240 }
229241
230- return AgentResult .completed (terminal , usage , root );
242+ return AgentResult .completed (terminal , usage , repairUsage , root );
231243 }
232244
233245 /**
234- * One bounded repair re-ask: single-turn, no tools, no graph. Returns the repaired
235- * assistant message, or {@code null} when the repair client itself failed.
246+ * One bounded repair re-ask: single-turn, no tools, no graph. Returns what the re-ask
247+ * produced — the repaired assistant message (or {@code null} when the response carried
248+ * none) together with the usage that response reported, or {@code null} when no
249+ * response was received at all and therefore nothing about the call's consumption
250+ * is known.
236251 */
237- private ChatMessage attemptRepair (ChatMessage terminal , String outputSchema ,
252+ private RepairAttempt attemptRepair (ChatMessage terminal , String outputSchema ,
238253 List <AgentResult .OutputValidationIssue > issues , ExecutionGuard control ) {
239254 try {
240255 String issueSummary = issues .stream ()
@@ -251,10 +266,12 @@ private ChatMessage attemptRepair(ChatMessage terminal, String outputSchema,
251266 GenerationOptions .builder ().toolChoice (ToolChoice .none ()).build (), Map .of ());
252267 ChatResponse response = control == null ? this .repairClient .callBlocking (request )
253268 : callUnderControl (request , control );
254- return response .messages ().stream ()
269+ ChatMessage repaired = response .messages ().stream ()
255270 .filter (message -> message .role () == ChatRole .ASSISTANT )
256271 .findFirst ()
257272 .orElse (null );
273+ Usage reportedUsage = response .usage () == null ? Usage .unknown () : response .usage ();
274+ return new RepairAttempt (repaired , reportedUsage );
258275 }
259276 catch (CancellationException cancelled ) {
260277 if (control == null ) {
@@ -278,6 +295,25 @@ private ChatMessage attemptRepair(ChatMessage terminal, String outputSchema,
278295 }
279296 }
280297
298+ /** The outcome of a repair re-ask that received a response. */
299+ private record RepairAttempt (ChatMessage message , Usage reportedUsage ) {
300+
301+ }
302+
303+ /**
304+ * Marks a repair re-ask that produced no re-validatable message — either no response
305+ * at all or a response without an assistant message — in front of the original
306+ * validation failure.
307+ */
308+ private static List <AgentResult .OutputValidationIssue > repairCallFailedIssues (
309+ List <AgentResult .OutputValidationIssue > original ) {
310+ List <AgentResult .OutputValidationIssue > marked = new ArrayList <>();
311+ marked .add (new AgentResult .OutputValidationIssue (AgentResult .ERROR_CODE_REPAIR_CALL_FAILED , ROOT_PATH ,
312+ "The bounded repair re-ask could not be performed or returned no assistant message" ));
313+ marked .addAll (original );
314+ return List .copyOf (marked );
315+ }
316+
281317 /**
282318 * Performs the repair re-ask bound to the run's guard: a stop raised while the call
283319 * is in flight cancels the pending call, and the cancellation observed while waiting
0 commit comments