Class ToolExecutionRound

java.lang.Object
dev.langchain4j.service.tool.ToolExecutionRound

public final class ToolExecutionRound extends Object
One round of tool execution: the tool calls of one AiMessage, in every AI service mode (blocking, CompletableFuture, TokenStream, reactive). A round is started by ToolService.startRound(ToolServiceContext, InvocationContext, AiServiceListenerRegistrar) and is the single way in and out of executing its calls:

The loop does not need to know that the calls it gets back can differ from the ones of the LLM: an input guardrail can rewrite the arguments of a call, and the rewritten call is then what the tool runs with and what the events report. The history keeps the call as the LLM made it.

A round can start before the AiMessage is known (a streaming model announces each tool call before its final response), so the calls are identified by the requests they were executed with, and the caller passes them, in the order of the LLM, when it completes the round. The tools of a round may run concurrently, so executing and recording calls is thread-safe.

Since:
1.25.0
  • Method Details

    • executeTool

      public GuardedToolExecution executeTool(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution)
      Executes a call of the round, blocking: its tool, through the tool guardrails of the tool if it has any, with the callbacks of the ToolService and the given ones. Every exception that ends the call is thrown (a fatal guardrail failure, an error of the tool that its error handler rethrows, a failing callback), and the round records it, for failBeforeRethrow(List, Throwable, List, ChatMemory, List); a VirtualMachineError is rethrown without being recorded.
      Parameters:
      call - the call of the LLM
      beforeToolExecution - called before the call is executed, with the call of the LLM; may be null
      afterToolExecution - called after the call is executed, with the request it was executed with and its result; may be null
      Returns:
      the request the call was executed with, and its result
    • executeToolAsync

      public CompletableFuture<GuardedToolExecution> executeToolAsync(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution, Executor executor)
      Executes a call of the round on a thread of executor, blocking that thread, as executeTool(ToolExecutionRequest, Consumer, Consumer) does. Nothing is thrown: every exception that ends the call fails the returned future as it was thrown (not wrapped), and the round records it. When executor rejects the call, the call fails with the rejection, so the round reports it like a call that threw.
      Returns:
      the future of the request the call was executed with, and its result
    • startTool

      public CompletableFuture<ToolExecutionResult> startTool(ToolExecutionRequest call, Consumer<BeforeToolExecution> beforeToolExecution, Consumer<ToolExecution> afterToolExecution, Executor executor)
      Starts a call of the round on executor, as executeTool(ToolExecutionRequest, Consumer, Consumer) does, without blocking. Nothing is thrown: every failure fails the returned future, and the round records it. Once the round is canceled, a call that has not started fails with a CancellationException instead of starting: neither its tool nor its callbacks run.
      Returns:
      the future of the result of the call
    • failBeforeRethrow

      public void failBeforeRethrow(List<ToolExecutionRequest> calls, Throwable errorToRethrow, List<ToolService.CompensableToolExecution> compensableExecutions, ChatMemory chatMemory, List<ChatMessage> messages)
      Fails a blocking round whose calls have all ended, when a call failed with an exception that ends the invocation, right before the caller rethrows errorToRethrow, the way the non-blocking modes fail a round: it fires a ToolExecutedEvent for each call that ended, with the request it was executed with, tells cross-agent compensation about the calls that ran successfully and that a call failed, and rolls back the compensable tools that ran successfully in this round and in the earlier rounds, when compensation on tool errors is enabled. A failure of a listener, of cross-agent compensation or of the rollback is added as suppressed to errorToRethrow, and so is the failure of a compensating action, which does not stop the others.
      Parameters:
      calls - the calls of the round, as they were started, in the order of the LLM
      errorToRethrow - the exception the caller rethrows once this method returns
      compensableExecutions - the compensable tools collected across rounds, or null when compensation is disabled
      chatMemory - the chat memory, or null when the AI service has none
      messages - the messages of the invocation, for the rollback when chatMemory is null
    • complete

      Completes the round once every call has been executed, and reports the calls as they were executed with their results. The history is not touched: it keeps the calls as the LLM made them.
      Parameters:
      calls - the calls of the round, as the LLM made them, in its order
    • completeAsync

      Non-blocking counterpart of complete(List), for calls started with startTool(ToolExecutionRequest, Consumer, Consumer, Executor): waits until every call has ended, normally or not, and reports them all. A call that failed is reported with an error result carrying the text of its failure, and the first failure, in the order of the calls, is the ToolExecutionRound.Outcome.firstError() of the round. Also for a round that is canceled.
      Parameters:
      calls - the calls of the round, as the LLM made them, in its order
    • completeAsync

      As completeAsync(List), for calls started before the AiMessage was known (a streaming model announces each tool call before its final response), with requests that can differ from the ones of the AiMessage: a provider can give a call another id in its final response, or format its arguments differently. Each call is executed as it was started, and reported with the request of the AiMessage at its position, which the history keeps, with the arguments it was executed with when an input guardrail rewrote them.
      Parameters:
      calls - the calls of the round, as they were started, in the order of the LLM
      aiMessageCalls - the calls of the AiMessage, in the same order