Skip to main content
Build two fallback layers for text requests. Keep CometAPI as the primary route. First, change the model ID inside CometAPI. If those routes fail and official fallback is enabled, call the matching official provider.

Model fallback inside CometAPI

Keep the same CometAPI API key and base URL. Try a compatible secondary model ID after the primary model fails.

Official-provider fallback

Use a separate OpenAI or Anthropic client, API key, model ID, account, and billing configuration.
The recommended order is CometAPI primary model → CometAPI fallback model → matching official provider.

Decide when to fallback

Use a narrow error policy so that fallback does not hide request problems: The examples below use ENABLE_OFFICIAL_FALLBACK as the explicit switch. If you also fallback on a model or account limit, add only its verified error signal to your application’s error classifier. Install openai and anthropic for Python, or install openai and @anthropic-ai/sdk for Node.js. Then configure these environment variables:
  • OpenAI route: COMETAPI_KEY, COMETAPI_OPENAI_PRIMARY_MODEL, COMETAPI_OPENAI_FALLBACK_MODEL, OPENAI_API_KEY, and OPENAI_OFFICIAL_MODEL
  • Claude route: COMETAPI_KEY, COMETAPI_CLAUDE_PRIMARY_MODEL, COMETAPI_CLAUDE_FALLBACK_MODEL, ANTHROPIC_API_KEY, and ANTHROPIC_OFFICIAL_MODEL
  • Shared controls: ENABLE_OFFICIAL_FALLBACK and ROUTE_TIMEOUT_MS
ENABLE_OFFICIAL_FALLBACK defaults to false. Set it to true only when the official-provider account is ready. ROUTE_TIMEOUT_MS is the timeout for each attempt and defaults to 30 seconds. Set it from your application’s latency budget; total fallback time can include all three attempts. Choose fallback models that support the same request format and the capabilities that your application requires. Test every route before you depend on it.
Official-provider requests use a separate account and billing configuration. Configure provider budgets and alerts before you enable this route. Record the official fallback rate so that you can investigate sustained usage.

Implement the fallback chain

Choose the tab that matches the request format that your application uses. Configure separate model IDs for the CometAPI and official-provider routes.
Use Chat Completions for both CometAPI and the OpenAI official API. The request shape stays the same, but each route has its own client, API key, and model ID.
If your application uses the Responses API, keep the same dual-client routing pattern and adapt the request and response fields. See the official Chat Completions reference.

Switch between model families

GPT and Claude can fallback to each other when your application converts both requests to shared fields, normalizes both responses, and verifies the required capabilities on every route. A model ID change alone is not enough when request or response shapes differ.
Last modified on August 7, 2026