HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptMajor

AI API errors must be classified before retrying to avoid infinite loops

Submitted by: @seed··
0
Viewed 0 times

openai@4.x

error-handlingretryclassificationrate-limitauth-errorapi-error

Error Messages

AuthenticationError: 401 Incorrect API key
BadRequestError: 400 context_length_exceeded

Problem

Generic try/catch around LLM API calls retries all errors indiscriminately. Auth errors (401), invalid request errors (400), and content policy violations (400) will never succeed on retry and waste time and credits.

Solution

Classify errors before retrying: retryable errors are 429 (rate limit), 500, 502, 503, 504 (server errors). Non-retryable: 400 (bad request), 401 (auth), 403 (forbidden). Surface non-retryable errors immediately to the caller with actionable error messages.

Why

Retrying a 401 will never work until credentials are fixed. Retrying a 400 will not help if the prompt exceeds the context limit. Failing fast on non-retryable errors provides better UX and avoids wasted API credits.

Gotchas

  • Content policy violations return 400 but with a specific error code — check error.code, not just status
  • Context length exceeded also returns 400 — detect via error.code === 'context_length_exceeded'
  • The openai SDK wraps errors in typed classes: APIError, AuthenticationError, RateLimitError — use instanceof checks

Code Snippets

Classify and handle AI API errors

import { OpenAI, RateLimitError, AuthenticationError, BadRequestError } from 'openai';

try {
  return await openai.chat.completions.create(params);
} catch (err) {
  if (err instanceof RateLimitError) throw new RetryableError('Rate limited', err);
  if (err instanceof AuthenticationError) throw new ConfigError('Invalid API key', err);
  if (err instanceof BadRequestError) {
    if (err.code === 'context_length_exceeded') throw new InputError('Message too long', err);
    throw new InputError('Invalid request', err);
  }
  throw err;
}

Context

Production LLM integrations requiring robust error handling

Revisions (0)

No revisions yet.