Why Error Handling Matters

Most API documentation lists error codes. This guide goes further — every error below was deliberately triggered against the live Claude API. The exact messages, the real responses, and the subtle behaviors that official documentation often misses. Building a reliable Claude integration means handling not just the errors that throw — but the ones that don’t.

The Error Response Structure

Every Claude API error follows the same JSON shape:
This consistency is useful — one error handler covers all error types:
Always log request_id on every failed request. If you need Anthropic support to investigate a specific error, this ID is the only way their team can trace it.

authentication_error — 401

What Causes It

An invalid or missing API key. The most common cause is an environment variable that was not set correctly or a key that was regenerated without updating all the places it is used.

Real Response

How to Fix

  • Confirm the x-api-key header is present on the request
  • Verify the key starts with sk-ant-
  • Check that your environment variable is set and the correct environment is active in your API client
  • If you recently regenerated the key, update it everywhere it is referenced
Never hardcode your API key in source code or commit it to version control. Store it as an environment variable and reference it server-side only. If a key is ever exposed, revoke it immediately from the Anthropic Console and generate a new one.

invalid_request_error — 400

What Causes It

A malformed request — missing required fields, invalid values, or incorrect structure. This is the broadest error type and covers several distinct problems. Always read error.message to understand exactly what is wrong.

Real Responses

Missing messages field:
Missing anthropic-version header:

How to Fix

Confirm every required field and header is present: Required fields: Required headers:
A 400 covers many different problems. Unlike 401 and 429 which are specific, a 400 can mean missing fields, invalid values, malformed JSON, or incorrect message structure. Always read error.message first before debugging further.

not_found_error — 404

What Causes It

A model string that does not exist. This error is distinct from invalid_request_error — the request was structurally valid, but the resource it referenced does not exist.

Real Response

How to Fix

Use a valid model string:
Most community API references document only authentication_error and invalid_request_error. The not_found_error type was discovered through real testing — by deliberately passing a model string that does not exist. It is a distinct error type worth handling separately in your application logic.

rate_limit_error — 429

What Causes It

Too many requests sent in a short period. Anthropic applies rate limits per API key based on your usage tier.

Response

How to Fix

Implement exponential backoff — wait before retrying and increase the wait time on each subsequent failure:
Most rate limit errors resolve within one or two retries. If you consistently hit rate limits, review your request volume or contact Anthropic about upgrading your usage tier.

Silent Truncation — 200

What Makes This Different

This is not an error response. It is a 200 OK. And it is the most dangerous failure mode in the Claude API.

What Causes It

Setting max_tokens too low to generate any output. The API accepts the request, starts generating, immediately hits the token limit, and returns an empty response — with no error thrown.

Real Response

content is an empty array. output_tokens is 0. stop_reason is "max_tokens". HTTP status is 200 OK.
This is the hardest failure to catch because it looks like success. An application that does not check stop_reason will pass an empty response to the user — a blank screen with no explanation. This was discovered by deliberately setting max_tokens: 0 during testing. Always check stop_reason before processing any response.

How to Fix

Always check stop_reason before processing Claude’s output:

Complete Error Reference


A Single Error Handler

Because all errors share the same structure, one function handles everything:

Errors During Streaming

Streaming responses can fail mid-stream. Anthropic sends an error event when this happens:
Your streaming implementation must handle the error event type — not just content_block_delta and message_stop:
A stream that starts successfully can still fail partway through. Never assume a stream that opened will complete.

Further Reading

Error Handling API Reference

Interactive playground for testing error responses

Streaming

Handle errors that occur mid-stream

Tool Use

Handle tool use errors and failed tool calls

Tokens

How max_tokens affects response completeness and silent truncation