惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
J
Java Code Geeks
S
SegmentFault 最新的问题
L
LangChain Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
H
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
N
Netflix TechBlog - Medium
A
About on SuperTechFans
博客园 - 叶小钗
美团技术团队
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How to Build a Multi-Model LLM Fallback Layer Without Rew...
Fan Chuanyu · 2026-06-15 · via DEV Community

Fan Chuanyu

Most LLM integrations start as a single provider call.

That is usually the right move. You pick one strong model, wire up a chat completions request, ship the feature, and learn from real users.

The problem starts later.

Your support assistant needs better latency. Your document workflow needs a larger context window. Your extraction job is too expensive on the flagship model. A provider returns rate-limit errors during a launch. A new model is cheaper for background tasks but not good enough for customer-facing reasoning.

At that point, model choice is no longer a one-time SDK decision. It becomes application infrastructure.

This post walks through a practical way to build a small multi-model fallback layer so your product can use more than one provider without spreading provider-specific logic through the codebase.

The mistake: provider logic inside product features

A first integration often looks like this:

const response = await client.chat.completions.create({
  model: "gpt-4.1",
  messages,
});

That is fine for a prototype. In production, the feature usually grows around the provider call:

  • retries
  • rate-limit handling
  • usage metering
  • customer quotas
  • model-specific parameters
  • prompt templates
  • latency tracking
  • fallback behavior
  • cost attribution

If each product feature owns those details, every model change becomes a product change. You do not only switch a model name. You update error handling, logging, pricing assumptions, quality tests, and maybe even prompt shape.

The goal is not to hide every model difference. Some differences matter. The goal is to keep provider decisions in one place.

A better boundary: route by task, not by feature

Instead of letting every feature pick a provider directly, define the type of work the request represents.

For example:

type LlmTask =
  | "support_chat"
  | "document_summary"
  | "data_extraction"
  | "title_generation"
  | "long_context_analysis";

Then map tasks to model policies:

type ModelRoute = {
  primary: string;
  fallback?: string[];
  maxLatencyMs?: number;
  maxInputTokens?: number;
  allowFallback: boolean;
};

const routes: Record<LlmTask, ModelRoute> = {
  support_chat: {
    primary: "anthropic/claude-sonnet",
    fallback: ["openai/gpt-4.1", "google/gemini-pro"],
    maxLatencyMs: 5000,
    allowFallback: true,
  },
  data_extraction: {
    primary: "openai/gpt-4.1-mini",
    fallback: ["qwen/qwen-plus"],
    maxLatencyMs: 3000,
    allowFallback: true,
  },
  long_context_analysis: {
    primary: "google/gemini-pro",
    fallback: [],
    maxInputTokens: 1_000_000,
    allowFallback: false,
  },
  document_summary: {
    primary: "openai/gpt-4.1-mini",
    fallback: ["deepseek/deepseek-chat"],
    allowFallback: true,
  },
  title_generation: {
    primary: "qwen/qwen-plus",
    fallback: ["openai/gpt-4.1-mini"],
    allowFallback: true,
  },
};

This gives your application a stable interface:

const result = await llm.generate({
  task: "data_extraction",
  messages,
  customerId,
});

The feature does not need to know whether the request went to OpenAI, Anthropic, Gemini, Qwen, or another provider. It only needs the result and the metadata required for debugging.

Keep fallback conservative

Fallback sounds simple: if the primary model fails, try another one.

In practice, fallback rules need to be conservative because not all failures are the same.

You can usually retry or fall back on:

  • transient network errors
  • provider 5xx responses
  • rate-limit errors
  • timeouts
  • temporary capacity issues

You should be careful with fallback on:

  • safety refusals
  • structured output failures
  • tool-calling workflows
  • tasks where model behavior affects money, legal decisions, or user trust
  • workflows where consistency matters more than availability

Here is a simplified fallback runner:

type GenerateRequest = {
  task: LlmTask;
  messages: Array<{ role: "system" | "user" | "assistant"; content: string }>;
  customerId: string;
};

async function generateWithFallback(request: GenerateRequest) {
  const route = routes[request.task];
  const candidates = [route.primary, ...(route.fallback ?? [])];

  let lastError: unknown;

  for (const model of candidates) {
    try {
      const startedAt = Date.now();

      const response = await callModelProvider({
        model,
        messages: request.messages,
      });

      await logUsage({
        customerId: request.customerId,
        task: request.task,
        model,
        latencyMs: Date.now() - startedAt,
        inputTokens: response.usage.inputTokens,
        outputTokens: response.usage.outputTokens,
        fallback: model !== route.primary,
      });

      return response;
    } catch (error) {
      lastError = error;

      if (!route.allowFallback || !isFallbackSafe(error)) {
        throw error;
      }
    }
  }

  throw lastError;
}

The important part is the policy, not the exact code. You want the fallback decision to be explicit, observable, and different for each workload.

Log cost before it hurts

LLM cost visibility is easy to postpone when usage is small. That is a trap.

By the time token cost is visible on your cloud bill, it is usually harder to know which feature, model, customer, or prompt caused the increase.

At minimum, log:

  • customer or workspace ID
  • feature or task name
  • provider and model
  • input tokens
  • output tokens
  • cached tokens if supported
  • latency
  • fallback status
  • request outcome

This lets you answer practical questions:

  • Which feature is most expensive?
  • Which customers generate the highest token cost?
  • Which background jobs can move to a cheaper model?
  • Which model has the worst tail latency?
  • How often are fallbacks happening?

You do not need a complicated system to start. A database table or analytics event is enough:

await db.llmUsage.create({
  data: {
    customerId,
    task,
    model,
    inputTokens,
    outputTokens,
    latencyMs,
    fallback,
    createdAt: new Date(),
  },
});

Do not pretend all models are identical

An OpenAI-compatible API can reduce integration work, but compatibility is not the same as interchangeability.

Models can differ in:

  • context window size
  • tool calling behavior
  • structured output reliability
  • latency by region
  • output style
  • refusal behavior
  • tokenization
  • price

The abstraction should keep common product code clean while still exposing model-specific facts where they matter.

A good rule: hide provider plumbing, not product-relevant behavior.

Where a gateway fits

You can build this layer yourself if you have specific routing, compliance, or observability requirements.

You can also use an OpenAI-compatible AI gateway if you want the model catalog, routing, pricing, and fallback surface managed outside your app. For example, datallmlab is one implementation option for teams that want access to GPT, Claude, Gemini, Qwen, DeepSeek, and other models through a single API.

The architectural point is the same either way: keep model selection outside feature code.

Checklist

Before adding a second provider, decide:

  1. Which workloads are allowed to fall back?
  2. Which workloads need consistent model behavior?
  3. Where will model routing be configured?
  4. How will you measure cost per customer and feature?
  5. How will you test output quality before switching a route?
  6. What errors are safe to retry?
  7. What errors should stop immediately?
  8. Who can change model routes in production?

Final thought

The best model for your product today may not be the best model next quarter.

That does not mean you should rewrite your app every time the model landscape changes. It means the app should treat model choice as a routing decision, not a hard-coded dependency.

Start small: one routing function, one usage log, one conservative fallback policy.

That is enough to keep your AI features flexible without turning your codebase into provider glue.