Store Chat Messages & State Without Managing Infrastructure.Check Out DialogueDB
Skip to content

Error Handling

llm-exe uses LlmExeError for errors created by the library.

ts
import {
  LlmExeError,
  isLlmExeError,
  serializeLlmExeError,
  formatLlmExeErrorForLog,
} from "llm-exe";

Error Shape

LlmExeError exposes these fields:

ts
try {
  await executor.execute(input);
} catch (error) {
  if (isLlmExeError(error)) {
    console.log(error.code);
    console.log(error.category);
    console.log(error.context);
    console.log(error.cause);
  }
}
FieldDescription
codeMachine-readable error code, such as parser.parse_failed
categoryThe prefix of the code, such as parser, prompt, or llm
contextExtra metadata about the failure
causeOriginal error, when available

Branching on Codes

Use isLlmExeError(error, code) to check error codes.

ts
try {
  const result = parser.parse(output);
} catch (error) {
  if (isLlmExeError(error, "parser.schema_validation_failed")) {
    // The output parsed, but did not match the schema.
  }

  if (isLlmExeError(error, "parser.parse_failed")) {
    // The output could not be parsed.
  }
}

You can also pass a list of codes.

ts
if (
  isLlmExeError(error, [
    "llm.provider_rate_limited",
    "llm.provider_unavailable",
  ])
) {
  // Retry, queue, or fall back.
}

Common Categories

Known error categories include:

  • configuration
  • parser
  • prompt
  • llm
  • embedding
  • executor
  • callable
  • state
  • request
  • auth
  • template
  • internal

Provider Errors

HTTP failures from LLM providers use typed llm-exe errors:

CodeTypical cause
llm.provider_rate_limitedProvider returned a rate-limit status
llm.provider_auth_failedMissing, invalid, or unauthorized credentials
llm.provider_invalid_requestProvider rejected the request body or model/options
llm.provider_unavailableTimeout, unavailable provider, or server-side failure
llm.provider_http_errorOther provider HTTP error

Embedding providers use the same pattern with the embedding. prefix.

Provider error context may include:

  • status
  • statusText
  • url
  • providerError
  • providerErrorBody
  • responseHeaders

Secrets are redacted before provider error data is added to context.

Serialization

Use serializeLlmExeError when sending errors to logs, queues, or telemetry.

ts
const payload = serializeLlmExeError(error);

Use formatLlmExeErrorForLog for compact logs.

ts
logger.error(formatLlmExeErrorForLog(error));

error.toJSON() also returns the serialized value for LlmExeError instances.