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

# Tambahkan fallback model dengan CometAPI

> Tambahkan fallback model dengan CometAPI dengan mencoba model ID secara berurutan, mencoba ulang kegagalan yang dapat diulang, dan berhenti pada error bentuk permintaan.

Tambahkan fallback model dengan mempertahankan daftar kecil model ID yang diurutkan di aplikasi Anda dan mencoba model berikutnya hanya ketika kegagalan dapat diulang atau spesifik terhadap model. Jangan mencoba ulang permintaan yang malformed; perbaiki error `400`, `401`, dan invalid request sebelum Anda memanggil model lain.

## Implementasikan fallback berurutan

Contoh Python berikut mencoba model ID secara berurutan dan berhenti pada error yang tidak dapat diulang:

```python theme={null}
import os
import time
from openai import OpenAI, APIError, RateLimitError

client = OpenAI(
    api_key=os.environ["COMETAPI_KEY"],
    base_url="https://api.cometapi.com/v1",
)

models = ["your-primary-model-id", "your-fallback-model-id"]

for model in models:
    try:
        completion = client.chat.completions.create(
            model=model,
            messages=[
                {
                    "role": "user",
                    "content": "Summarize CometAPI in one sentence.",
                }
            ],
        )
        print(completion.choices[0].message.content)
        break
    except RateLimitError:
        time.sleep(2)
        continue
    except APIError as error:
        status_code = getattr(error, "status_code", None)
        if status_code in {500, 503, 504, 524}:
            time.sleep(2)
            continue
        raise
else:
    raise RuntimeError("All configured model fallbacks failed.")
```

Respons yang berhasil berasal dari model pertama yang selesai:

```json theme={null}
{
  "model": "your-fallback-model-id",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "CometAPI gives developers one API surface for multiple model providers."
      }
    }
  ]
}
```

## Pilih model fallback

Permintaan berikut mencantumkan model ID yang tersedia dan dapat Anda evaluasi untuk fallback:

```bash theme={null}
curl https://api.cometapi.com/api/models
```

Respons berisi record model:

```json theme={null}
{
  "success": true,
  "page": 0,
  "page_size": 0,
  "total": 301,
  "data": [
    {
      "id": "deepseek-v4-pro",
      "provider": "DeepSeek",
      "model_type": "text",
      "features": [
        "text-to-text"
      ],
      "pricing": {
        "currency": "USD / M Tokens",
        "input": 0.416,
        "output": 0.832,
        "per_request": null,
        "per_second": null
      },
      "api_doc_url": "https://apidoc.cometapi.com/api/text/chat"
    }
  ]
}
```

## Error umum

| Error                                               | Perbaikan                                                                         |
| --------------------------------------------------- | --------------------------------------------------------------------------------- |
| Fallback menyembunyikan permintaan yang tidak valid | Jangan lakukan fallback pada error `400`, `401`, atau invalid request.            |
| Model memiliki format output yang berbeda           | Normalisasikan respons di aplikasi Anda sebelum mengembalikannya kepada pengguna. |
| Fallback meningkatkan biaya                         | Perkirakan biaya untuk setiap model sebelum menambahkannya ke daftar fallback.    |
| Terlalu banyak fallback paralel                     | Coba fallback secara berurutan kecuali produk Anda memerlukan racing paralel.     |

## Tautan terkait

* [Kode error dan strategi retry](/id/guides/error-codes-and-retry-strategy)
* [Batas laju dan konkurensi](/id/guides/rate-limits-and-concurrency)
* [Halaman model](/id/overview/models)
* [Direktori model](https://www.cometapi.com/models/)
* [Harga](https://www.cometapi.com/pricing/)
* [Quickstart CometAPI](/id/overview/quick-start)

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "TechArticle",
        "@id": "https://apidoc.cometapi.com/guides/model-fallback-with-cometapi",
        "headline": "Bagaimana cara menambahkan model fallback dengan CometAPI?",
        "description": "Tambahkan model fallback dengan CometAPI dengan mencoba model ID secara berurutan, melakukan retry pada kegagalan yang dapat di-retry, dan berhenti pada error bentuk permintaan.",
        "url": "https://apidoc.cometapi.com/guides/model-fallback-with-cometapi",
        "author": {
          "@type": "Organization",
          "name": "CometAPI"
        },
        "publisher": {
          "@type": "Organization",
          "name": "CometAPI",
          "url": "https://www.cometapi.com"
        }
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "Dokumentasi CometAPI",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Panduan",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "Bagaimana cara menambahkan model fallback dengan CometAPI?",
            "item": "https://apidoc.cometapi.com/guides/model-fallback-with-cometapi"
          }
        ]
      }
    ]
    }
    `}
</script>
