Claude 原生协议(Messages)
用 Anthropic 官方 SDK 和原生参数调用 Claude,支持自适应思考、提示缓存、工具调用和流式输出。
POST /anthropic/v1/messages需要 Gateway Key支持流式更新于 2026-11-20快速上手
- 接口:
POST /anthropic/v1/messages,与 Anthropic Messages API 格式相同;SDK 的base_url填https://llmapi.memene.cn/anthropic,不要带/v1。 - 鉴权:
x-api-key或Authorization: Bearer均可,填本平台的 API Key。 - 必填:
model、max_tokens和messages;系统提示放在顶层system,不是消息角色。 - 计费:按实际 Token 用量计费,缓存写入与缓存读取按模型的对应价格计算,见 可用模型。
from anthropic import Anthropic
client = Anthropic(
base_url="https://llmapi.memene.cn/anthropic",
auth_token="gw_memene_<keyId>_<secret>",
)
msg = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
system="你是一名简洁的中文助手。",
messages=[{"role": "user", "content": "用一句话解释什么是向量数据库。"}],
)
print(msg.content[0].text)
print(msg.usage)回答在 content 数组中,文本块的 type 为 text;usage 中的 input_tokens、output_tokens、cache_creation_input_tokens、cache_read_input_tokens 是本次计费用量。
{
"id": "msg_abc123",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "The meaning of life is a profound philosophical question..."
}
],
"model": "claude-sonnet-5-5",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 14,
"output_tokens": 48
}
}什么时候用这个入口
Claude 模型也可以通过 Chat Completions 调用。以下情况推荐使用 Messages:
- 已有基于 Anthropic SDK 的代码,或使用 Claude Code 等依赖 Messages 协议的工具;
- 需要
thinking、output_config、cache_control等 Claude 原生参数; - 需要读取思考块、工具调用块等原生响应结构。
Claude 模型列表与价格见 可用模型(厂商为 Anthropic 的模型)。其他厂商的模型也能通过此入口调用,但 Claude 专有参数对它们不生效。
流式输出
设置 stream: true 或使用 SDK 的 messages.stream。超时规则与 Chat Completions 相同:单次请求最长约 30 分钟;流式请求连续 30 秒没有新数据视为中断;长输出建议始终使用流式。
with client.messages.stream(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "写一首关于秋天的四行短诗。"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print("\n", stream.get_final_message().usage)流式模式下响应以 SSE 格式返回。事件类型包括 message_start、content_block_start、content_block_delta、content_block_stop、message_delta、message_stop。
自适应思考与 effort
当前在售的 Claude 模型使用自适应思考:thinking: {"type": "adaptive"} 让模型自行决定是否以及思考多少,output_config.effort 控制整体投入程度。
effort常见取值为low、medium、high,部分模型另支持xhigh、max,见可用模型中的推理档位。- 较新的 Claude 模型不再接受
thinking: {"type": "enabled", "budget_tokens": N}的手动预算写法,会返回 400。 - 思考内容以
thinking块返回,计入output_tokens;max_tokens需要为思考和回答都留出空间。 - 在多轮对话中切换
effort可能使提示缓存失效。
msg = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=16000,
thinking={"type": "adaptive"},
output_config={"effort": "high"},
messages=[{"role": "user", "content": "证明:任意两个奇数之和是偶数。"}],
)
for block in msg.content:
if block.type == "thinking":
print("[思考]", block.thinking)
elif block.type == "text":
print(block.text)提示缓存
多次请求共用的长系统提示、文档或工具定义,可以用 cache_control 标记为可缓存。首次请求按缓存写入价计费,之后在缓存有效期内命中的部分按更低的缓存读取价计费;本平台按响应 usage 中的实际缓存用量结算。
msg = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
system=[{
"type": "text",
"text": LONG_DOCUMENT, # 多次请求共用的长文本
"cache_control": {"type": "ephemeral"},
}],
messages=[{"role": "user", "content": "这份文档的结论是什么?"}],
)
# 首次写入缓存,之后命中缓存的部分按缓存读取价计费
print(msg.usage.cache_creation_input_tokens, msg.usage.cache_read_input_tokens)请求参数
下表为常用参数,其余参数(如 tools、tool_choice、metadata)与 Anthropic 定义一致。
Content-Type:application/json
| 参数 | 必填 | 说明 |
|---|---|---|
| model string | 必填 | 模型 ID,如 "claude-sonnet-5-5"。 |
| messages object[] | 必填 | 对话消息数组。 |
| role string | 必填 | 消息角色,"user" 或 "assistant"。 |
| content string | object[] | 必填 | 消息内容,纯文本或内容块数组。 |
| max_tokens number | 必填 | 生成的最大 token 数。Anthropic API 中此字段为必填。 |
| stream boolean | 可选 | 是否启用流式输出,默认 false。 |
| temperature number | 可选 | 采样温度,范围 0~1。默认 1。 |
| top_p number | 可选 | 核采样参数。 |
| top_k number | 可选 | Top-K 采样参数。 |
| system string | object[] | 可选 | 系统提示词。 |
| stop_sequences string[] | 可选 | 停止序列。 |
常见问题
Node.js SDK 返回 401?
请使用 authToken 传入 API Key(如上方示例),SDK 会发送 Authorization: Bearer。也可以直接用 cURL 的 x-api-key 头。确认 baseURL 是 https://llmapi.memene.cn/anthropic,不带 /v1。
支持工具调用和图片输入吗?
支持。工具使用 Anthropic 原生的 tools(name、description、input_schema)与 tool_use / tool_result 内容块;图片和 PDF 以 image、document 内容块传入(Base64 或 URL)。Anthropic 的服务端工具(如网页搜索)依赖厂商侧计价,本平台不保证可用。
出错时如何排查?
本平台的错误码与 Chat Completions 一致,见 常见错误码;Anthropic 返回的错误会原样透传。