← 提示词库 Anthropic/claude-code/skills/claude-api/shared/error-codes.md 原文 md
🌐 中英双语对照

HTTP Error Codes Reference / HTTP 错误码参考

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.

本文件记录 Claude API 返回的 HTTP 错误码、其常见原因及处理方法。各语言的错误处理示例参见 python/ 或 typescript/ 文件夹。

Error Code Summary / 错误码汇总

Code Error Type Retryable Common Cause
400 invalid_request_error No Invalid request format or parameters
401 authentication_error No Invalid or missing API key
402 billing_error No Billing or payment problem
403 permission_error No Not allowed for this credential
404 not_found_error No Unknown endpoint, or model not found or not available to your org
413 request_too_large No Request exceeds size limits
429 rate_limit_error Yes Too many requests
500 api_error Yes Anthropic service issue
529 overloaded_error Yes API is temporarily overloaded
代码 错误类型 可重试 常见原因
400 invalid_request_error 否 请求格式或参数无效
401 authentication_error 否 API 密钥无效或缺失
402 billing_error 否 计费或支付问题
403 permission_error 否 该凭证无权执行此操作
404 not_found_error 否 未知端点,或模型不存在或对你的组织不可用
413 request_too_large 否 请求超过大小限制
429 rate_limit_error 是 请求过多
500 api_error 是 Anthropic 服务问题
529 overloaded_error 是 API 暂时过载

Detailed Error Information / 详细错误信息

400 Bad Request / 400 错误请求

Causes:

原因:

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:

**修复:**发送前校验请求结构。检查:


401 Unauthorized / 401 未授权

Causes:

原因:

Fix: Set ANTHROPIC_API_KEY, or run ant auth login and leave the client constructor empty. For raw HTTP with an OAuth token, use Authorization: Bearer <token> (not x-api-key:).

**修复:**设置 ANTHROPIC_API_KEY,或运行 ant auth login 并让客户端构造函数留空。对携带 OAuth 令牌的原始 HTTP,使用 Authorization: Bearer <token>(而非 x-api-key:)。


403 Forbidden / 403 禁止访问

Causes:

原因:

A model your organization cannot use is normally a 404, not a 403 (see below). A beta header your organization is not enabled for is a 400.

你的组织无法使用的模型通常是 404 而非 403(见下文)。未对你的组织启用的 beta 头则是 400。

Fix: Check your organization's access and workspace settings in the Console.

**修复:**在 Console 中检查你组织的访问权限与工作区设置。


404 Not Found / 404 未找到

Causes:

原因:

A model that does not exist and a model your organization cannot use return the same response, not_found_error with a message that starts with model: <id>. The API does not reveal whether a model exists to callers who cannot use it.

不存在的模型与你的组织无法使用的模型返回相同的响应:not_found_error,消息以 model: <id> 开头。对无法使用某模型的调用方,API 不会透露该模型是否存在。

【评论】对无权使用的调用方不区分"模型不存在"与"模型不可用",属于避免向外界泄露产品与可用性信息的信息披露控制。

Fix: Use exact model IDs from the models documentation. You can use aliases (e.g., claude-opus-5-5). To see which models your organization can use, call GET /v1/models.

**修复:**使用模型文档中的确切模型 ID。可以使用别名(如 claude-opus-5-5)。要查看你的组织可使用哪些模型,调用 GET /v1/models。


413 Request Too Large / 413 请求过大

Causes:

原因:

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

**修复:**减小输入体积——截断会话历史、压缩/缩放图像,或将大文档分块。


400 Validation Errors / 400 校验错误

Some 400 errors are specifically related to parameter validation:

有些 400 错误专门与参数校验有关:

Model-specific 400s on Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7:

Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 上特定于模型的 400:

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

旧模型(Opus 4.6 及更早)上扩展思考的常见错误:

# 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 / 429 触发限流

Causes:

原因:

Headers to check:

需要查看的响应头:

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.

**修复:**Anthropic SDK 会以指数退避自动重试 429 和 5xx 错误(默认 max_retries=2)。自定义重试行为参见各语言的错误处理示例。


500 Internal Server Error / 500 服务器内部错误

Causes:

原因:

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

**修复:**以指数退避重试。若持续出现,查看 status.anthropic.com。


529 Overloaded / 529 过载

Causes:

原因:

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

**修复:**以指数退避重试。考虑换用其他模型(Haiku 的负载通常较低)、把请求在时间上摊开,或实现请求排队。


Common Mistakes and Fixes / 常见错误与修复

Mistake Error Fix
temperature/top_p/top_k on Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 400 Remove the parameter (see shared/model-migration.md)
budget_tokens on Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 400 Use thinking: {type: "adaptive"}
thinking: {type: "disabled"} on Fable 5/5.1 400 Omit the thinking param entirely (accepted on Opus 4.8/4.7)
Org set to ZDR / retention below 30 days (Fable 5/5.1, Mythos 5/5.1) 400 on every request Fix the org's data-retention configuration - the payload isn't the problem
thinking: {type: "disabled"} or budget_tokens on Claude Opus 5.5 400 "thinking.type.disabled" is not supported for this model Omit thinking; control depth with output_config.effort (default medium)
computer_20251124 tool on Claude Opus 5.5 400 does not support tool types: computer_20251124 {type: "computer_toolset_20260801"} - no beta header, no name / display size; update the agent loop for member tool calls
thinking: {type: "disabled"} on Claude Sonnet 5.5 400 "thinking.type.disabled" is not supported for this model {type: "between_tools"} at effort high or below (no other thinking field, no per-message effort change), or thinking on at a lower effort
thinking: {type: "between_tools"} on any other model, or at xhigh / max 400 Send it only to Claude Sonnet 5.5 at effort high or below; otherwise omit thinking
tool_choice any / tool on Claude Fable 5.1 / Claude Mythos 5.1 / Claude Opus 5.5 / Claude Sonnet 5.5 400 {type: "auto"} + name the tool in the prompt (strict: true for schema-valid args), or structured outputs
Edited history replayed with thinking blocks (Claude Fable 5.1 / Claude Opus 5.5 / Claude Sonnet 5.5, preserved thinking; Claude Mythos 5.1 doesn't run this check) 400 Invalid signature in thinking block ... bound to a different conversation Stop editing history - keep the transcript append-only, using mid-conversation role: "system" / tool-change messages, turn-scoped clear_at reminders that are never deleted, server-side context editing, and summary-only compaction instead of edits; recover once by stripping the named block and every thinking block after it (text and tool calls stay), or prefix_mismatch_behavior: "drop_block" (thinking on only - not with Claude Sonnet 5.5's between_tools)
thinking.block_binding without thinking-binding-controls-2026-08-01 400 block_binding: Extra inputs are not permitted Send the beta header where the controls beta is offered (shared/platform-availability.md); elsewhere remove block_binding and use strip-and-retry
budget_tokens >= max_tokens (older models) 400 Ensure budget_tokens < max_tokens
Typo in model ID 404 Use valid model ID like claude-opus-5-5
First message is assistant 400 First message must be user
Consecutive same-role messages 400 Alternate user and assistant
API key in code 401 (leaked key) Use environment variable
Custom retry needs 429/5xx SDK retries automatically; customize with max_retries
常见错误 错误码 修复
在 Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 上使用 temperature/top_p/top_k 400 删除该参数(见 shared/model-migration.md)
在 Claude Opus 5.5 / Claude Opus 5 / Fable 5/5.1 / Opus 4.8 / 4.7 上使用 budget_tokens 400 使用 thinking: {type: "adaptive"}
在 Fable 5/5.1 上使用 thinking: {type: "disabled"} 400 完全省略 thinking 参数(Opus 4.8/4.7 上可接受)
组织被设为 ZDR / 保留期低于 30 天(Fable 5/5.1、Mythos 5/5.1) 每个请求都 400 修复组织的数据保留配置——问题不在载荷
在 Claude Opus 5.5 上使用 thinking: {type: "disabled"} 或 budget_tokens 400 "thinking.type.disabled" is not supported for this model 省略 thinking;用 output_config.effort 控制深度(默认 medium)
在 Claude Opus 5.5 上使用 computer_20251124 工具 400 does not support tool types: computer_20251124 {type: "computer_toolset_20260801"}——无需 beta 头,无 name/显示尺寸;并更新智能体循环以适配成员工具调用
在 Claude Sonnet 5.5 上使用 thinking: {type: "disabled"} 400 "thinking.type.disabled" is not supported for this model 在 effort high 或以下使用 {type: "between_tools"}(不带其他 thinking 字段、不按消息改 effort),或保持思考开启并调低 effort
在任何其他模型上、或在 xhigh/max 下使用 thinking: {type: "between_tools"} 400 只发给 effort high 或以下的 Claude Sonnet 5.5;否则省略 thinking
在 Claude Fable 5.1 / Claude Mythos 5.1 / Claude Opus 5.5 / Claude Sonnet 5.5 上使用 tool_choice 的 any/tool 400 {type: "auto"} + 在提示词中点名工具(需要 schema 合法参数时加 strict: true),或结构化输出
编辑过的历史连同思考块一起重放(Claude Fable 5.1 / Claude Opus 5.5 / Claude Sonnet 5.5 的保留思考;Claude Mythos 5.1 不运行该检查) 400 Invalid signature in thinking block ... bound to a different conversation 停止编辑历史——保持转录只追加,改用会话中途的 role: "system"/工具变更消息、从不删除的回合级 clear_at 提醒、服务端上下文编辑以及仅摘要式压缩来代替编辑;一次性恢复:剥除被指名的块及其后的所有思考块(文本与工具调用保留),或使用 prefix_mismatch_behavior: "drop_block"(仅思考开启时——不适用于 Claude Sonnet 5.5 的 between_tools)
没有 thinking-binding-controls-2026-08-01 时使用 thinking.block_binding 400 block_binding: Extra inputs are not permitted 在提供该 controls beta 的平台发送 beta 头(shared/platform-availability.md);其他平台移除 block_binding 并改用"剥除后重试"
budget_tokens >= max_tokens(较旧模型) 400 确保 budget_tokens < max_tokens
模型 ID 拼写错误 404 使用有效的模型 ID,如 claude-opus-5-5
首条消息为 assistant 400 首条消息必须是 user
连续同角色消息 400 user 与 assistant 交替
API 密钥写在代码里 401(密钥已泄露) 使用环境变量
自定义重试需求 429/5xx SDK 自动重试;用 max_retries 定制

Typed Exceptions in SDKs / SDK 中的类型化异常

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

始终使用 SDK 的类型化异常类,而不是用字符串匹配检查错误消息。每个 HTTP 状态码在每个 SDK 中都映射到特定的异常类。

【评论】"按类型化异常而非错误消息字符串匹配"是官方 SDK 的通用设计:每个状态码对应独立异常类,使调用方能以语言原生的异常机制做可靠分支。

Exception class names by language / 各语言的异常类名

HTTP Python (anthropic.*) / TypeScript (Anthropic.*) Ruby (Anthropic::Errors::*) Java (com.anthropic.errors.*) C# PHP (Anthropic\Core\Exceptions\*)
400 BadRequestError BadRequestError BadRequestException AnthropicBadRequestException BadRequestException
401 AuthenticationError AuthenticationError UnauthorizedException AnthropicUnauthorizedException AuthenticationException
403 PermissionDeniedError PermissionDeniedError PermissionDeniedException AnthropicForbiddenException PermissionDeniedException
404 NotFoundError NotFoundError NotFoundException AnthropicNotFoundException NotFoundException
422 UnprocessableEntityError UnprocessableEntityError UnprocessableEntityException AnthropicUnprocessableEntityException UnprocessableEntityException
429 RateLimitError RateLimitError RateLimitException AnthropicRateLimitException RateLimitException
>=500 InternalServerError InternalServerError InternalServerException Anthropic5xxException InternalServerException
net APIConnectionError APIConnectionError AnthropicIoException AnthropicIOException APIConnectionException
base APIError (both); APIStatusError (Python only) APIStatusError / APIError AnthropicServiceException AnthropicApiException APIStatusException / APIException
HTTP 状态码 Python (anthropic.*) / TypeScript (Anthropic.*) Ruby (Anthropic::Errors::*) Java (com.anthropic.errors.*) C# PHP (Anthropic\Core\Exceptions\*)
400 BadRequestError BadRequestError BadRequestException AnthropicBadRequestException BadRequestException
401 AuthenticationError AuthenticationError UnauthorizedException AnthropicUnauthorizedException AuthenticationException
403 PermissionDeniedError PermissionDeniedError PermissionDeniedException AnthropicForbiddenException PermissionDeniedException
404 NotFoundError NotFoundError NotFoundException AnthropicNotFoundException NotFoundException
422 UnprocessableEntityError UnprocessableEntityError UnprocessableEntityException AnthropicUnprocessableEntityException UnprocessableEntityException
429 RateLimitError RateLimitError RateLimitException AnthropicRateLimitException RateLimitException
>=500 InternalServerError InternalServerError InternalServerException Anthropic5xxException InternalServerException
网络 APIConnectionError APIConnectionError AnthropicIoException AnthropicIOException APIConnectionException
基类 APIError(两者);APIStatusError(仅 Python) APIStatusError / APIError AnthropicServiceException AnthropicApiException APIStatusException / APIException

The Ruby and PHP classes live in a dedicated errors namespace - write Anthropic::Errors::RateLimitError and Anthropic\Core\Exceptions\RateLimitException (not bare Anthropic::RateLimitError). All 4xx C# exceptions also inherit from Anthropic4xxException.

Ruby 与 PHP 的类位于专门的错误命名空间——应写 Anthropic::Errors::RateLimitError 和 Anthropic\Core\Exceptions\RateLimitException(而不是裸的 Anthropic::RateLimitError)。所有 4xx 的 C# 异常还继承自 Anthropic4xxException。

Catch most-specific first, in a chain / 按从最具体到基类的顺序链式捕获

Order catch/except/rescue clauses from the most specific subclass to the base class, with a separate clause for each category you handle differently - retryable (429, >=500, network) vs. non-retryable (4xx). The SDK defines a distinct class per status for exactly this reason; a single broad catch-all discards that information.

把 catch/except/rescue 子句从最具体的子类排到基类,对需要区别处理的每个类别各写一个子句——可重试(429、>=500、网络)与不可重试(4xx)。SDK 为每个状态定义单独的类正是为此;一个宽泛的兜底 catch 会丢弃这些信息。

try:
    msg = client.messages.create(...)
except anthropic.NotFoundError as e:          # 404 - e.g. bad model ID
    ...
except anthropic.RateLimitError as e:         # 429 - back off and retry
    ...
except anthropic.APIStatusError as e:         # any other non-2xx HTTP response
    print(e.status_code, e.message)
except anthropic.APIConnectionError as e:     # network failure before a response
    ...

The same chain shape applies in every SDK: TypeScript instanceof Anthropic.NotFoundError -> RateLimitError -> APIConnectionError -> APIError (check APIConnectionError before APIError - in the TypeScript SDK it's a subclass of APIError, unlike Python where it's a sibling); Ruby rescue Anthropic::Errors::NotFoundError -> ...::RateLimitError -> ...::APIStatusError; Java catch (NotFoundException) ... catch (RateLimitException) ... catch (AnthropicServiceException); C# catch (AnthropicNotFoundException) ... catch (AnthropicRateLimitException) ... catch (AnthropicApiException); PHP catch (NotFoundException) ... catch (RateLimitException) ... catch (APIStatusException).

同样的链式结构适用于每个 SDK:TypeScript 用 instanceof Anthropic.NotFoundError -> RateLimitError -> APIConnectionError -> APIError(先查 APIConnectionError 再查 APIError——在 TypeScript SDK 中它是 APIError 的子类,不像 Python 中是并列关系);Ruby 用 rescue Anthropic::Errors::NotFoundError -> ...::RateLimitError -> ...::APIStatusError;Java 用 catch (NotFoundException) ... catch (RateLimitException) ... catch (AnthropicServiceException);C# 用 catch (AnthropicNotFoundException) ... catch (AnthropicRateLimitException) ... catch (AnthropicApiException);PHP 用 catch (NotFoundException) ... catch (RateLimitException) ... catch (APIStatusException)。

Go - errors.As then branch on status / Go——先 errors.As 再按状态码分支

The Go SDK returns a single *anthropic.Error for all non-2xx responses. Unwrap it with errors.As, then branch on StatusCode:

Go SDK 对所有非 2xx 响应返回单一的 *anthropic.Error。用 errors.As 解包,然后按 StatusCode 分支:

_, err := client.Messages.New(ctx, params)
if err != nil {
    var apierr *anthropic.Error
    if errors.As(err, &apierr) {
        switch apierr.StatusCode {
        case 404:
            // bad model ID / resource
        case 429:
            // back off and retry
        default:
            // other API error - apierr.StatusCode, apierr.RequestID
        }
    } else {
        // transport-level error (*url.Error wrapping *net.OpError, etc.)
    }
}

Error .type Field / 错误的 .type 字段

All APIStatusError subclasses now expose a .type property (Python: .type, TypeScript: .type, Java: .errorType(), Go: .Type(), Ruby: .type, PHP: .type) that returns the API error type string (e.g., "invalid_request_error", "authentication_error", "rate_limit_error", "overloaded_error"). Use this to classify errors by type name instead of by status code. "billing_error" is a 402 and "permission_error" is a 403.

所有 APIStatusError 的子类现在都暴露一个 .type 属性(Python:.type,TypeScript:.type,Java:.errorType(),Go:.Type(),Ruby:.type,PHP:.type),返回 API 错误类型字符串(如 "invalid_request_error"、"authentication_error"、"rate_limit_error"、"overloaded_error")。用它按类型名而不是状态码对错误分类。"billing_error" 对应 402,"permission_error" 对应 403。

except anthropic.APIStatusError as e:
    if e.type == "rate_limit_error":
        # handle rate limiting
    elif e.type == "overloaded_error":
        # handle overload