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

推荐订阅源

小众软件
小众软件
博客园_首页
M
MIT News - Artificial intelligence
雷峰网
雷峰网
GbyAI
GbyAI
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
S
SegmentFault 最新的问题
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
V
Visual Studio Blog
月光博客
月光博客
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
云风的 BLOG
云风的 BLOG
美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队

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
One API Key for GPT, Claude, Gemini, and Qwen: A Practica...
plasma · 2026-06-24 · via DEV Community

If you've built anything serious with LLM APIs, you've probably hit this pattern:

  • GPT is great for one task, but too expensive for another.
  • Claude is better at long-context reasoning, but you don't want to rewrite your whole client.
  • Gemini or Qwen may be good enough for cheaper background jobs.
  • Your app slowly turns into a pile of provider-specific SDKs, env vars, retry logic, and billing dashboards.

Not fun.

The cleanest version of this setup is simple: keep your app speaking the OpenAI API format, but route requests to different models behind the scenes.

That's the idea behind an OpenAI-compatible AI gateway.

Disclosure: I work on TokenBay, an AI model API gateway. This post is based on the setup I usually recommend when developers want access to multiple model families without rewriting their app every time they test a new provider.

The Problem

Most AI apps start with one model provider.

That works fine until you need to optimize for cost, latency, quality, availability, or task type.

For example:

  • Use a strong reasoning model for complex user-facing answers.
  • Use a cheaper model for summarization, tagging, extraction, or internal jobs.
  • Fall back to another provider when one API is slow or unavailable.
  • Test new models without refactoring half your codebase.

The painful part is not calling one API.

The painful part is maintaining five slightly different API integrations.

The Practical Pattern

Instead of wiring every provider directly into your app, you can use an OpenAI-compatible gateway.

Your application keeps using the familiar chat.completions.create() interface.

The only things you usually change are:

  • baseURL
  • apiKey
  • model

That means your app can switch between GPT, Claude, Gemini, Qwen, and other models while keeping most of your code unchanged.

Quickstart with Node.js

Install the OpenAI SDK:

npm install openai

Create a file called index.js:

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.TOKENBAY_API_KEY,
  baseURL: "https://api.tokenbay.com/v1"
});

async function main() {
  const response = await client.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      {
        role: "system",
        content: "You are a concise technical assistant."
      },
      {
        role: "user",
        content: "Explain model routing in one paragraph."
      }
    ]
  });

  console.log(response.choices[0].message.content);
}

main().catch(console.error);

Run it:

TOKENBAY_API_KEY=your_api_key_here node index.js

If your app already uses the OpenAI SDK, the main migration is usually just the client config:

const client = new OpenAI({
  apiKey: process.env.TOKENBAY_API_KEY,
  baseURL: "https://api.tokenbay.com/v1"
});

That's the whole point.

You should not need a different SDK for every model family just to run a basic chat completion.

Routing by Task Type

Once your app can talk to multiple models through the same interface, you can make routing decisions in code.

Here's a simple example:

function selectModel(taskType) {
  switch (taskType) {
    case "reasoning":
      return "gpt-4o";

    case "cheap_summary":
      return "qwen-plus";

    case "long_context":
      return "claude-3-5-sonnet";

    default:
      return "gpt-4o-mini";
  }
}

Then call the selected model:

async function runTask(taskType, userInput) {
  const model = selectModel(taskType);

  const response = await client.chat.completions.create({
    model,
    messages: [
      {
        role: "user",
        content: userInput
      }
    ]
  });

  return response.choices[0].message.content;
}

This is intentionally boring code.

Boring is good here.

You want model routing to be understandable, testable, and easy to change.

Adding a Fallback

Provider outages happen. Rate limits happen. Weird transient API failures happen.

A basic fallback wrapper can save you from a lot of production pain:

async function completeWithFallback(messages) {
  const models = [
    "gpt-4o-mini",
    "qwen-plus",
    "gemini-1.5-flash"
  ];

  let lastError;

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

      return {
        model,
        content: response.choices[0].message.content
      };
    } catch (error) {
      lastError = error;
      console.warn(`Model ${model} failed, trying next option...`);
    }
  }

  throw lastError;
}

Example usage:

const result = await completeWithFallback([
  {
    role: "user",
    content: "Summarize this customer support ticket in 3 bullet points."
  }
]);

console.log(`Used model: ${result.model}`);
console.log(result.content);

This is not a full production-grade retry system, but it gives you the shape:

  • Try your preferred model.
  • Fall back to another compatible model.
  • Keep the calling code simple.

Where This Helps Most

An OpenAI-compatible gateway is useful when you have multiple LLM workloads inside the same product.

Some common examples:

  • SaaS apps with user-facing AI features
  • Internal support tools
  • AI agents
  • Document summarization workflows
  • Batch extraction jobs
  • Coding assistants
  • Evaluation pipelines
  • Side projects where API cost actually matters

Not every task needs the most expensive model.

A lot of production AI work is routing the right request to the right model at the right cost.

Cost Control

This is where gateways become more than a convenience layer.

Once all requests pass through one API layer, you can start thinking about:

  • Per-task model selection
  • Usage tracking
  • Budget limits
  • Cheaper models for background jobs
  • Higher-quality models only where they matter
  • Fallbacks when a provider is unavailable

TokenBay is built around this workflow: one API key for major model families, OpenAI-compatible requests, and a usage dashboard so you can see what your app is actually spending.

In many cases, routing routine tasks to lower-cost models can reduce API spend without hurting the user experience.

The exact savings depend on your workload and model choices, so I would treat any blanket savings claim with suspicion. Test it against your own traffic.

A Simple Rule of Thumb

When I look at an AI feature, I usually split calls into three buckets:

Task Model Strategy
User-facing reasoning Use a stronger model
Summarization / tagging / extraction Try cheaper models first
Background automation Optimize heavily for cost and latency

You do not need a complicated ML routing system on day one.

A plain function like selectModel(taskType) is enough to start.

Final Thoughts

The best AI API architecture is usually the one that gives you room to change your mind.

Models change. Prices change. Latency changes. Your product changes.

If your code is tightly coupled to one provider, every model experiment becomes annoying.

If your app talks to one OpenAI-compatible interface, you can test and route across GPT, Claude, Gemini, Qwen, and others with much less friction.

That is the real win: not just saving money, but making model choice a runtime decision instead of a rewrite.

For this post, I used TokenBay as the OpenAI-compatible gateway example, but the same routing pattern applies to any gateway that supports the OpenAI API format.

I'd also be curious how other teams are handling model routing today. Are you using a gateway, building your own abstraction layer, or still calling each provider directly?