Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build LLM-powered apps with the Anthropic Claude API or SDK across Python, TypeScript, Java, Go, Ruby, C#, and PHP.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
shared/error-codes.md
1# HTTP Error Codes Reference23This 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.45## Error Code Summary67| Code | Error Type | Retryable | Common Cause |8| ---- | ----------------------- | --------- | ------------------------------------ |9| 400 | `invalid_request_error` | No | Invalid request format or parameters |10| 401 | `authentication_error` | No | Invalid or missing API key |11| 403 | `permission_error` | No | API key lacks permission |12| 404 | `not_found_error` | No | Invalid endpoint or model ID |13| 413 | `request_too_large` | No | Request exceeds size limits |14| 429 | `rate_limit_error` | Yes | Too many requests |15| 500 | `api_error` | Yes | Anthropic service issue |16| 529 | `overloaded_error` | Yes | API is temporarily overloaded |1718## Detailed Error Information1920### 400 Bad Request2122**Causes:**2324- Malformed JSON in request body25- Missing required parameters (`model`, `max_tokens`, `messages`)26- Invalid parameter types (e.g., string where integer expected)27- Empty messages array28- Messages not alternating user/assistant2930**Example error:**3132```json33{34"type": "error",35"error": {36"type": "invalid_request_error",37"message": "messages: roles must alternate between \"user\" and \"assistant\""38},39"request_id": "req_011CSHoEeqs5C35K2UUqR7Fy"40}41```4243**Fix:** Validate request structure before sending. Check that:4445- `model` is a valid model ID46- `max_tokens` is a positive integer47- `messages` array is non-empty and alternates correctly4849---5051### 401 Unauthorized5253**Causes:**5455- Missing `x-api-key` header or `Authorization` header56- Invalid API key format57- Revoked or deleted API key5859**Fix:** Ensure `ANTHROPIC_API_KEY` environment variable is set correctly.6061---6263### 403 Forbidden6465**Causes:**6667- API key doesn't have access to the requested model68- Organization-level restrictions69- Attempting to access beta features without beta access7071**Fix:** Check your API key permissions in the Console. You may need a different API key or to request access to specific features.7273---7475### 404 Not Found7677**Causes:**7879- Typo in model ID (e.g., `claude-sonnet-4.6` instead of `claude-sonnet-4-6`)80- Using deprecated model ID81- Invalid API endpoint8283**Fix:** Use exact model IDs from the models documentation. You can use aliases (e.g., `claude-opus-4-7`).8485---8687### 413 Request Too Large8889**Causes:**9091- Request body exceeds maximum size92- Too many tokens in input93- Image data too large9495**Fix:** Reduce input size — truncate conversation history, compress/resize images, or split large documents into chunks.9697---9899### 400 Validation Errors100101Some 400 errors are specifically related to parameter validation:102103- `max_tokens` exceeds model's limit104- Invalid `temperature` value (must be 0.0-1.0)105- `budget_tokens` >= `max_tokens` in extended thinking106- Invalid tool definition schema107108**Model-specific 400s on Opus 4.7:**109110- `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.111- `thinking: {type: "enabled", budget_tokens: N}` is removed — sending it returns 400. Use `thinking: {type: "adaptive"}` instead.112113**Common mistake with extended thinking on older models (Opus 4.6 and earlier):**114115```116# Wrong: budget_tokens must be < max_tokens117thinking: budget_tokens=10000, max_tokens=1000 → Error!118119# Correct120thinking: budget_tokens=10000, max_tokens=16000121```122123---124125### 429 Rate Limited126127**Causes:**128129- Exceeded requests per minute (RPM)130- Exceeded tokens per minute (TPM)131- Exceeded tokens per day (TPD)132133**Headers to check:**134135- `retry-after`: Seconds to wait before retrying136- `x-ratelimit-limit-*`: Your limits137- `x-ratelimit-remaining-*`: Remaining quota138139**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.140141---142143### 500 Internal Server Error144145**Causes:**146147- Temporary Anthropic service issue148- Bug in API processing149150**Fix:** Retry with exponential backoff. If persistent, check [status.anthropic.com](https://status.anthropic.com).151152---153154### 529 Overloaded155156**Causes:**157158- High API demand159- Service capacity reached160161**Fix:** Retry with exponential backoff. Consider using a different model (Haiku is often less loaded), spreading requests over time, or implementing request queuing.162163---164165## Common Mistakes and Fixes166167| Mistake | Error | Fix |168| ------------------------------- | ---------------- | ------------------------------------------------------- |169| `temperature`/`top_p`/`top_k` on Opus 4.7 | 400 | Remove the parameter (see `shared/model-migration.md`) |170| `budget_tokens` on Opus 4.7 | 400 | Use `thinking: {type: "adaptive"}` |171| `budget_tokens` >= `max_tokens` (older models) | 400 | Ensure `budget_tokens` < `max_tokens` |172| Typo in model ID | 404 | Use valid model ID like `claude-opus-4-7` |173| First message is `assistant` | 400 | First message must be `user` |174| Consecutive same-role messages | 400 | Alternate `user` and `assistant` |175| API key in code | 401 (leaked key) | Use environment variable |176| Custom retry needs | 429/5xx | SDK retries automatically; customize with `max_retries` |177178## Typed Exceptions in SDKs179180**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:181182| HTTP Code | TypeScript Class | Python Class |183| --------- | --------------------------------- | --------------------------------- |184| 400 | `Anthropic.BadRequestError` | `anthropic.BadRequestError` |185| 401 | `Anthropic.AuthenticationError` | `anthropic.AuthenticationError` |186| 403 | `Anthropic.PermissionDeniedError` | `anthropic.PermissionDeniedError` |187| 404 | `Anthropic.NotFoundError` | `anthropic.NotFoundError` |188| 429 | `Anthropic.RateLimitError` | `anthropic.RateLimitError` |189| 500+ | `Anthropic.InternalServerError` | `anthropic.InternalServerError` |190| Any | `Anthropic.APIError` | `anthropic.APIError` |191192```typescript193// ✅ Correct: use typed exceptions194try {195const response = await client.messages.create({...});196} catch (error) {197if (error instanceof Anthropic.RateLimitError) {198// Handle rate limiting199} else if (error instanceof Anthropic.APIError) {200console.error(`API error ${error.status}:`, error.message);201}202}203204// ❌ Wrong: don't check error messages with string matching205try {206const response = await client.messages.create({...});207} catch (error) {208const msg = error instanceof Error ? error.message : String(error);209if (msg.includes("429") || msg.includes("rate_limit")) { ... }210}211```212213All 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`).214