> ## 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.

# Ước tính chi phí request trước khi gọi model

> Ước tính chi phí request CometAPI trước khi gọi model bằng cách kết hợp giá trong model directory với kích thước input, giới hạn output hoặc số lượng tác vụ.

Ước tính chi phí trước khi gọi model bằng cách kết hợp giá trong model directory với đơn vị tính phí của endpoint: token, hình ảnh, thời lượng âm thanh hoặc tác vụ video. Hãy xem phần ước tính này như một cơ chế kiểm soát ngân sách, sau đó sử dụng dữ liệu usage và bản ghi billing thực tế sau khi request hoàn tất.

## Ước tính các lệnh gọi dựa trên token

Ví dụ Python sau đây ước tính chi phí request dựa trên token từ các giá trị pricing đã cấu hình:

```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}")
```

Kết quả là một giá trị ước tính trước khi gọi:

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

## Đặt ngân sách output tối đa

Request sau đây giới hạn output được tạo để phần ước tính có một ngưỡng trên:

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

Phản hồi bao gồm usage thực tế sau khi gọi model:

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

## Ước tính các lệnh gọi dựa trên tác vụ

Ví dụ JavaScript sau đây ước tính một quy trình làm việc dựa trên tác vụ như tạo ảnh hoặc video:

```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)}`);
```

Kết quả là ngân sách cho tác vụ:

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

## Các lỗi thường gặp

| Error                        | Fix                                                                                                                        |
| ---------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| Dùng giá từ sai model        | Sao chép pricing từ đúng model ID trong model directory.                                                                   |
| Bỏ qua output token          | Đặt `max_completion_tokens` hoặc giới hạn output riêng của endpoint.                                                       |
| Coi ước tính như hóa đơn     | So sánh giá trị ước tính với usage thực tế sau lệnh gọi.                                                                   |
| Thiếu hệ số nhân theo tác vụ | Với hình ảnh, âm thanh và video, hãy kiểm tra xem billing được tính theo tác vụ, theo giây hay theo từng tài sản được tạo. |

## Liên kết liên quan

* [Bảng giá](https://www.cometapi.com/pricing/)
* [Danh mục model](https://www.cometapi.com/models/)
* [Trang Models](/vi/overview/models)
* [Giới thiệu về Pricing](/vi/pricing/about-pricing)
* [Bắt đầu nhanh với CometAPI](/vi/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": "Làm cách nào để ước tính chi phí trước khi gọi model?",
        "description": "Ước tính chi phí request của CometAPI trước khi gọi model bằng cách kết hợp giá trong danh mục model với kích thước đầu vào, giới hạn đầu ra hoặc số lượng tác vụ.",
        "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": "Tài liệu CometAPI",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Hướng dẫn",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "Làm cách nào để ước tính chi phí trước khi gọi model?",
            "item": "https://apidoc.cometapi.com/guides/how-to-estimate-cost-before-calling-a-model"
          }
        ]
      }
    ]
    }
    `}
</script>
