NAAPI 提供的 Anthropic Messages 兼容接口,适合 Claude 原生 SDK、content blocks、prompt caching、thinking 和 Anthropic 工具调用协议。
https://naapi.ccPOST https://naapi.cc/v1/messagesx-api-key: <API_KEY>anthropic-version: 2023-06-01Authorization: Bearer <API_KEY>model、messages、max_tokenscontent[] 中 type == "text" 的 textNAAPI_API_KEY 环境变量读取naapi.apifox.cn是文档站点。Anthropic SDK 的base_url应设为https://naapi.cc。
client.messages.create(...) 的 Claude 项目tool_use / tool_result 协议thinking、prompt caching 或 Anthropic 原生 usage 字段MODEL 替换为账户当前可用的 Anthropic Messages 兼容模型 ID。x-api-key。自定义 HTTP 客户端也可以使用 Authorization: Bearer <API_KEY>,但仍需发送 anthropic-version。| 字段 | 必填 | 说明 |
|---|---|---|
model | 是 | 模型 ID |
messages | 是 | Anthropic user / assistant 消息数组 |
max_tokens | 是 | 最大输出 Token 数,必须大于 0 |
system | 否 | 顶层系统指令,字符串或 content blocks |
stream | 否 | true 启用 Anthropic SSE 事件流 |
temperature | 否 | 采样温度,范围通常为 0-1 |
stop_sequences | 否 | 停止序列数组 |
tools | 否 | Anthropic 工具定义,使用 input_schema |
tool_choice | 否 | auto、any、tool 或 none |
thinking | 否 | 支持的模型可配置 budget_tokens |
system 不是 messages 中的 system role。Anthropic 原生协议应将系统指令放在顶层 system。tool_useuser 消息回传 tool_resulttool_result.tool_use_id 必须匹配模型返回的 tool_use.idinput_schema,不是 OpenAI 的 parametersinput_tokens、cache_creation_input_tokens、cache_read_input_tokens 和 output_tokens。不要仅用 input_tokens 推断全部输入规模或费用。anthropic-version: 2023-06-01。max_tokens。system,不要创建 Anthropic 不支持的 system role 消息。tool_calls、tool_call_id 或 response_format 直接发到此接口。content[0];应遍历 content blocks 并按 type 处理。thinking、speed、MCP 或所有扩展字段。error.message;400 / 401 / 429 不要盲目重试。| 状态 | 常见原因 | 处理 |
|---|---|---|
400 | 缺少 max_tokens、content block 错误或字段不兼容 | 读取 error.message 并修正请求 |
401 | x-api-key / Bearer Key 缺失或无效 | 检查鉴权请求头 |
429 | 频率、并发或额度限制 | 降低并发,使用退避 |
5xx | 网关或上游短暂异常 | 有上限地重试,避免重复执行工具 |