Class LLMReadableCheckedException

java.lang.Object
java.lang.Throwable
java.lang.Exception
ai.redouble.nucleo.harness.errors.LLMReadableCheckedException
All Implemented Interfaces:
LLMReadable, LLMReadableException, Serializable
Direct Known Subclasses:
CorrectableLLMException, UncorrectableLLMException

public abstract sealed class LLMReadableCheckedException extends Exception implements LLMReadableException permits CorrectableLLMException, UncorrectableLLMException
Base class for exceptions that can be communicated to LLMs in an actionable, understandable form.

This is the mandatory throws type for all LLM-facing code. Tool.execute(JobResources, JobContext) declares throws LLMReadableCheckedException, which means every tool, thinker, and doer must express errors through this hierarchy. Raw exceptions (IOException, SQLException) cannot escape - they must be wrapped via unwrap(Throwable) or wrapWithContext(Throwable, String, String, Object, String).

The two branches define LLM behavior:

  • CorrectableLLMException - The LLM's input was wrong. It may fix its parameters and call the same tool again.
  • UncorrectableLLMException - The tool itself cannot work right now, regardless of input. The LLM should not retry this tool - instead it should try a different tool, take an alternative approach, or explain the limitation to the user.

Subclasses should provide clear, actionable error messages that:

  • Explain what went wrong in plain language
  • Indicate which parameter or input caused the problem
  • Suggest how to fix the issue (valid ranges, formats, etc.)
  • Avoid technical jargon that LLMs might not understand in context

Design Philosophy:

  • Full exception hierarchy (stack traces, causes) preserved for developer debugging
  • Clean, LLM-friendly messages extracted via getLLMMessage()
  • Formatted output for conversation via explainToLLM()
  • No automatic retries - LLM decides all next steps based on error information

IMPORTANT: Do NOT create service-specific exception hierarchies.

NEVER create classes like FooException extends LLMReadableException for a service. NEVER create FooAuthException, FooInvalidInputException, FooServerException, etc. The framework already provides the right concrete types via CorrectableLLMException and UncorrectableLLMException. Every external service should map its HTTP error codes directly to the existing framework types:

HttpExceptions performs that mapping for every status; a client never spells it out.

The hierarchy:

LLMReadable (marker interface)
  +-- LLMReadableException (sealed, extends LLMReadable)
        +-- LLMReadableCheckedException (sealed, checked)
        |     +-- CorrectableLLMException (non-sealed abstract, isCorrectable=true)
        |     |     +-- InvalidInputException, ResourceNotFoundException, GuardrailException, ...
        |     +-- UncorrectableLLMException (non-sealed abstract, isCorrectable=false)
        |           +-- ExternalServiceException, UnauthorizedException, SystemException, ...
        +-- LLMReadableRuntimeException (sealed, unchecked)
              +-- CorrectableRuntimeLLMException (non-sealed concrete, isCorrectable=true)
              +-- UncorrectableRuntimeLLMException (non-sealed concrete, isCorrectable=false)
Since:
0.1 (2026-03-29)
Author:
Andrey Santrosyan
See Also:
  • Constructor Details

    • LLMReadableCheckedException

      public LLMReadableCheckedException(String message)
      Constructs a new LLM-readable exception with the specified detail message.
      Parameters:
      message - the detail message (for logging and debugging)
    • LLMReadableCheckedException

      public LLMReadableCheckedException(String message, Throwable cause)
      Constructs a new LLM-readable exception with the specified detail message and cause.

      The cause is preserved for developer debugging while the LLM message is extracted from the subclass implementation.

      Parameters:
      message - the detail message (for logging and debugging)
      cause - the cause (which is saved for later retrieval by Throwable.getCause())
  • Method Details

    • getLLMMessage

      public abstract String getLLMMessage()
      Returns a clean, actionable error message suitable for LLM understanding.

      This message should:

      • Explain what went wrong in plain language
      • Be specific about which parameter or input caused the problem
      • Suggest how to fix the issue when possible
      • Avoid stack traces, exception types, or code references

      Good examples:

      • "Maximum result limit is 100. Please reduce maxResults parameter to 100 or less."
      • "Parameter 'dateFrom' has invalid format '2024-13-01'. Use ISO 8601 format: YYYY-MM-DD."
      • "Permission denied to access system clock. This tool requires elevated privileges."

      Bad examples:

      • "NullPointerException at line 247"
      • "Error code -1"
      • "Invalid input" (not specific enough)
      Specified by:
      getLLMMessage in interface LLMReadable
      Returns:
      clean, actionable error message for LLM
    • explainToLLM

      public String explainToLLM()
      Returns a fully formatted string to add to the LLM conversation context.

      This declaration restates the interface default: just the LLM message. The four branch classes, CorrectableLLMException, UncorrectableLLMException, CorrectableRuntimeLLMException and UncorrectableRuntimeLLMException, override it to append their correctability hint, and no concrete class overrides it again, so every exception tells the model whether to retry in the same words.

      Specified by:
      explainToLLM in interface LLMReadableException
      Returns:
      formatted string for conversation context
    • isCorrectable

      public abstract boolean isCorrectable()
      Indicates whether the LLM may retry the same tool with different input.

      true (correctable): The LLM's input was wrong. It may fix its parameters and call the same tool again, or take a different approach if correction seems unlikely to help. Examples: invalid query syntax, missing required field, malformed identifier, ID that doesn't exist.

      false (uncorrectable): The tool itself cannot work right now, regardless of what the LLM sends. No amount of parameter tweaking will help. The LLM should not retry this tool - instead it should try a different tool, take an alternative approach, or explain the limitation to the user. Examples: API down, auth failure, rate limit, system error.

      Specified by:
      isCorrectable in interface LLMReadableException
      Returns:
      true if the LLM should retry the same tool with corrected input, false if the LLM should abandon this tool and try something else
    • wrapWithContext

      public static LLMReadableCheckedException wrapWithContext(Throwable t, String serviceName, String paramName, Object paramValue, String operation)
      Wraps a throwable with semantic context, preserving the correctable/uncorrectable distinction.

      Use this in multi-step tools where the LLM needs to know which step failed and why. Catches the throwable and call this method to produce a properly typed exception:

      A live retry signal in the chain is rethrown instead, as unwrap(Throwable) does and by the same rule.

      Example:

      try {
          response = apiUtil.get(path);
      } catch (Throwable t) {
          throw LLMReadableCheckedException.wrapWithContext(t,
              "PubChem", "compoundIdentifier", identifier,
              "CID resolution for " + idType);
      }
      
      Parameters:
      t - the original throwable
      serviceName - the external service name (e.g., "PubChem", "ChEMBL")
      paramName - the LLM-facing parameter that triggered this operation
      paramValue - the value the LLM provided for that parameter
      operation - description of what was being attempted
      Returns:
      an LLMReadableException with context, ready to throw
    • unwrap

      public static LLMReadableCheckedException unwrap(Throwable t)
      Unwraps a throwable to find a buried LLMReadableException exception, or wraps it in a SystemException if none is found.

      Use this when code must only throw LLMReadableCheckedException but calls methods that throw broader exceptions (e.g., ExecutionException from JobHandle.get(), or InterruptedException).

      Walks the cause chain to the first exception that decides the outcome: a dispatcher retry signal or an LLM-readable exception, whichever comes first. A checked LLMReadableCheckedException is returned. A runtime LLMReadableRuntimeException is thrown as itself, keeping its correctable/uncorrectable classification. A chain with neither becomes a SystemException.

      Retry signals. A dispatcher retry signal - every UpstreamRetryException, and the dispatcher's own ResponseCorrectionRetryException and OutputTruncationRetryException - is rethrown as itself when nothing LLM-readable lies above it in the chain, so a broad catch around a job's own model exchange hands the signal to that job's dispatcher loop and never turns a wait the dispatcher knows how to do into a failure. A signal that lies beneath an LLM-readable exception is not rethrown: whoever built that exception held the signal and ruled on it. That is how a child job's failure arrives, its retries spent, its time budget or its cancellation reached, and it travels up as the failure it is, with the signal still readable in its cause chain. The job that awaits the child runs once.

      All call sites use throw LLMReadableCheckedException.unwrap(e), so every path exits with the correct exception type.

      Parameters:
      t - the throwable to unwrap
      Returns:
      an LLMReadableException - either the original buried one, or a new SystemException
      Throws:
      LLMReadableRuntimeException - if a runtime LLM-readable exception is found in the chain