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

# モデル呼び出し前にリクエストコストを見積もる

> モデル呼び出し前に、モデルディレクトリの価格と入力サイズ、出力上限、またはタスク数を組み合わせて CometAPI リクエストのコストを見積もります。

モデル呼び出し前に、モデルディレクトリの価格と、そのエンドポイントで課金される単位（トークン、画像、音声の長さ、または動画タスク）を組み合わせてコストを見積もります。見積もりは予算のガードとして扱い、リクエスト完了後は実際の usage と請求記録を使用してください。

## トークンベースの呼び出しを見積もる

以下の Python の例では、設定された価格の値からトークンベースのリクエストコストを見積もります。

```python theme={null}
import math
import os

prompt = "Write a short product description for CometAPI."
max_output_tokens = 200

input_price_per_1m = float(os.environ["MODEL_INPUT_PRICE_PER_1M"])
output_price_per_1m = float(os.environ["MODEL_OUTPUT_PRICE_PER_1M"])

estimated_input_tokens = math.ceil(len(prompt) / 4)

estimated_cost = (
    estimated_input_tokens * input_price_per_1m
    + max_output_tokens * output_price_per_1m
) / 1_000_000

print(f"Estimated maximum cost: ${estimated_cost:.6f}")
```

結果は呼び出し前の見積もりです。

```text theme={null}
Estimated maximum cost: $0.000123
```

## 最大出力予算を設定する

以下のリクエストでは生成される出力に上限を設け、見積もりに上限値を持たせています。

```bash theme={null}
curl https://api.cometapi.com/v1/chat/completions \
  -H "Authorization: Bearer $COMETAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "your-model-id",
    "messages": [
      {
        "role": "user",
        "content": "Write a short product description for CometAPI."
      }
    ],
    "max_completion_tokens": 200
  }'
```

レスポンスには、モデル呼び出し後の実際の usage が含まれます。

```json theme={null}
{
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 42,
    "total_tokens": 52
  }
}
```

## タスクベースの呼び出しを見積もる

以下の JavaScript の例では、画像生成や動画生成のようなタスクベースのワークフローを見積もります。

```javascript theme={null}
const taskCount = 3;
const pricePerTask = Number(process.env.MODEL_PRICE_PER_TASK);

const estimatedCost = taskCount * pricePerTask;

console.log(`Estimated maximum cost: $${estimatedCost.toFixed(4)}`);
```

結果はタスク予算です。

```text theme={null}
Estimated maximum cost: $0.4500
```

## よくあるエラー

| Error            | Fix                                                 |
| ---------------- | --------------------------------------------------- |
| 誤ったモデルの価格を使用している | モデルディレクトリ内の同じ model ID の価格をコピーしてください。               |
| 出力トークンを無視している    | `max_completion_tokens` またはエンドポイント固有の出力上限を設定してください。 |
| 見積もりを請求額として扱っている | 呼び出し後に見積もりと実際の usage を比較してください。                     |
| タスクの乗数が抜けている     | 画像、音声、動画では、課金単位がタスクごと、秒ごと、または生成アセットごとかを確認してください。    |

## 関連リンク

* [料金](https://www.cometapi.com/pricing/)
* [モデルディレクトリ](https://www.cometapi.com/models/)
* [モデルページ](/ja/overview/models)
* [料金について](/ja/pricing/about-pricing)
* [CometAPI クイックスタート](/ja/overview/quick-start)

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "TechArticle",
        "@id": "https://apidoc.cometapi.com/guides/how-to-estimate-cost-before-calling-a-model",
        "headline": "モデルを呼び出す前にコストを見積もるにはどうすればよいですか？",
        "description": "モデルディレクトリの料金情報と入力サイズ、出力上限、またはタスク数を組み合わせることで、モデル呼び出し前に CometAPI のリクエストコストを見積もります。",
        "url": "https://apidoc.cometapi.com/guides/how-to-estimate-cost-before-calling-a-model",
        "author": {
          "@type": "Organization",
          "name": "CometAPI"
        },
        "publisher": {
          "@type": "Organization",
          "name": "CometAPI",
          "url": "https://www.cometapi.com"
        }
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "CometAPI ドキュメント",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "ガイド",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "モデルを呼び出す前にコストを見積もるにはどうすればよいですか？",
            "item": "https://apidoc.cometapi.com/guides/how-to-estimate-cost-before-calling-a-model"
          }
        ]
      }
    ]
    }
    `}
</script>
