Claude 消息接口
- 直接使用 Anthropic Messages 原生结构
- 适合需要
system顶层字段、tool_use、思考模式等特性的场景 - 与 OpenAI 结构互不影响,可以按需混用
认证
所有接口均使用 Bearer Token 进行认证。
也兼容 x-api-key。
获取 API Key:访问控制台 创建。
请求时在请求头中添加:
Authorization: Bearer YOUR_OAIRoute_KEY不传时默认使用稳定版本。需要固定行为时可显式指定。
All endpoints authenticate with a Bearer token.
x-api-key is also accepted.
Get an API key: open the console and create one.
Add it to the request header:
Authorization: Bearer YOUR_OAIRoute_KEYUses the stable version by default. Set it explicitly to pin behavior.
请求参数
Claude 模型 ID。
生成的最大 token 数。该字段在原生结构里是必填的,这一点与 OpenAI 不同。
消息数组,只允许 user 与 assistant 两种角色,且必须交替出现。系统提示不进这里。
取值 user 或 assistant。
消息正文。需要多模态或工具结果时传内容块数组,元素含 type。
内容块类型,取值 text、image、tool_use、tool_result。
type 为 text 时的正文。
type 为 image 时的图片来源,含 type(base64 或 url)、media_type 与 data。
系统提示。放在顶层而不是 messages 里,这是与 OpenAI 结构最主要的差异。
采样温度,取值 0 到 1。
核采样阈值。
每步只从概率最高的 k 个 token 中采样。
停止序列数组。
是否流式返回。
工具清单。与 OpenAI 的 function 包装不同,这里直接给 name、description、input_schema。
工具名。
工具用途说明。
入参的 JSON Schema。
取值 auto、any 或 tool。
思考模式。开启后响应里会多出 thinking 内容块。
The Claude model ID.
Maximum tokens to generate. This field is required in the native schema, which differs from OpenAI.
The message array. Only user and assistant roles are allowed, and they must alternate. System prompts don't go here.
Either user or assistant.
The message body. For multimodal input or tool results, pass an array of content blocks, each with a type.
The content block type: text, image, tool_use, or tool_result.
The body when type is text.
The image source when type is image, containing type (base64 or url), media_type, and data.
The system prompt. It sits at the top level rather than in messages; this is the main difference from the OpenAI schema.
Sampling temperature, from 0 to 1.
Nucleus sampling threshold.
Sample each step only from the k highest-probability tokens.
An array of stop sequences.
Whether to stream.
The tool list. Unlike OpenAI's function wrapper, you give name, description, and input_schema directly.
The tool name.
A description of what the tool does.
The JSON Schema for the inputs.
One of auto, any, or tool.
Thinking mode. When enabled, the response includes thinking content blocks.
响应
消息唯一标识,形如 msg_。
固定为 message。
固定为 assistant。
内容块数组,可能含 text、tool_use、thinking 等多种类型。
结束原因。end_turn 正常结束,max_tokens 触达上限,tool_use 发起工具调用。
输入 token 数。
输出 token 数。
A unique message identifier, in the form msg_.
Always message.
Always assistant.
The content block array, which may contain text, tool_use, thinking, and other types.
Why generation ended. end_turn is normal, max_tokens hit the limit, tool_use means a tool call was issued.
Input token count.
Output token count.
两种结构的对应关系
| OpenAI | Claude 原生 | 说明 |
|---|---|---|
messages[].role = system | system 顶层字段 | 系统提示的位置不同 |
max_tokens 可选 | max_tokens 必填 | 原生结构必须显式给出上限 |
tools[].function | tools[] 直接扁平 | 少一层包装 |
finish_reason | stop_reason | 字段名不同,取值也不同 |
usage.prompt_tokens | usage.input_tokens | 命名差异 |
| OpenAI | Claude native | Notes |
|---|---|---|
messages[].role = system | Top-level system field | System prompts live in different places |
max_tokens optional | max_tokens required | The native schema demands an explicit limit |
tools[].function | tools[] flat | One less wrapper |
finish_reason | stop_reason | Different field names and different values |
usage.prompt_tokens | usage.input_tokens | A naming difference |
支持的模型列表
以下模型均可通过本接口调用,清单由模型目录自动生成,实际可用范围以你的账号分组为准:
Anthropic 系列
claude-opus-5-5旗舰,长程任务与复杂推理claude-sonnet-5-5主力,能力与成本平衡claude-haiku-5-5轻量,响应最快claude-fable-5-1创作向 等
更多模型持续更新中…
GET /api/pricing。The models below can all be called through this endpoint. The list is generated from the model catalog; what your account group actually allows takes precedence:
Anthropic 系列
claude-opus-5-5旗舰,长程任务与复杂推理claude-sonnet-5-5主力,能力与成本平衡claude-haiku-5-5轻量,响应最快claude-fable-5-1创作向 等
更多模型持续更新中…
GET /api/pricing directly.错误码
For error response formats, status codes, and retry guidance see Error codes and rate limits; for troubleshooting steps on common errors see the FAQ.
使用示例
基础对话
Claude 原生结构里 max_tokens 是必填项,不传会直接报错。系统提示放在顶层的 system 字段,不在 messages 数组里。
多轮对话
历史消息同样按顺序放进 messages,助手的回复用 role: assistant 回填。顶层 system 不需要重复,每轮都放一次即可。
使用系统提示词
system 支持字符串,也支持内容块数组。需要带缓存标记时用数组形式,把稳定部分标成可缓存。
流式响应
加 "stream": true 后返回 SSE 事件流。事件类型有 message_start、content_block_delta、message_stop 等,取增量文本看 delta.text。
工具使用
工具定义直接放在 tools 数组,比 OpenAI 结构少一层 function 包装。模型决定调用时,stop_reason 会变成 tool_use。
视觉理解
图片通过内容块传入,type 为 image,来源可以是 URL 或 base64。文字与图片可以混在同一个 content 数组里。
Basic chat
In Claude's native schema max_tokens is required; omitting it errors out. The system prompt goes in the top-level system field, not in the messages array.
Multi-turn chat
History also goes into messages in order, with assistant replies filled back in under role: assistant. The top-level system doesn't need repeating; once per request is enough.
Use a system prompt
system accepts a string or an array of content blocks. Use the array form when you need cache markers, marking the stable part as cacheable.
Streaming response
Adding "stream": true returns an SSE event stream. Event types include message_start, content_block_delta, message_stop, and more; read incremental text from delta.text.
Tool use
Tool definitions go directly in the tools array, one wrapper layer less than the OpenAI schema. When the model decides to call one, stop_reason becomes tool_use.
Vision
Images go in through content blocks with type set to image, sourced from a URL or base64. Text and images can be mixed in the same content array.