Unified gateway: switching models only changes the model name, not the SDK or auth
client = OpenAI(
api_key=GATEWAY_KEY,
base_url="https://api.thistoken.ai/v1"
)
def generate_plan(profile: dict) -> dict:
resp = client.chat.completions.create(
model="gpt-4o", # Strong model for the main plan
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": PLAN_SYSTEM_PROMPT}, # Includes exercise whitelist and contraindication rules
{"role": "user", "content": json.dumps(profile, ensure_ascii=False)}
]
)
plan = json.loads(resp.choices[0].message.content)
invalid = validate_against_action_lib(plan, profile)
if invalid:
plan = retry_or_fallback(profile, invalid) # Retry/fallback
return plan
When rewriting exercise instructions, just change `model` to a lightweight model—the rest of the code is fully reusable.
## 4. The Efficiency Math: Before vs. After
| Metric | Before | After |
|---|---|---|
| Time to produce one plan | 40 minutes (manual) | ~8 seconds (generation + validation) |
| User wait time | Average 18 hours | Under 30 seconds |
| Cost per plan | 30-50 RMB | Token cost ~0.03-0.08 RMB |
| Plan modification requests | 1-2 day round trip | Regenerated in minutes |
| Adding 500 new users | +1 part-time coach | +0 headcount |
The coach's role shifted from "writing plans" to "curating the exercise library + quality spot-checks"—2 people now support the output that used to require 6. **The marginal cost of personalization dropped from 30 RMB to a few cents**, which is what finally made the business model work.
## 5. Why a Unified AI API Gateway Saves Maintenance Costs
This is the most underrated part of the whole solution. Initially, we considered connecting directly to each vendor's official API, but abandoned the idea after doing the math:
- **Multiple SDK maintenance**: Connecting to 3 vendors means maintaining 3 SDKs, 3 auth setups, 3 error handling implementations. When one vendor released a major SDK upgrade, we spent an entire day adapting—that day could have gone toward the product. With a unified gateway, there's only one OpenAI-compatible protocol; **switching models = changing one string**, done in 5 minutes;
- **Model replaceability**: Fitness plan generation is price-sensitive—use vendor A today, switch to vendor B next month if they cut prices. A unified gateway turns "model selection" into an operations decision rather than a development task, with no re-release process needed;
- **Centralized budget and rate limiting**: All calls go through one entry point, letting you monitor token consumption in real time and set quotas per feature, preventing a malfunctioning feature from blowing up your bill—a lifesaver compared to the era of discovering over-budget bills at month's end;
- **Unified retry and fallback**: Timeout, rate-limit, and content-moderation failure fallback logic is written once and shared across all model calls, instead of being duplicated in every piece of business code.
For a small team, this means **one person can maintain features depending on 3-5 models**, rather than each model consuming half an engineer.
## 6. Implementation Advice
If you want to build a similar personalized generation feature, remember three things: let AI do the generation, let rules do the safety, and let the gateway do the unification. Start with the smallest whitelist + a single model to get the loop working end-to-end, then gradually introduce model division of labor and fallback chains—you'll see results within two weeks.
If you're looking for a unified AI API gateway that supports multiple models, the OpenAI-compatible protocol, and real-time quota management to quickly implement scenarios like this, check out https://api.thistoken.ai/register —register and start integrating right away.
---
Ready to try it yourself? Sign up at https://api.thistoken.ai/register to get your API key and start building.Vous voulez essayer Token.AI ?
Créez une API Key au niveau du projet, activez les canaux dans la console et configurez le routage, les budgets et les journaux d'audit.
注册 ThisToken.AI 并获取 API Key