Class LlmVisibleToolExecutionException

All Implemented Interfaces:
ToolErrorVisibleToLlm, Serializable
Direct Known Subclasses:
McpApplicationErrorException

public class LlmVisibleToolExecutionException extends ToolExecutionException implements ToolErrorVisibleToLlm
A ToolExecutionException whose message is written for the LLM, so it implements ToolErrorVisibleToLlm: error handlers that honor that interface send the message to the LLM instead of failing the AI Service invocation.

It is thrown by tools that LangChain4j provides (for example, skills and tool search) when the LLM can do something about the failure, typically when it called the tool with a missing or invalid argument.

To tell the LLM about a failure in your own @Tool method, throw ToolErrorVisibleToLlm.from(String) or your own exception implementing ToolErrorVisibleToLlm.

Since:
1.21.0
See Also:
  • Constructor Details

    • LlmVisibleToolExecutionException

      public LlmVisibleToolExecutionException(String message)
      Parameters:
      message - the text to send to the LLM, written for the LLM. Must not be blank.
    • LlmVisibleToolExecutionException

      public LlmVisibleToolExecutionException(String message, Throwable cause)
      Parameters:
      message - the text to send to the LLM, written for the LLM. Must not be blank.
      cause - the original error. It is not sent to the LLM.
  • Method Details

    • messageForLlm

      public String messageForLlm()
      Description copied from interface: ToolErrorVisibleToLlm
      The text that is sent to the LLM as the result of the failed tool execution.

      Write it for the LLM, not for a log file: say what went wrong and, when it helps, what the LLM could do about it. Do not pass the message of another exception through (for example return cause.getMessage()): such messages are written for developers and often contain internal details that should not reach the LLM provider.

      Must not be blank. A blank text is ignored (and a warning is logged), and the exception is treated as if it did not implement this interface: a handler that fails the invocation fails it, and a handler that sends the message of the exception to the LLM sends that instead.

      Specified by:
      messageForLlm in interface ToolErrorVisibleToLlm