メインコンテンツへスキップ
POST
POST /v1/messages を使用して、Anthropic メッセージ形式で Claude にリクエストを送信します。 これらの例では、公式の Anthropic SDK に CometAPI のベース URL を設定し、 $COMETAPI_KEY から API キーを読み取ります。
フィールドの定義とモデル固有のオプションについては、次の公式資料を参照してください: Messages API リファレンス. OpenAI 互換のリクエストについては、次を参照してください: チャット補完.
x-api-key または Authorization: Bearer を使用して認証します。Anthropic SDK が使用するのは x-api-keyです。HTTP の例には anthropic-version: 2023-06-01 が含まれます。

クイックスタート

次の例では、3 つの検索クエリを要求します。$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 を省略しています。サンプリング制御を追加する前に、選択した モデルのパラメータに関するドキュメントを確認してください。

プロンプトをキャッシュする

リクエスト間で再利用する参照資料にキャッシュのブレークポイントを設定します。 実行する前に、参照資料を 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 。サンプルを試す際に使用できます。

レスポンスをストリーミングする

Server-Sent Events を使用するには stream: true を設定します。SDK はテキストの断片を 到着するたびに公開します:
メッセージストリームには、message_start、コンテンツブロックイベント、message_delta、 および message_stop が含まれます。コンテンツブロックには、テキスト、思考、ツールの動作が含まれることがあります。 テキストブロックでは、content_block_delta に text_delta が含まれます。最終的な使用量 と停止理由は message_delta から読み取ります。

推論量を制御する

output_config.effort を設定して推論量を調整します。この例では low を使って短い説明を求め、ストリーミングされたメッセージの完了を待ちます:
推論量のレベルを選択するには、次の公式資料を参照してください: 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 を指定して送信します。 2 つのリクエストの間で assistant のコンテンツをすべて保持してください。 次の例では、架空の注文結果を渡し、その結果を使用するよう Claude に依頼します:

レスポンスの例

ストリーミング(Streaming)を使用しないリクエストはメッセージオブジェクトを返します。次の例では、 テキストと使用量のフィールドを、例示用のメッセージ識別子とともに示します:
停止理由は次のアクションを示します。end_turn は回答の完了を示し、 max_tokens は出力が上限に達したことを示し、tool_use はクライアントツールの 結果を要求します。pause_turn を返すサーバーツールのターンでは、 返された assistant のコンテンツを変更せずに続行します。

承認

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年11月14日