> ## Documentation Index
> Fetch the complete documentation index at: https://docs.comfy.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Comfy Router で Claude Sonnet 5 を使う

> Comfy Router 経由で anthropic/claude-sonnet-5 を呼び出します: エンドポイント、リクエスト形状、Router が返すレスポンスについて説明します。

`anthropic/claude-sonnet-5` の API リファレンスです。Anthropic から Comfy Router が提供しています。

## クイックスタート

[Comfy ワークスペース](https://platform.comfy.org/profile/api-keys)でキーを作成し、`COMFY_API_KEY` としてエクスポートします。Python と TypeScript のスニペットは Comfy SDK（`pip install comfy-sdk`、`npm install @comfyorg/sdk`）を使用しています。cURL のスニペットは、同じ呼び出しを生の HTTP で行うものです。

**モデル ID:** `anthropic/claude-sonnet-5`

**エンドポイント:** `POST https://api.comfy.org/v2/models/anthropic/claude-sonnet-5`

<Tabs>
  <Tab title="Wait for the result">
    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # Reads COMFY_API_KEY from the environment.
      # The SDK automatically creates an idempotency key and reuses it for automatic retries.
      with Comfy() as client:
          result = client.models.run(
              "anthropic/claude-sonnet-5",
              {
                  "max_tokens": 16,
                  "messages": [
                      {
                          "content": "Reply with the single word: ok",
                          "role": "user",
                      },
                  ],
              },
          )

      print(result)
      ```

      ```typescript TypeScript theme={null}
      import { comfy } from "@comfyorg/sdk";

      // Reads COMFY_API_KEY from the environment.
      // The SDK automatically creates an idempotency key and reuses it for automatic retries.
      const { data } = await comfy.models.run("anthropic/claude-sonnet-5", {
        max_tokens: 16,
        messages: [
          {
            content: "Reply with the single word: ok",
            role: "user",
          },
        ],
      });

      console.log(data);
      ```

      ```bash cURL theme={null}
      curl https://api.comfy.org/v2/models/anthropic/claude-sonnet-5 \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"max_tokens\": 16, \"messages\": [{\"content\":\"Reply with the single word: ok\",\"role\":\"user\"}]}"
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Queue and collect later">
    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # Reads COMFY_API_KEY from the environment.
      # Each submit() call mints its own Idempotency-Key and reuses it for automatic retries.
      with Comfy() as client:
          handle = client.models.submit(
              "anthropic/claude-sonnet-5",
              {
                  "max_tokens": 16,
                  "messages": [
                      {
                          "content": "Reply with the single word: ok",
                          "role": "user",
                      },
                  ],
              },
          )
          print("request_id:", handle.request_id)  # with the model ID, all another process needs

          # Poll until the request completes, waiting the Retry-After the server names.
          for update in handle.iter_events():
              print(update.status, update.queue_position)

          # The provider's own payload, the same value models.run() returns.
          # A request that failed or was cancelled raises the typed Router error here.
          result = handle.get()

      print(result)
      ```

      ```typescript TypeScript theme={null}
      import { comfy } from "@comfyorg/sdk";

      // Reads COMFY_API_KEY from the environment.
      // Each submit() call mints its own Idempotency-Key and reuses it for automatic retries.
      const handle = await comfy.models.submit("anthropic/claude-sonnet-5", {
        max_tokens: 16,
        messages: [
          {
            content: "Reply with the single word: ok",
            role: "user",
          },
        ],
      });
      console.log("requestId:", handle.requestId); // with the model ID, all another process needs

      // Poll until the request completes, waiting the Retry-After the server names.
      for await (const update of handle.events()) {
        console.log(update.status, update.queuePosition);
      }

      // The same result models.run() returns. A request that failed or was cancelled rejects here.
      const result = await handle.get();

      console.log(result.data);
      ```

      ```bash cURL theme={null}
      # 1. Submit. Router answers 201 with request_id, status_url, response_url and cancel_url.
      curl https://api.comfy.org/v2/models/anthropic/claude-sonnet-5/requests \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"max_tokens\": 16, \"messages\": [{\"content\":\"Reply with the single word: ok\",\"role\":\"user\"}]}"

      # 2. Poll until status is COMPLETED, waiting the Retry-After seconds each response names.
      REQUEST_ID="<request_id from the 201 body>"
      curl -i https://api.comfy.org/v2/models/anthropic/claude-sonnet-5/requests/$REQUEST_ID/status \
        -H "X-API-Key: $COMFY_API_KEY"

      # 3. Collect. 200 with the model's native output, 202 with the status body while it is still running.
      curl https://api.comfy.org/v2/models/anthropic/claude-sonnet-5/requests/$REQUEST_ID \
        -H "X-API-Key: $COMFY_API_KEY"
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## Schema

### 入力

<ParamField body="max_tokens" type="integer" required>
  停止するまでに生成するトークンの最大数。
</ParamField>

<ParamField body="messages" type="object[]" required>
  会話のターン。content ブロックの分類体系の全体については Anthropic Messages API のドキュメントを参照してください。
</ParamField>

<ParamField body="messages[].content" type="object" required>
  文字列の省略形、または content ブロック（text、image、document、tool\_use、tool\_result など）の配列のいずれかです。
</ParamField>

<ParamField body="messages[].role" type="string" required>
  指定可能な値: `user`、`assistant`
</ParamField>

<ParamField body="model" type="string">
  Anthropic のモデル識別子（例: `claude-sonnet-4-5`、`claude-opus-4-7`）。
</ParamField>

<ParamField body="stream" type="boolean">
  true の場合、レスポンスは単一の JSON ボディではなく、Anthropic のメッセージイベントの `text/event-stream` になります。
</ParamField>

<ParamField body="system" type="object">
  トップレベルのシステムプロンプト。文字列または content ブロックの配列のいずれかで、どちらもそのまま Anthropic に渡されます。
</ParamField>

Router が `GET /v2/models/anthropic/claude-sonnet-5/openapi.json` で提供するスキーマから生成されています。これは、リクエストがプロバイダーに到達する前に Router が呼び出しを検証する際に使用するドキュメントと同じものです。

### 出力

<ResponseField name="id" type="string" />

<ResponseField name="model" type="string" />

<ResponseField name="role" type="string" />

<ResponseField name="stop_reason" type="string" />

<ResponseField name="stop_sequence" type="string" />

<ResponseField name="type" type="string" />

<ResponseField name="usage" type="object">
  Anthropic Messages API 呼び出しのトークン使用量。
</ResponseField>

<ResponseField name="usage.cache_creation" type="object">
  Anthropic Messages API 呼び出しにおける、キャッシュ書き込みの入力トークンの TTL 別内訳。
</ResponseField>

<ResponseField name="usage.cache_creation.ephemeral_1h_input_tokens" type="integer" />

<ResponseField name="usage.cache_creation.ephemeral_5m_input_tokens" type="integer" />

<ResponseField name="usage.cache_creation_input_tokens" type="integer" />

<ResponseField name="usage.cache_read_input_tokens" type="integer" />

<ResponseField name="usage.input_tokens" type="integer" />

<ResponseField name="usage.output_tokens" type="integer" />

<ResponseField name="content" type="object[]" required>
  返信の content ブロックを順番に並べたもの。完了したすべてのメッセージに含まれ、そのターンで何も生成されなかった場合は空になります。
</ResponseField>

<ResponseField name="content[].text" type="string">
  ブロックのテキスト。`text` ブロックに含まれ、それ以外の種類には含まれません。
</ResponseField>

<ResponseField name="content[].type" type="string">
  ブロックの種類。`text` のみが `text` を持ち、`thinking`、`redacted_thinking`、`tool_use`、`server_tool_use`、およびツール結果ブロックが、現在 Anthropic が送信するその他の種類であり、このリストは開かれています。
</ResponseField>

## 例

### 入力

```json theme={null}
{
  "max_tokens": 16,
  "messages": [
    {
      "content": "Reply with the single word: ok",
      "role": "user"
    }
  ]
}
```

### 出力

```json theme={null}
{
  "content": [
    {
      "text": "ok",
      "type": "text"
    }
  ],
  "id": "msg_01ExampleInvalidPlaceholder",
  "model": "claude-sonnet-5",
  "role": "assistant",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "type": "message",
  "usage": {
    "input_tokens": 16,
    "output_tokens": 3
  }
}
```

## 出荷前の確認

SDK は `Idempotency-Key` を生成し、自動リトライで再利用します。手動リトライでは元のキーを再利用してください。Router は最大 10 分間接続を保持できます。

リクエストが失敗すると、Router は理由を示す `X-Comfy-Error-Type` レスポンスヘッダーを送信します。`422` は、プロバイダーを呼び出す前に Router が入力を拒否したことを意味します。生成されたアセットは [結果 URL の有効期限](/ja/development/comfy-router/reference#結果アセット) があるため、早めにダウンロードしてください。

<CardGroup cols={3}>
  <Card title="ヘッダー" icon="list" href="/ja/development/comfy-router/quickstart">
    認証、冪等性、リクエスト ID、エラー分類、リトライ間隔、支出上限。
  </Card>

  <Card title="Router API の利用" icon="code" href="/ja/development/comfy-router/quickstart">
    モデルの検出、バリデーションエラー、リトライ、課金。
  </Card>

  <Card title="制限事項" icon="triangle-exclamation" href="/ja/development/comfy-router/limitations">
    Router が現在対応していないことと、代替手段。
  </Card>
</CardGroup>
