Provider error normalization. Classifies provider/LLM API errors into a common shape (e.g. context_overflow vs api_error), extracts human-readable messages from heterogeneous response bodies, and detects context-window overflow across many providers via message-pattern matching.

Methods

(static) parseAPICallError(input) → {Object}

Normalize a non-streaming API call error into a common shape, classifying it as "context_overflow" (via isOverflow, a 413, or an explicit context_length_exceeded code) or otherwise "api_error" with status/retry info.

Parameters:
NameTypeDescription
inputObject

{providerID: string, error: Object} — the provider id and the raw API error.

Returns:

A normalized error descriptor.

Type: 
Object

(static) parseStreamError(input) → {Object}

Parse an error object delivered inside a streaming response into a normalized error, mapping known error codes (context_length_exceeded, insufficient_quota, usage_not_included, invalid_prompt, server_error) to their type and retryability.

Parameters:
NameTypeDescription
input*

The raw stream error payload (object or JSON string).

Returns:

A normalized error {type, message, ...}, or undefined when not an error.

Type: 
Object

(inner) isOpenAiErrorRetryable(e) → {boolean}

Decide whether an OpenAI-style API error should be retried, treating 404 as retryable because OpenAI sometimes 404s models that are actually available.

Parameters:
NameTypeDescription
eObject

The provider error (statusCode, isRetryable).

Returns:

True when the request should be retried.

Type: 
boolean

(inner) isOverflow(message) → {boolean}

Detect whether an error message indicates a context-window overflow, matching the provider-specific OVERFLOW_PATTERNS plus bare 400/413 "(no body)" responses from providers like Cerebras and Mistral.

Parameters:
NameTypeDescription
messagestring

The error message text to test.

Returns:

True when the message looks like a context overflow.

Type: 
boolean

(inner) json(input) → {Object}

Coerce a value to a plain object: parse a JSON string, pass through objects, and return undefined for anything that is not (or does not parse to) an object.

Parameters:
NameTypeDescription
input*

A JSON string or value to coerce.

Returns:

The parsed object, or undefined.

Type: 
Object

(inner) message(providerID, e) → {string}

Build the best human-readable message for a provider error, falling back from the error message to the response body, JSON error fields, or the HTTP status text, and rewriting HTML gateway/proxy 401/403 pages into actionable guidance.

Parameters:
NameTypeDescription
providerIDstring

The provider id (reserved for provider-specific handling).

eObject

The provider error (message, responseBody, statusCode).

Returns:

A trimmed, human-readable error message.

Type: 
string