Add Model Fallback to an OpenAI-Compatible Node.js App

A single model can be unavailable, rate-limited, or temporarily slow. If your application already uses an OpenAI-compatible API, a simple fallback can make testing more resilient without introducing another SDK.

This tutorial uses Node.js and the offi…


This content originally appeared on DEV Community and was authored by Jinze Wang

A single model can be unavailable, rate-limited, or temporarily slow. If your application already uses an OpenAI-compatible API, a simple fallback can make testing more resilient without introducing another SDK.

This tutorial uses Node.js and the official OpenAI JavaScript package. It tries one model first and switches to a second model only when the first request fails.

1. Install the SDK

npm install openai

2. Store the API key outside your code

On macOS or Linux:

export JINZEAI_API_KEY="your_api_key_here"

On PowerShell:

$env:JINZEAI_API_KEY="your_api_key_here"

Never commit a real API key. Rotate it immediately if it appears in a public repository, screenshot, or support message.

3. Create an OpenAI-compatible client

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://jinzeai.cc/v1",
  apiKey: process.env.JINZEAI_API_KEY,
});

4. Add a small fallback function

const models = ["deepseek-chat", "qwen-flash"];

async function completeWithFallback(messages) {
  let lastError;

  for (const model of models) {
    try {
      const response = await client.chat.completions.create({
        model,
        messages,
      });

      return {
        model,
        text: response.choices[0].message.content,
      };
    } catch (error) {
      lastError = error;
      console.warn(`${model} failed: ${error.status ?? "unknown status"}`);
    }
  }

  throw lastError;
}

const result = await completeWithFallback([
  {
    role: "user",
    content: "Explain model fallback in one sentence.",
  },
]);

console.log(`Model: ${result.model}`);
console.log(result.text);

5. Decide which errors should trigger fallback

The minimal example retries on every error so the control flow is easy to see. A production application should be more selective.

Fallback may be reasonable for:

  • rate limits;
  • upstream server errors;
  • temporary timeouts;
  • a model that is unavailable to the current account.

Do not silently retry authentication errors. An HTTP 401 usually means the key is missing, inactive, or formatted incorrectly. Fix the key instead of sending the same invalid request to another model.

6. Check model availability first

Available beta models can change. Use your key with GET /v1/models rather than assuming every model is enabled for every account.

curl https://jinzeai.cc/v1/models \
  -H "Authorization: Bearer $JINZEAI_API_KEY"

Try it during the public beta

JinzeAI is testing an OpenAI-compatible endpoint for users outside mainland China. No payment is required during the public beta, and limited free test credit may be available.

Model availability and test limits may change during beta. Feedback about SDK compatibility, latency, and failure handling is especially useful.


This content originally appeared on DEV Community and was authored by Jinze Wang


Print Share Comment Cite Upload Translate Updates
APA

Jinze Wang | Sciencx (2026-08-14T03:35:25+00:00) Add Model Fallback to an OpenAI-Compatible Node.js App. Retrieved from https://www.scien.cx/2026/08/14/add-model-fallback-to-an-openai-compatible-node-js-app/

MLA
" » Add Model Fallback to an OpenAI-Compatible Node.js App." Jinze Wang | Sciencx - Friday August 14, 2026, https://www.scien.cx/2026/08/14/add-model-fallback-to-an-openai-compatible-node-js-app/
HARVARD
Jinze Wang | Sciencx Friday August 14, 2026 » Add Model Fallback to an OpenAI-Compatible Node.js App., viewed ,<https://www.scien.cx/2026/08/14/add-model-fallback-to-an-openai-compatible-node-js-app/>
VANCOUVER
Jinze Wang | Sciencx - » Add Model Fallback to an OpenAI-Compatible Node.js App. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2026/08/14/add-model-fallback-to-an-openai-compatible-node-js-app/
CHICAGO
" » Add Model Fallback to an OpenAI-Compatible Node.js App." Jinze Wang | Sciencx - Accessed . https://www.scien.cx/2026/08/14/add-model-fallback-to-an-openai-compatible-node-js-app/
IEEE
" » Add Model Fallback to an OpenAI-Compatible Node.js App." Jinze Wang | Sciencx [Online]. Available: https://www.scien.cx/2026/08/14/add-model-fallback-to-an-openai-compatible-node-js-app/. [Accessed: ]
rf:citation
» Add Model Fallback to an OpenAI-Compatible Node.js App | Jinze Wang | Sciencx | https://www.scien.cx/2026/08/14/add-model-fallback-to-an-openai-compatible-node-js-app/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.