跳轉到主要內容
POST
使用 POST /v1/messages 以 Anthropic 訊息格式傳送 Claude 請求。 範例會使用 CometAPI 基礎 URL 設定官方 Anthropic SDK,並 從 $COMETAPI_KEY 讀取您的 API 金鑰。
如需欄位定義和模型專屬選項,請參閱官方 Messages API 參考資料. 如需 OpenAI 相容請求,請參閱 聊天補全.
使用 x-api-key 或 Authorization: Bearer 進行驗證。Anthropic SDK 使用 x-api-key。HTTP 範例包含 anthropic-version: 2023-06-01。

快速開始

以下範例會請求三個搜尋查詢。請先設定 $COMETAPI_KEY,再 執行這些範例。針對 Python 安裝 anthropic,或針對 JavaScript 安裝 @anthropic-ai/sdk:
回應包含一個 content 陣列。讀取 type 為 text 的區塊; 思考和工具區塊可能會出現在文字之前。

控制自適應思考

將 thinking.type 設為 adaptive,並選擇一個 output_config.effort 值。 以下範例使用 xhigh,並從串流中讀取完成的訊息:
思考會占用 max_tokens 輸出限制的一部分。請為最終 答案以及思考預留空間。回應可以包含沒有可見 思考區塊的文字。請在對話 記錄中原封不動地保留任何傳回的思考區塊。 如需模型專屬的思考設定,請參閱 思考. 這些範例省略了 temperature、top_p 和 top_k;新增取樣控制項前,請查閱所選 模型的參數文件。

快取 Prompt

在每次請求之間重複使用的參考資料上放置快取中斷點。 在執行此範例前,請將參考資料儲存為 UTF-8 reference.txt 檔案。 請使用符合所選模型 最小可快取長度:
使用相同的模型、參考文字和思考設定再次執行請求。 檢查使用量計數器,以判斷請求是否重複使用了前綴:
  • cache_creation_input_tokens 計算寫入快取的 Token 數量。
  • cache_read_input_tokens 計算從快取讀取的 Token 數量。
  • input_tokens 計算在這些快取計數器之外處理的輸入。
在重複請求時,cache_read_input_tokens 會回報從快取讀取了多少輸入 Token 。OpenAPI Prompt 快取 範例包含一份完整的虛構參考資料,您可以將其儲存為 reference.txt 以嘗試此範例。

串流回應

將 stream: true 設為使用 Server-Sent Events。SDK 會在文字片段抵達時將其公開: 。
訊息串流包含 message_start、內容區塊事件、message_delta, 以及 message_stop。內容區塊可包含文字、思考或工具活動。 對於文字區塊,content_block_delta 包含一個 text_delta。從最終使用量中讀取 以及來自 message_delta 的停止原因。

控制推理量

設定 output_config.effort 以引導推理量。此範例使用 low 來取得簡短說明,並等待完成的串流訊息:
使用官方 effort 參考資料 來選擇 effort 等級。另行設定 max_tokens 以限制輸出長度。

使用伺服器工具

伺服器工具會在 API 請求期間執行,並傳回結果區塊,與 Claude 的答案一同提供。
擷取一篇論文,並要求 Claude 在答案中使用擷取到的文件。 此範例會串流傳輸回應,並檢查最終文字和 web_fetch_tool_result 區塊:
回應會將一個 server_tool_use 區塊與一個 web_fetch_tool_result 配對,後者包含擷取到的文件或工具錯誤。

傳回用戶端工具結果

對於用戶端工具,Claude 會傳回一個 tool_use 區塊。執行您的應用程式函式 並在具有相符 tool_result 的 tool_use_id 區塊中傳送其結果。 在兩個請求之間保留完整的助理內容。 此範例提供虛構的訂單結果,並要求 Claude 使用該結果:

回應範例

非串流請求會傳回訊息物件。以下範例顯示其 文字和使用量欄位,以及示例訊息識別碼:
停止原因說明下一個動作。end_turn 會完成答案, max_tokens 表示輸出已達限制,而 tool_use 要求用戶端工具 結果。對於傳回 pause_turn 的伺服器工具回合,請繼續使用 傳回的助理內容,且不要變更。

授權

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 feature identifiers required by a specific beta API feature. Omit this header for the examples on this page.

主體

application/json
model
string
預設值:claude-opus-5
必填

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

範例:

"claude-opus-5"

messages
object[]
必填

Conversation history. Use user and assistant messages with text strings or content-block arrays. Return complete assistant content blocks when continuing a tool call.

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

Sampling temperature. The examples omit sampling overrides.

必填範圍: 0 <= x <= 1
top_p
number

Nucleus sampling threshold. The examples omit sampling overrides.

必填範圍: 0 <= x <= 1
top_k
integer

Limits sampling to the k most likely tokens. The examples omit sampling overrides.

必填範圍: x >= 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

Thinking configuration. The adaptive example uses type adaptive and sets output_config.effort separately.

tools
object[]

Client tools define a name and input_schema. Server tools use a versioned type and name, such as the web_fetch and web_search examples on this page.

tool_choice
object

Controls how the model uses tools.

metadata
object

Request metadata for tracking and analytics.

output_config
object

Configuration for reasoning effort and structured output.

service_tier
enum<string>

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

可用選項:
auto,
standard_only

回應

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

id
string

Message identifier returned by the API.

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

Model ID reported by the response.

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.

最後修改於 2026年9月8日