GuideOpen

HTTP Error Codes Reference (shared) (Claude Code templates (davila7))

This file documents HTTP error codes returned by the Claude API, their common causes, and how to handle them. For language-specific error handling examples, see the python/ or typescript/ folders.

HTTP Error Codes Reference

This file documents HTTP error codes returned by the Claude API, their common causes, and how to handle them. For language-specific error handling examples, see the python/ or typescript/ folders.

Error Code Summary

CodeError TypeRetryableCommon Cause
400invalid_request_errorNoInvalid request format or parameters
401authentication_errorNoInvalid or missing API key
403permission_errorNoAPI key lacks permission
404not_found_errorNoInvalid endpoint or model ID
413request_too_largeNoRequest exceeds size limits
429rate_limit_errorYesToo many requests
500api_errorYesAnthropic service issue
529overloaded_errorYesAPI is temporarily overloaded

Detailed Error Information

400 Bad Request

Causes:

  • Malformed JSON in request body
  • Missing required parameters (model, max_tokens, messages)
  • Invalid parameter types (e.g., string where integer expected)
  • Empty messages array
  • Messages not alternating user/assistant

Example error:

{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "messages: roles must alternate between \"user\" and \"assistant\""
  },
  "request_id": "req_011CSHoEeqs5C35K2UUqR7Fy"
}

Fix: Validate request structure before sending. Check that:

  • model is a valid model ID
  • max_tokens is a positive integer
  • messages array is non-empty and alternates correctly

401 Unauthorized

Causes:

  • Missing x-api-key header or Authorization header
  • Invalid API key format
  • Revoked or deleted API key

Fix: Ensure ANTHROPIC_API_KEY environment variable is set correctly.


403 Forbidden

Causes:

  • API key doesn't have access to the requested model
  • Organization-level restrictions
  • Attempting to access beta features without beta access

Fix: Check your API key permissions in the Console. You may need a different API key or to request access to specific features.


404 Not Found

Causes:

  • Typo in model ID (e.g., claude-sonnet-4.6 instead of claude-sonnet-4-6)
  • Using deprecated model ID
  • Invalid API endpoint

Fix: Use exact model IDs from the models documentation. You can use aliases (e.g., claude-opus-4-7).


413 Request Too Large

Causes:

  • Request body exceeds maximum size
  • Too many tokens in input
  • Image data too large

Fix: Reduce input size — truncate conversation history, compress/resize images, or split large documents into chunks.


400 Validation Errors

Some 400 errors are specifically related to parameter validation:

  • max_tokens exceeds model's limit
  • Invalid temperature value (must be 0.0-1.0)
  • budget_tokens >= max_tokens in extended thinking
  • Invalid tool definition schema

Model-specific 400s on Opus 4.7:

  • temperature, top_p, top_k are removed — sending any of them returns 400. Delete the parameter; see shared/model-migration.md → Per-SDK Syntax Reference.
  • thinking: {type: "enabled", budget_tokens: N} is removed — sending it returns 400. Use thinking: {type: "adaptive"} instead.

Common mistake with extended thinking on older models (Opus 4.6 and earlier):

# Wrong: budget_tokens must be < max_tokens
thinking: budget_tokens=10000, max_tokens=1000  → Error!

# Correct
thinking: budget_tokens=10000, max_tokens=16000

429 Rate Limited

Causes:

  • Exceeded requests per minute (RPM)
  • Exceeded tokens per minute (TPM)
  • Exceeded tokens per day (TPD)

Headers to check:

  • retry-after: Seconds to wait before retrying
  • x-ratelimit-limit-*: Your limits
  • x-ratelimit-remaining-*: Remaining quota

Fix: The Anthropic SDKs automatically retry 429 and 5xx errors with exponential backoff (default: max_retries=2). For custom retry behavior, see the language-specific error handling examples.


500 Internal Server Error

Causes:

  • Temporary Anthropic service issue
  • Bug in API processing

Fix: Retry with exponential backoff. If persistent, check status.anthropic.com.


529 Overloaded

Causes:

  • High API demand
  • Service capacity reached

Fix: Retry with exponential backoff. Consider using a different model (Haiku is often less loaded), spreading requests over time, or implementing request queuing.


Common Mistakes and Fixes

MistakeErrorFix
temperature/top_p/top_k on Opus 4.7400Remove the parameter (see shared/model-migration.md)
budget_tokens on Opus 4.7400Use thinking: {type: "adaptive"}
budget_tokens >= max_tokens (older models)400Ensure budget_tokens < max_tokens
Typo in model ID404Use valid model ID like claude-opus-4-7
First message is assistant400First message must be user
Consecutive same-role messages400Alternate user and assistant
API key in code401 (leaked key)Use environment variable
Custom retry needs429/5xxSDK retries automatically; customize with max_retries

Typed Exceptions in SDKs

Always use the SDK's typed exception classes instead of checking error messages with string matching. Each HTTP error code maps to a specific exception class:

HTTP CodeTypeScript ClassPython Class
400Anthropic.BadRequestErroranthropic.BadRequestError
401Anthropic.AuthenticationErroranthropic.AuthenticationError
403Anthropic.PermissionDeniedErroranthropic.PermissionDeniedError
404Anthropic.NotFoundErroranthropic.NotFoundError
429Anthropic.RateLimitErroranthropic.RateLimitError
500+Anthropic.InternalServerErroranthropic.InternalServerError
AnyAnthropic.APIErroranthropic.APIError
// ✅ Correct: use typed exceptions
try {
  const response = await client.messages.create({...});
} catch (error) {
  if (error instanceof Anthropic.RateLimitError) {
    // Handle rate limiting
  } else if (error instanceof Anthropic.APIError) {
    console.error(`API error ${error.status}:`, error.message);
  }
}

// ❌ Wrong: don't check error messages with string matching
try {
  const response = await client.messages.create({...});
} catch (error) {
  const msg = error instanceof Error ? error.message : String(error);
  if (msg.includes("429") || msg.includes("rate_limit")) { ... }
}

All exception classes extend Anthropic.APIError, which has a status property. Use instanceof checks from most specific to least specific (e.g., check RateLimitError before APIError).

Source: https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/development/claude-api/shared/error-codes.md

Author: davila7, Claude Code templates (davila7). License: Apache-2.0.

Shown here with formatting for the web. Keep the author and license notice when you reuse it.

Related entries

There are more like this in the members library

The Claude Code library holds 13,843 entries. Members can search all of them by meaning and read every one in full.