any-llm-ts
Guides

Error handling

Handle normalized provider failures and errors raised during streaming.

Provider SDK errors are converted into a common hierarchy rooted at AnyLLMError.

import {
  AnyLLMError,
  AuthenticationError,
  RateLimitError,
  UnsupportedOperationError,
  completion,
} from "any-llm-ts";

try {
  await completion({
    provider: "openai",
    model: "gpt-4.1-mini",
    messages: [{ role: "user", content: "Hello" }],
  });
} catch (error) {
  if (error instanceof AuthenticationError) {
    // Replace or refresh credentials.
  } else if (error instanceof RateLimitError) {
    console.error(error.retryAfter);
  } else if (error instanceof UnsupportedOperationError) {
    // Choose a provider that supports the operation.
  } else if (error instanceof AnyLLMError) {
    console.error(error.provider, error.statusCode, error.code);
  } else {
    throw error;
  }
}

Common classes

ErrorMeaning
MissingApiKeyErrorNo explicit key or expected environment variable was found.
AuthenticationErrorThe provider returned HTTP 401 or 403.
InvalidRequestErrorThe provider rejected a request with another 4xx status.
RateLimitErrorHTTP 429; retryAfter is retained when present.
ModelNotFoundErrorHTTP 404.
ContextLengthExceededErrorThe provider reported a context or token limit.
ContentFilterErrorThe provider reported filtered content.
UpstreamProviderErrorHTTP 502.
GatewayTimeoutErrorHTTP 504.
ProviderErrorAnother provider-side or unclassified SDK failure.
UnsupportedProviderErrorThe requested provider name is not registered.
UnsupportedOperationErrorThe adapter does not implement the requested operation.
InvalidModelSyntaxErrorA stateless helper could not determine a provider from the model.

Every AnyLLMError retains the original error as cause and exposes provider, statusCode, code, param, and errorType when the SDK supplied them.

Streaming errors

Wrap both stream creation and iteration in the same try block. A network or provider failure may occur after several chunks have already arrived.

On this page