7 AI Models, 1 API Key: How to Build a Model-Agnostic SaaS
If you're building an AI product, the worst mistake is locking yourself to one provider. Here's the architecture that lets you switch between Kimi K3, GPT-5, Claude 4, DeepSeek V4, GLM-5, Qwen-Plus, and Doubao Pro at runtime — without changing a single line of application code.
The problem with single-vendor lock-in
Last month, DeepSeek had a 4-hour outage. Companies using DeepSeek directly had their apps go down.
Companies using OpenAI are at the mercy of OpenAI's pricing changes (GPT-4 Turbo went from $10/M to $15/M in 18 months).
The fix: build a model-agnostic layer. Here's how.
The pattern: a unified gateway
The architecture has 3 layers:
- Your application (any language) — calls one API endpoint
- Gateway — translates to provider-specific calls, handles failover
- Providers — OpenAI, Anthropic, Moonshot, DeepSeek, Zhipu, Alibaba, ByteDance
You can build your own gateway, or use one: TokenEase (https://tokenease.io) gives you this out of the box.
Option 1: Use TokenEase (5 minutes)
Get a key
Free trial: https://tokenease.io/api/register ($1 credit, 1M tokens, 14 days)
Call any model with one line change
from openai import OpenAI
client = OpenAI(
api_key="your-tokenease-key",
base_url="https://tokenease.io/v1"
)
# Switch models by changing the model parameter
MODELS = {
"fast": "deepseek-v4-flash", # $0.27/M
"smart": "kimi-k3", # $0.50/M
"coding": "gpt-5", # $15/M
"long-doc": "claude-4-opus", # $15/M
"chinese": "kimi-k3", # best for Chinese
"vision": "gpt-5", # supports images
"cheap": "glm-4-flash", # $0.10/M
}
def chat(model_key, user_message):
response = client.chat.completions.create(
model=MODELS[model_key],
messages=[{"role": "user", "content": user_message}],
max_tokens=2000
)
return response.choices[0].message.content
That's it. Your application can route to any of 7 models based on user preference, cost, or task.
Option 2: Build your own gateway (advanced)
If you want full control, here's a minimal gateway in Python:
import os
from openai import OpenAI
# Provider configs
PROVIDERS = {
"kimi": {
"base_url": "https://api.moonshot.cn/v1",
"key_env": "MOONSHOT_KEY",
"requires_china": True
},
"deepseek": {
"base_url": "https://api.deepseek.com/v1",
"key_env": "DEEPSEEK_KEY",
"requires_china": False
},
"openai": {
"base_url": "https://api.openai.com/v1",
"key_env": "OPENAI_KEY",
"requires_china": False
},
# ... add more
}
class ModelRouter:
def __init__(self):
self.clients = {
name: OpenAI(
api_key=os.environ[cfg["key_env"]],
base_url=cfg["base_url"]
)
for name, cfg in PROVIDERS.items()
}
self.health = {name: True for name in PROVIDERS}
def chat(self, model, messages, **kwargs):
# Auto-failover logic
try:
response = self.clients[model].chat.completions.create(
model=model, messages=messages, **kwargs
)
return response.choices[0].message.content
except Exception as e:
# Mark unhealthy, try fallback
self.health[model] = False
fallback = self.get_fallback(model)
if fallback:
return self.chat(fallback, messages, **kwargs)
raise
def get_fallback(self, model):
# Define fallback chain
fallbacks = {
"kimi-k3": "deepseek-v4",
"gpt-5": "claude-4",
"deepseek-v4": "kimi-k3",
# ...
}
for fb in fallbacks.get(model, []):
if self.health[fb]:
return fb
return None
Cost optimization patterns
Pattern 1: tiered routing
def smart_route(task, user_message):
# Use cheap models for simple tasks
if task == "summarize" and len(user_message) < 1000:
return "glm-4-flash" # $0.10/M
elif task == "code":
return "gpt-5" # best for code
elif task == "long-doc":
return "claude-4-opus" # 200K context
else:
return "kimi-k3" # best price/quality
Pattern 2: cascading
Try cheap model first, escalate if quality is low:
def cascading_chat(user_message):
# First try cheap model
response = chat("deepseek-v4-flash", user_message)
# If response is too short, escalate
if len(response) < 50:
response = chat("kimi-k3", user_message)
return response
Pattern 3: parallel evaluation
For high-stakes tasks, run multiple models and pick the best:
import concurrent.futures
def consensus_chat(user_message, models=["kimi-k3", "gpt-5", "claude-4"]):
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = {
executor.submit(chat, m, user_message): m
for m in models
}
responses = {f.result(): m for f, m in futures.items()}
# Pick the longest (usually most detailed)
return max(responses.keys(), key=len)
Real-world architecture
A production AI SaaS typically has:
User → Your App → Gateway → [Provider 1, Provider 2, Provider 3]
↓
Cache (Redis)
↓
Cost Tracker + Analytics
Key features:
- Auto-failover: if Provider 1 is down, route to Provider 2
- Cost tracking: log tokens per request, bill users accordingly
- Rate limiting: 60 req/min per user on free tier
- Caching: cache identical requests for 5 minutes (huge cost savings)
- Streaming: SSE for real-time responses
Use TokenEase vs build your own
| TokenEase | Build your own | |
|---|---|---|
| Time to set up | 5 min | 2 weeks |
| Failover | Built-in | You code it |
| Cost tracking | Built-in | You code it |
| Model coverage | 7 models | You add each |
| China access | Built-in | You deal with it |
| Pricing | $1.99-99.9/mo | Engineering time |
For most teams, TokenEase is the right choice. For companies with specific compliance needs (e.g., data must stay in EU), build your own.
Try it
Free trial: https://tokenease.io/api/register
7 models, 1 key, $1 free credit, 14 days.
Disclaimer: I work on TokenEase. The pricing above is current as of July 2026.
Top comments (0)