Skip to main content
POST
CometAPI 原生支援 Anthropic 訊息 API,讓你能直接存取 Claude 模型及其 Anthropic 專屬功能。對於自適應思考、Prompt 快取與 effort 控制等 Claude 能力,請使用此端點。
請以官方的 Anthropic Messages API reference 作為完整參數清單、回應 schema 與 Claude 專屬行為的權威來源。此 CometAPI 頁面說明如何透過 CometAPI 傳送該請求格式。
隨著 Claude 功能持續演進,Anthropic 的請求參數與回應欄位可能會變動。請查看 Anthropic 訊息 API 文件 以取得最新且完整的參數清單與供應商特定行為。
許多較新的 Claude 模型在 Messages API 上會拒絕非預設的 temperaturetop_ptop_k 值。除非你已確認所選 model 支援,否則請省略這些取樣欄位。若 model 回傳不支援或已棄用參數的錯誤,請從請求中移除該欄位。
驗證同時支援 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 等級時,thinking tokens 會計入你的 max_tokens 限制。請將 max_tokens 設得足夠高,以同時容納 thinking 與最終答案。

快取 Prompt

為了降低後續請求的延遲與成本,可將大型 system prompts 或對話前綴進行快取。將 cache_control 加到應快取的內容區塊:
快取使用情況會在回應的 usage 欄位中回報:
  • cache_creation_input_tokens — 寫入快取的 tokens(以較高費率計費)
  • cache_read_input_tokens — 從快取讀取的 tokens(以較低費率計費)
Prompt 快取要求被快取的內容區塊至少有 1,024 tokens。短於此長度的內容將不會被快取。

串流回應

若要使用 Server-Sent Events (SSE) 進行串流回應,請設定 stream: true。事件會依照以下順序到達:
  1. message_start — 包含訊息中繼資料與初始 usage
  2. content_block_start — 標記每個內容區塊的開始
  3. content_block_delta — 遞增的文字片段(text_delta
  4. content_block_stop — 標記每個內容區塊的結束
  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.