Routescope APIRoutescope API
客户端工具配置

如何设置备用模型:OpenAI 失败时自动切换到 Claude / Qwen / DeepSeek

说明如何在自己的应用代码里做模型失败兜底,OpenAI 请求失败时自动改用 Claude / Qwen / DeepSeek。

要实现“OpenAI 失败时自动切换到 Claude / Qwen / DeepSeek”,最直接的方式是在自己的应用代码里捕获失败,然后用备用模型重新请求。

第一步:准备备用模型列表

打开模型广场,复制当前账号可用的模型名。建议先准备一个主模型,再准备 2 到 3 个备用模型。

const fallbackModels = [
  "your-openai-model",
  "your-claude-model",
  "your-qwen-model",
  "your-deepseek-model",
];

如果你的代码使用 OpenAI Compatible 的 /v1/chat/completions,请确认备用模型也支持这个请求方式。模型是否可用,以模型广场和实际测试结果为准。

第二步:只对可重试错误切换模型

建议对这些情况切换备用模型:

错误类型是否建议切换
网络超时建议
429建议
上游 5xx建议
400不建议,通常是参数问题
401 / 403不建议,通常是 API Key、额度或权限问题
404不建议,通常是模型名不存在

JavaScript / TypeScript 示例

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.ROUTESCOPE_API_KEY,
  baseURL: "https://api.routescope.ai/v1",
});

const fallbackModels = [
  "your-openai-model",
  "your-claude-model",
  "your-qwen-model",
  "your-deepseek-model",
];

function shouldRetry(error: any) {
  const status = error?.status;
  return !status || status === 429 || status >= 500;
}

export async function createChatWithFallback(messages: any[]) {
  let lastError: unknown;

  for (const model of fallbackModels) {
    try {
      return await client.chat.completions.create({
        model,
        messages,
      });
    } catch (error) {
      lastError = error;

      if (!shouldRetry(error)) {
        throw error;
      }
    }
  }

  throw lastError;
}

Python 示例

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-token",
    base_url="https://api.routescope.ai/v1",
)

fallback_models = [
    "your-openai-model",
    "your-claude-model",
    "your-qwen-model",
    "your-deepseek-model",
]


def should_retry(error):
    status = getattr(error, "status_code", None)
    return status is None or status == 429 or status >= 500


def create_chat_with_fallback(messages):
    last_error = None

    for model in fallback_models:
        try:
            return client.chat.completions.create(
                model=model,
                messages=messages,
            )
        except Exception as error:
            last_error = error
            if not should_retry(error):
                raise

    raise last_error

客户端工具怎么处理

如果你用的是没有代码逻辑的客户端工具,例如某些编辑器插件或桌面客户端,通常只能手动切换模型。可以先在同一个工具里保存多个 RouteScope 模型配置:

场景处理方式
主模型偶发失败手动把模型切到备用模型后重试。
自己开发的应用用上面的代码自动切换。
团队提供了统一模型名直接使用团队给你的模型名。

验证备用模型是否生效

测试时可以把第一个模型名临时改成一个不可用模型,确认代码会继续尝试下一个模型。测试完成后记得恢复真实模型名。

进入操作记录后,重点查看:

检查项说明
多条请求记录失败模型和成功模型会分别留下记录。
请求模型是否按你设置的顺序尝试。
错误原因是否只对 429、超时或 5xx 做了重试。
最终消耗以最终成功的模型和输出 token 为准。

最后更新于