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

# Add model fallback with CometAPI

> Add model fallback with CometAPI by trying model IDs in order, retrying retryable failures, and stopping on request-shape errors.

Add model fallback by keeping a small ordered list of model IDs in your app and trying the next model only when the failure is retryable or model-specific. Do not retry malformed requests; fix `400`, `401`, and invalid request errors before you call another model.

## Implement ordered fallback

The following Python example tries model IDs in order and stops on non-retryable errors:

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

The successful response comes from the first model that completes:

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

## Choose fallback models

The following request lists available model IDs that you can evaluate for fallback:

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

The response contains model records:

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

## Common errors

| Error                                | Fix                                                                      |
| ------------------------------------ | ------------------------------------------------------------------------ |
| Fallback hides invalid requests      | Do not fallback on `400`, `401`, or invalid request errors.              |
| Models have different output formats | Normalize the response in your app before returning it to users.         |
| Fallback increases cost              | Estimate cost for each model before adding it to the fallback list.      |
| Too many parallel fallbacks          | Try fallbacks sequentially unless your product requires parallel racing. |

## Related links

* [Error codes and retry strategy](/guides/error-codes-and-retry-strategy)
* [Rate limits and concurrency](/guides/rate-limits-and-concurrency)
* [Models page](/overview/models)
* [Model directory](https://www.cometapi.com/models/)
* [Pricing](https://www.cometapi.com/pricing/)
* [CometAPI quickstart](/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": "How do I add model fallback with CometAPI?",
        "description": "Add model fallback with CometAPI by trying model IDs in order, retrying retryable failures, and stopping on request-shape errors.",
        "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": "CometAPI Docs",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Guides",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "How do I add model fallback with CometAPI?",
            "item": "https://apidoc.cometapi.com/guides/model-fallback-with-cometapi"
          }
        ]
      }
    ]
    }
    `}
</script>
