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.
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, andOPENAI_OFFICIAL_MODEL - Claude route:
COMETAPI_KEY,COMETAPI_CLAUDE_PRIMARY_MODEL,COMETAPI_CLAUDE_FALLBACK_MODEL,ANTHROPIC_API_KEY, andANTHROPIC_OFFICIAL_MODEL - Shared controls:
ENABLE_OFFICIAL_FALLBACKandROUTE_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.
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.- OpenAI models
- Claude models
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.