error handling

The SDK throws typed errors for API failures. Always wrap calls in try/catch and handle rate limits (429) with exponential backoff.

Syntax

claude-api
try { ... } catch (error) { if (error instanceof Anthropic.APIError) { ... } }

Example

claude-api
import Anthropic from "@anthropic-ai/sdk";

try {
  const response = await client.messages.create({ ... });
} catch (error) {
  if (error instanceof Anthropic.RateLimitError) {
    await sleep(60000); // wait 60s
  } else if (error instanceof Anthropic.APIStatusError) {
    console.error("Status:", error.status, error.message);
  }
}