> ## 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 請求成本。

在模型呼叫前，將模型目錄價格與端點計費單位結合來估算成本：Token、圖片、音訊長度或影片任務。將估算視為預算防護，然後在請求完成後使用實際用量與帳單記錄。

## 估算以 Token 計費的呼叫

以下 Python 範例會根據已設定的定價值，估算以 Token 計費的請求成本：

```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
  }'
```

回應會在模型呼叫後包含實際使用量：

```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 複制定價。               |
| 忽略輸出 Token | 設定 `max_completion_tokens` 或端點專用的輸出上限。 |
| 將估算當成帳單    | 在呼叫後將估算與實際使用量進行比較。                     |
| 遺漏任務乘數     | 對於圖片、音訊和影片，請確認計費是依任務、每秒，還是每個生成資產計算。    |

## 相關連結

* [價格](https://www.cometapi.com/pricing/)
* [模型目錄](https://www.cometapi.com/models/)
* [模型頁面](/zh-Hant/overview/models)
* [關於定價](/zh-Hant/pricing/about-pricing)
* [CometAPI 快速入門](/zh-Hant/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>
