Skip to main content
POST
CometAPI 原生支持 Anthropic 消息 API,让你能够直接访问 Claude 模型及其 Anthropic 专属功能。对于自适应思考、Prompt 缓存和 effort 控制等 Claude 能力,请使用此端点。
请将官方的 Anthropic Messages API reference 作为完整参数列表、响应 schema 和 Claude 特定行为的权威来源。本 CometAPI 页面说明了如何通过 CometAPI 发送该请求结构。
随着 Claude 功能的演进,Anthropic 的请求参数和响应字段可能会发生变化。请查看 Anthropic Messages API 文档 以获取最新的完整参数列表和特定提供商行为。
许多较新的 Claude 模型在 Messages API 上会拒绝非默认的 temperaturetop_ptop_k 值。除非你已经确认所选模型支持这些采样字段,否则请省略它们。如果模型返回不支持或参数已弃用的错误,请从请求中移除相应字段。
x-api-keyAuthorization: Bearer 两种请求头都支持用于身份验证。官方 Anthropic SDK 默认使用 x-api-key

快速开始

要通过 CometAPI 使用官方 Anthropic SDK,请设置 base URL:

控制自适应思考

使用 output_config.effort 搭配自适应思考来控制 Claude 在响应中投入多少工作量。较新的 Claude 模型会拒绝旧版的手动 thinking 结构 thinking={"type": "enabled", "budget_tokens": ...}
当你使用更高的 effort 级别时,思考 Token 会计入你的 max_tokens 限制。请将 max_tokens 设置得足够高,以同时容纳思考内容和最终答案。

缓存 Prompt

为减少后续请求的延迟和成本,可以缓存较大的 system Prompt 或对话前缀。为需要缓存的 content 块添加 cache_control
缓存使用情况会在响应的 usage 字段中报告:
  • cache_creation_input_tokens — 写入缓存的 tokens(按更高费率计费)
  • cache_read_input_tokens — 从缓存读取的 tokens(按较低费率计费)
Prompt 缓存要求被缓存的 content 块至少包含 1,024 tokens。短于此长度的内容将不会被缓存。

流式输出响应

要使用 Server-Sent Events (SSE) 进行流式输出(Streaming)响应,请设置 stream: true。事件会按以下顺序到达:
  1. message_start — 包含消息元数据和初始 usage
  2. content_block_start — 标记每个 content 块的开始
  3. content_block_delta — 增量文本块(text_delta
  4. content_block_stop — 标记每个 content 块的结束
  5. message_delta — 最终的 stop_reason 和完整的 usage
  6. message_stop — 表示流结束

控制 effort

要控制 Claude 在生成响应时投入多少 effort,请使用 output_config.effort

使用服务器工具

Claude 支持在 Anthropic 基础设施上运行的服务器端工具:
从 URL 获取并分析内容:

响应示例

来自 CometAPI Anthropic 端点的典型响应:

与 OpenAI 兼容端点对比

授权

x-api-key
string
header
必填

Your CometAPI key passed via the x-api-key header. Authorization: Bearer $COMETAPI_KEY is also supported.

请求头

anthropic-version
string
默认值:2023-06-01

The Anthropic API version to use. Defaults to 2023-06-01.

示例:

"2023-06-01"

anthropic-beta
string

Comma-separated list of beta features to enable. Examples: max-tokens-3-5-sonnet-2024-07-15, pdfs-2024-09-25, output-128k-2025-02-19.

请求体

application/json
model
string
必填

The Claude model to use. See the Models page for available Claude model IDs.

示例:

"claude-sonnet-5"

messages
object[]
必填

The conversation messages. Must alternate between user and assistant roles. Each message's content can be a string or an array of content blocks (text, image, document, tool_use, tool_result). There is a limit of 100,000 messages per request.

max_tokens
integer
必填

The maximum number of tokens to generate. The model may stop before reaching this limit. When using thinking, the thinking tokens count towards this limit.

必填范围: x >= 1
示例:

1024

system

System prompt providing context and instructions to Claude. Can be a plain string or an array of content blocks (useful for prompt caching).

temperature
number
默认值:1

Model-dependent sampling control. Many newer Claude models reject non-default temperature values on the Messages API. Omit this field unless you have verified that the selected model accepts it; if the model returns an unsupported or deprecated-parameter error, remove the field instead of substituting another sampling value.

必填范围: 0 <= x <= 1
示例:

1

top_p
number

Model-dependent nucleus sampling control. Many newer Claude models reject non-default top_p values on the Messages API. Omit this field unless you have verified support for the selected model. Do not set temperature and top_p together.

必填范围: 0 <= x <= 1
示例:

1

top_k
integer

Model-dependent top-k sampling control. Many newer Claude models reject non-default top_k values on the Messages API. Omit this field unless you have verified support for the selected model.

必填范围: x >= 0
示例:

0

stream
boolean
默认值:false

If true, stream the response incrementally using Server-Sent Events (SSE). Events include message_start, content_block_start, content_block_delta, content_block_stop, message_delta, and message_stop.

stop_sequences
string[]

Custom strings that cause the model to stop generating when encountered. The stop sequence is not included in the response.

thinking
object

Controls Claude thinking when the selected model supports a configurable thinking mode. For newer adaptive-thinking models, use {"type":"adaptive"} with output_config.effort, or omit thinking when adaptive thinking is already the model default. Manual {"type":"enabled","budget_tokens":...} is supported only by older models and is rejected by newer Claude models.

tools
object[]

Tools the model may use. Supports client-defined functions, web search (web_search_20250305), web fetch (web_fetch_20250910), code execution (code_execution_20250522), and more.

tool_choice
object

Controls how the model uses tools.

metadata
object

Request metadata for tracking and analytics.

output_config
object

Configuration for response effort and output format. Field support depends on the selected Claude model.

service_tier
enum<string>

The service tier to use. auto tries priority capacity first, standard_only uses only standard capacity.

可用选项:
auto,
standard_only

响应

200 - application/json

Successful response. When stream is true, the response is a stream of SSE events.

id
string

Unique identifier for this message (e.g., msg_01XFDUDYJgAACzvnptvVoYEL).

type
enum<string>

Always message.

可用选项:
message
role
enum<string>

Always assistant.

可用选项:
assistant
content
object[]

The response content blocks. May include text, thinking, tool_use, and other block types.

model
string

The specific model version that generated this response, such as claude-sonnet-5.

stop_reason
enum<string>

Why the model stopped generating. refusal can be returned as a successful HTTP response when the model declines a request.

可用选项:
end_turn,
max_tokens,
stop_sequence,
tool_use,
pause_turn,
refusal
stop_sequence
string | null

The stop sequence that caused the model to stop, if applicable.

usage
object

Token usage statistics.