Claude Messages API 申请及使用
要从支持 MCP 的客户端发送 Claude 消息,可通过 AI Chat MCP 的 aichat_create_message 调用 Messages 接口。使用具有该接口权限的应用凭据。AI Chat MCP 对接指南
Messages API 使用 Anthropic Messages 请求和响应格式,提供消息、系统提示词、内容块、工具调用、思考控制和流式事件的统一接口。通过 model 选择此入口发布的模型,客户端沿用同一套 Messages 数据结构。
本文介绍 Messages API 的申请、调用和响应处理方式。
¶ Grok
向 POST /v1/messages 提交 grok-4.7、grok-4.5、grok-4 或 grok-3。max_tokens 设置本次回复的输出上限,普通文本从返回的 content 数组读取:
{"model": "grok-4.7", "max_tokens": 256, "messages": [{"role": "user", "content": "只回复 OK"}]}
费用按请求中的型号计算。grok-4.7 的普通输入、缓存读取和缓存写入 token 合计达到 200,000 时,本次调用采用长上下文费率。
¶ Gemini 3.8 Flash 与 Gemini 4
model 可选择 gemini-3.8-flash、gemini-4-argon、gemini-4-carbon、gemini-4-hydrogen、gemini-4-neon。
向 POST https://api.acedata.cloud/v1/messages 发送以下 JSON。读取返回的 content 数组中 type: "text" 内容块的 text 字段,以及 usage.input_tokens 和 usage.output_tokens。多轮对话在 messages 中传入历史。
{"model": "gemini-3.8-flash", "max_tokens": 1024, "messages": [{"role": "user", "content": "只回复 OK"}]}
设置 "stream": true 可逐个接收 Messages 事件,直至 message_stop。使用函数工具时,把返回的完整 content 原样保留为下一轮的 assistant 消息,再在 user 消息中添加 tool_result,其 tool_use_id 填写对应 tool_use 块的 id。
¶ GPT-6.2 Astra 与 GPT-6.2 Sol
model 可选择 gpt-6.2-astra 或 gpt-6.2-sol。支持文本、图像输入和文本输出,工具及结果按 Messages 的 tools、tool_use、tool_result 格式提交。推理档位支持 low、medium、high、xhigh、max,不支持 none 或 minimal;参数格式以本接口定义为准。普通输入、缓存读取、缓存写入与输出分别计费,输入及缓存总量超过 272,000 tokens 时整次请求采用长上下文价格。
¶ Claude Fable 5.2
使用完整型号 claude-fable-5-2。该型号适合复杂推理和长程任务;需要原生思考内容块与工具交互时,优先使用 Messages。自适应思考持续开启,省略 temperature、top_p、top_k,工具选择使用 auto,不要强制指定工具。max_tokens 同时覆盖思考和最终文本。 当前不提供此型号的 Responses 和 Count Tokens 接口。
按当前最大套餐(142,800 Credits/$13,584)折算,Fable 5.2 普通输入为 $3.20、输出为 $16/百万 token;缓存读取为 $0.08,5 分钟/1 小时缓存写入分别为 $4/$6.40/百万 token。上述价格为官方标准 API 单价的 40%(便宜 60%);其他套餐及账户额外优惠请以模型定价页和账单为准。
¶ GPT-6.1 Terra
使用 model: "gpt-6.1-terra" 可通过此接口调用该模型。请求仍使用 Messages 格式的 messages 和 max_tokens,返回的文本与用量也遵循本接口结构。切换型号时请核对当前价格及账号可用性;单独报告的缓存读取和创建 token 分别结算。
¶ 申请流程
要使用 Claude Messages API,首先到 Ace Data Cloud 控制台 获取您的 API Token,留作备用。

如果你尚未登录或注册,会自动跳转到登录页面邀请你注册和登录,完成后会自动返回当前页面。
通用应用的 API Key 可用于账号已开放的服务;专用应用的 Key 按该应用的服务范围使用。请检查 Key 的权限、有效期和限额,并确认应用余额足以支付本次请求。可用试用额度以账号显示为准,余额不足时点击「充值」按钮即可。
📘 完整文档:Claude Messages API →
¶ 基本使用
Claude Messages API 的请求路径为 /v1/messages,与 Anthropic 官方 API 保持一致。平台接受的兼容模型请求按该模型的输入、输出及缓存用量计费;Messages 的输入 token 不包含单独报告的缓存读取和创建 token,这些维度分别结算。费用以控制台当前模型价格和响应 usage.cost 为准。
我们至少需要提供三个必填参数:
model:填写本接口模型清单中的完整模型 ID。切换模型时继续使用 Messages 的请求字段和响应结构;模型清单、上下文上限和价格见对应模型的 API 与定价页面。messages:输入的消息数组,每条消息包含role(角色)和content(内容),其中role支持user和assistant。max_tokens:最大输出 token 数,用于限制单次回复的长度。
常用可选参数:
system:系统提示词,用于设定模型的行为和角色。temperature:生成随机性,0-1 之间,值越大回复越发散。stream:是否使用流式响应,设为true可实现逐字返回效果。stop_sequences:自定义停止序列,模型遇到这些文本时会停止生成。top_p:核采样参数,与 temperature 配合控制生成的随机性。top_k:仅从概率最高的 K 个选项中采样。tools:工具定义,用于让模型调用外部函数。tool_choice:控制模型如何使用提供的工具。cache_control:在请求的最后一个可缓存内容块处自动创建缓存断点;也可写在具体内容块上。
工具调用统一使用 tools[].input_schema、tool_choice、tool_use 和 tool_result;tool_choice.disable_parallel_tool_use: true 禁止同轮并行工具调用。思考控制使用 thinking 与 output_config.effort,结构化输出使用 output_config.format。max_tokens 是本次输出预算,不会被平台自动提高;思考与正文共同占用该预算。
参数取值和功能限制以接口及模型说明为准;处理参数错误时,请读取具体错误字段和原因。
¶ cURL 示例
curl -X POST 'https://api.acedata.cloud/v1/messages' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
"model": "claude-fable-5-1",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello, Claude"
}
]
}'
¶ 结构化输出
使用 output_config.format 指定 JSON Schema,响应仍采用 Messages 的 content 内容块:
{
"output_config": {
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {"answer": {"type": "string"}},
"required": ["answer"],
"additionalProperties": false
}
}
}
}
¶ Python 示例
import requests
url = "https://api.acedata.cloud/v1/messages"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-fable-5-1",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello, Claude"}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
调用之后,返回结果如下:
{
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hi! My name is Claude. How can I help you today?"
}
],
"model": "claude-opus-4-8",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 12,
"output_tokens": 15
}
}
返回结果字段说明:
id:本次消息的唯一标识符。type:始终为message。role:始终为assistant。content:回复内容数组,每个元素包含type(如text)和对应的内容。model:处理请求的模型名称。stop_reason:停止原因。稳定取值包括end_turn、max_tokens、stop_sequence、tool_use、pause_turn(可把当前 assistant 内容原样回传以继续)、refusal和model_context_window_exceeded。stop_sequence:如果因自定义停止序列而停止,显示匹配的停止序列文本。stop_details:当stop_reason为refusal时,可能包含拒绝类别和说明。usage:token 使用统计。input_tokens是未缓存输入;cache_creation_input_tokens和cache_read_input_tokens分别是缓存写入与读取;output_tokens是全部输出 token 数。若返回output_tokens_details.thinking_tokens,该值是output_tokens的子集,计算总量或费用时不要再次相加。该明细没有权威计数时可能为null或省略。usage.cache_creation:可选的缓存写入 TTL 明细,包含ephemeral_5m_input_tokens与ephemeral_1h_input_tokens。对象存在时,两项之和等于cache_creation_input_tokens;字段为null或省略表示当前响应没有可用的 TTL 拆分,不能按0解读。usage.cost:非流式响应可能包含 Ace Data Cloud 记录的额度消耗对象,其中amount是本次实际消耗、currency是计量单位,list_amount是折扣前金额(如有)。Fable 5.1 的官方缓存读取基价为 $0.25/百万 Token,5 分钟与 1 小时缓存写入基价分别为 $12.50 和 $20/百万 Token;平台实际价格按套餐折扣换算。
¶ 系统提示词
Claude Messages API 支持通过 system 字段设定系统提示词,用于定义模型的行为、角色和上下文。
¶ Python 示例
import requests
url = "https://api.acedata.cloud/v1/messages"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"system": "你是一位专业的中文翻译助手,请将用户输入的英文翻译成中文。",
"messages": [
{"role": "user", "content": "The quick brown fox jumps over the lazy dog."}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
通过设置 system 提示词,可以精确地控制 Claude 的角色和行为方式。
¶ 流式响应
该接口也支持流式响应,将 stream 参数设为 true 即可获得逐步返回的效果,非常适合在网页中实现逐字显示。
¶ Python 示例
import requests
url = "https://api.acedata.cloud/v1/messages"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"stream": True,
"messages": [
{"role": "user", "content": "Hello, Claude"}
]
}
response = requests.post(url, json=payload, headers=headers, stream=True)
for line in response.iter_lines():
if line:
print(line.decode("utf-8"))
流式响应以 Server-Sent Events (SSE) 格式返回,每行以 event: 和 data: 为前缀。流式事件类型包括:
message_start:消息开始,包含消息的基本信息和模型名称。content_block_start:内容块开始。content_block_delta:内容块增量更新,包含新生成的文本片段。content_block_stop:内容块结束。message_delta:消息级别的增量更新,包含stop_reason和最终的usage信息。output_tokens_details.thinking_tokens的权威值只应从最后一个message_delta.usage读取,不要跨事件累加。message_stop:消息结束。
输出效果如下:
event: message_start
data: {"type":"message_start","message":{"id":"msg_01XFDUDYJgAACzvnptvVoYEL","type":"message","role":"assistant","content":[],"model":"claude-sonnet-4-20250514","stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":12,"output_tokens":0}}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hi"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"! My name is"}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Claude. How can I help you today?"}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":15}}
event: message_stop
data: {"type":"message_stop"}
可以看到,流式响应中 content_block_delta 事件包含了逐步生成的文本内容,通过拼接所有 text_delta 即可获得完整回复。
¶ JavaScript 示例
const options = {
method: "POST",
headers: {
accept: "application/json",
authorization: "Bearer {token}",
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
stream: true,
messages: [{ role: "user", content: "Hello, Claude" }],
}),
};
const response = await fetch("https://api.acedata.cloud/v1/messages", options);
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(decoder.decode(value));
}
¶ 多轮对话
如果您想要对接多轮对话功能,需要在 messages 数组中交替排列 user 和 assistant 角色的消息,将之前的对话历史一并传入。
¶ Python 示例
import requests
url = "https://api.acedata.cloud/v1/messages"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello, my name is Alice."},
{"role": "assistant", "content": "Hello Alice! Nice to meet you. How can I help you today?"},
{"role": "user", "content": "What is my name?"}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
返回结果如下:
{
"id": "msg_01Y1wfQmd89g968TVbFu57Yc",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Your name is Alice, as you just told me!"
}
],
"model": "claude-sonnet-4-20250514",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 40,
"output_tokens": 14
}
}
通过在 messages 中传递完整的对话历史,Claude 可以结合上下文进行准确的回答。
¶ 深度思考模型
Claude 的 thinking 与 thinking summary 是两个不同概念:模型可以进行内部推理,但 API 不会返回原始思维链。需要展示推理过程时,API 返回的是经过处理的摘要。
当前模型建议使用 adaptive thinking,并通过 output_config.effort 控制总体推理投入:
import requests
url = "https://api.acedata.cloud/v1/messages"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-opus-5",
"max_tokens": 16000,
"thinking": {
"type": "adaptive",
"display": "summarized"
},
"output_config": {
"effort": "high"
},
"messages": [
{"role": "user", "content": "What is the sine of 30 degrees?"}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
响应中的 thinking 块形如:
{
"type": "thinking",
"thinking": "The problem asks for a standard trigonometric value...",
"signature": "opaque-signature"
}
display: "summarized"返回可读的思考摘要;它不是原始思维链。display: "omitted"返回thinking: "",但仍保留 opaquesignature以支持后续对话。display: "updates"是 Anthropic 的 beta 模式,需携带anthropic-beta: thinking-display-updates-2026-08-18。官方定义是隐藏推理正文、显示工具调用之间的简短进度更新。平台按原值传递该参数和 beta 请求头,不会强制改写为summarized;实际输出内容取决于所选模型的支持情况。- Messages 请求使用
thinking、output_config和采样参数。支持的取值以所选模型说明为准;参数错误会在响应中说明。 - Fable 5.1、Fable 5、Opus 5、Sonnet 5、Opus 4.8 和 Opus 4.7 的 display 默认值为
omitted;Opus 4.6、Sonnet 4.6 及更早支持 thinking 的模型默认使用summarized。 - Display 只影响返回内容和流式延迟,不关闭推理,也不减少 thinking token 的计费。
- 是否默认启用 thinking 与 display 默认值是两个独立问题。Opus 5、Sonnet 5 默认启用 adaptive thinking;对 Opus 5,省略
thinking等同于 adaptive,省略output_config.effort等同于high。Opus 4.8、4.7 和 4.6 需要显式启用。 - Thinking 与最终正文共同占用
max_tokens输出预算。预算过小时,thinking 可能占用大部分额度,使正文为空或被截断;请提高max_tokens,或使用low/mediumeffort 控制推理投入。 - 对允许关闭 thinking 的模型,可传
thinking: {"type":"disabled"}。between_tools等新增模式以及 effort 组合取决于所选模型;平台不依据旧的型号规则提前拒绝。 - 固定思考预算可通过
budget_tokens传递;新模型通常使用thinking.type=adaptive和output_config.effort。是否支持固定预算或关闭思考,以所选模型的当前能力为准。 - 多轮对话和工具调用时,应将 assistant 返回的完整 thinking 块及 signature 原样传回;不要修改或自行生成 signature。
- 参数透传不代表每个模型都支持所有功能。跨协议调用仍需转换,
redacted_thinking等内容的支持范围和错误由实际处理请求的模型服务决定。
流式请求中,summarized 会产生 thinking_delta;omitted 不产生 thinking_delta,只保留 thinking 块生命周期和 signature_delta。
¶ 视觉模型
Claude 支持多模态输入,可以同时处理文本和图像。在 Messages API 中,通过将 content 设为数组格式,并传入图像内容块即可使用视觉能力。
¶ 使用 Base64 编码图像
import base64
import requests
url = "https://api.acedata.cloud/v1/messages"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
# 读取并编码图片
with open("image.png", "rb") as f:
image_data = base64.standard_b64encode(f.read()).decode("utf-8")
payload = {
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_data
}
},
{
"type": "text",
"text": "What's in this image?"
}
]
}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
¶ 使用 URL 图像
import requests
url = "https://api.acedata.cloud/v1/messages"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://cdn.acedata.cloud/ueugot.png"
}
},
{
"type": "text",
"text": "What's in this image?"
}
]
}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
¶ cURL 示例
curl -X POST 'https://api.acedata.cloud/v1/messages' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://cdn.acedata.cloud/ueugot.png"
}
},
{
"type": "text",
"text": "What'\''s in this image?"
}
]
}
]
}'
支持的图片格式包括:image/jpeg、image/png、image/gif、image/webp。
¶ 文档与 PDF
PDF 使用 document 内容块,支持 Base64 与 URL 两种稳定来源。Base64 来源必须使用 application/pdf:
import base64
with open("report.pdf", "rb") as f:
pdf_data = base64.standard_b64encode(f.read()).decode("utf-8")
payload = {
"model": "claude-fable-5-1",
"max_tokens": 1024,
"messages": [{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_data
},
"title": "Quarterly report"
},
{"type": "text", "text": "Summarize this PDF."}
]
}]
}
URL 来源写作 {"type":"url","url":"https://example.com/report.pdf"}。document 还支持 text/plain 与由 text/image 块组成的 content 来源;可选字段包括 title、context 和 citations。Files API 的 file_id 来源属于独立 beta 功能,不在本接口的稳定契约内。
¶ 提示缓存
顶层 cache_control 会自动把缓存断点放在最后一个可缓存块上:
payload = {
"model": "claude-fable-5-1",
"max_tokens": 1024,
"cache_control": {"type": "ephemeral", "ttl": "5m"},
"system": "You are an expert on this reference material.",
"messages": [{"role": "user", "content": "Summarize the key points."}]
}
需要精确控制位置时,也可把同样的 cache_control 写在 text、image、document、tool_use、tool_result 内容块或工具定义上。ttl 支持 5m(默认)与 1h;请通过 usage.cache_creation_input_tokens 和 usage.cache_read_input_tokens 判断缓存写入与命中。
当响应提供 usage.cache_creation 时,ephemeral_5m_input_tokens + ephemeral_1h_input_tokens = cache_creation_input_tokens。若 cache_creation 为 null 或省略,表示只有缓存写入总量、没有权威 TTL 拆分;此时不要将任一 bucket 当作已知的 0,计费和总量仍以 aggregate 字段为准。
返回结果示例:
{
"id": "msg_01NCrxpZmV17bhQJJRQEFEb9",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "This image shows an API request configuration interface for what appears to be an AI chat completion service. The interface includes parameters for model selection, messages, stream mode, and max tokens settings."
}
],
"model": "claude-sonnet-4-20250514",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 1570,
"output_tokens": 52
}
}
¶ 工具调用(Tool Use)
Claude Messages API 原生支持工具调用功能,允许模型在需要时调用您预定义的工具/函数。
¶ Python 示例
import requests
url = "https://api.acedata.cloud/v1/messages"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
],
"messages": [
{"role": "user", "content": "What's the weather like in San Francisco?"}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
当模型决定调用工具时,返回结果中 content 会包含 tool_use 类型的内容块:
{
"id": "msg_01Aq9w938a90dw8q",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Let me check the weather in San Francisco for you."
},
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lgs",
"name": "get_weather",
"input": {
"location": "San Francisco, CA"
}
}
],
"model": "claude-sonnet-4-20250514",
"stop_reason": "tool_use",
"stop_sequence": null,
"usage": {
"input_tokens": 120,
"output_tokens": 68
}
}
注意 stop_reason 为 tool_use,表示模型需要调用工具。收到该结果后,您需要执行工具函数并将结果以 tool_result 的形式回传给模型:
payload = {
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
],
"messages": [
{"role": "user", "content": "What's the weather like in San Francisco?"},
{
"role": "assistant",
"content": [
{"type": "text", "text": "Let me check the weather in San Francisco for you."},
{"type": "tool_use", "id": "toolu_01A09q90qw90lq917835lgs", "name": "get_weather", "input": {"location": "San Francisco, CA"}}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lgs",
"content": "Sunny, 72°F"
}
]
}
]
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
模型会基于工具返回的结果,生成最终的自然语言回复。
¶ 与 Chat Completion API 的区别
Ace Data Cloud 同时提供两种 Claude API 格式,两者的主要区别如下:
Messages API 的 usage.input_tokens 仅表示未缓存输入,cache_read_input_tokens 与 cache_creation_input_tokens 是独立计费分桶;三者会分别按对应价格计算。
| 特性 | Messages API (/v1/messages) |
Chat Completion API (/v1/chat/completions) |
|---|---|---|
| 格式 | Anthropic 原生格式 | OpenAI 兼容格式 |
| 系统提示词 | 独立的 system 字段 |
通过 messages 中 role: "system" 传递 |
| 响应结构 | content 数组(支持多种类型) |
choices 数组(包含 message) |
| 流式格式 | SSE 事件(多种事件类型) | SSE data 行 |
| 深度思考 | 原生 thinking 与 output_config.effort |
模型默认策略与兼容参数 |
| 工具调用 | 原生 tools + input_schema |
OpenAI 兼容的 tools 格式 |
| Token 统计 | output_tokens_details.thinking_tokens |
completion_tokens_details.reasoning_tokens |
如果您的系统已经对接了 OpenAI 格式的 API,可以使用 Chat Completion API 来无缝切换。如果您需要使用 Claude 的全部原生能力,建议使用 Messages API。
¶ 错误处理
公开接口的错误响应使用 Ace Data Cloud 平台 envelope:error.code 是稳定错误码,error.message 是说明,trace_id 用于排查请求。常见 HTTP 状态包括:
400:请求参数或协议内容无效。401:授权令牌无效、缺失或过期。403:禁止访问、余额不足或配额受限。404:API 或模型不存在。413:请求体过大。429:请求过多。500/503/504:服务错误、暂时不可用或处理超时。
¶ 错误响应示例
{
"error": {
"code": "api_error",
"message": "fetch failed"
},
"trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}
该错误结构是 Ace Data Cloud 的运行时契约,不等同于 Anthropic 官方错误 envelope;请按 HTTP 状态与 error.code 处理。
¶ 结论
通过本文档,您已经了解了如何使用 Claude Messages API 以 Anthropic 原生格式调用 Claude 的对话功能。Messages API 支持基本对话、系统提示词、流式响应、多轮对话、深度思考、视觉理解、PDF、提示缓存和工具调用等丰富功能。如有任何问题,请随时联系我们的技术支持团队。
