You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Building a Multi-Model AI Chatbot with Python — Route by Task Complexity (2026 Guide)
#26
Most AI chatbots use a single model for everything — simple but expensive. A smarter approach: route different tasks to different models based on complexity, and let your routing logic handle cost optimization automatically.
The cost difference between models isn't marginal — it's orders of magnitude:
Model
Input (1M tokens)
Output (1M tokens)
Context
Use Case
ERNIE Tiny 8K
$0.1781
$0.1781
8K
Simple Q&A, greetings
DeepSeek V4 Pro
$0.42
$0.84
1M
Complex reasoning, long docs
A greeting ("Hi, how are you?") costs ~$0.000024 with ERNIE Tiny. Sending it to DeepSeek V4 Pro costs ~$0.000058 — 2.4x more expensive for zero quality gain. At scale, routing becomes a real lever.
The Complete Project
Here's a production-ready multi-model chatbot using AIWave's OpenAI-compatible API:
multi_model_bot/
├── bot.py # Main entry point
├── router.py # Task complexity classifier
├── models.py # Model configurations
├── config.py # API key and settings
└── requirements.txt
fromrouterimportclassify_complexityfrommodelsimportcall_modelSYSTEM_PROMPTS= {
"simple": "You are a helpful assistant. Keep responses concise and friendly.",
"complex": (
"You are a senior software engineer and technical analyst. ""Provide thorough, well-reasoned responses with code examples ""where appropriate. Think step-by-step."
),
}
classMultiModelChat:
def__init__(self):
self.total_cost=0.0self.request_count=0self.route_counts= {"simple": 0, "complex": 0}
defchat(self, message: str) ->dict:
self.request_count+=1route=classify_complexity(message)
self.route_counts[route] +=1result=call_model(
model_key=route,
message=message,
system_prompt=SYSTEM_PROMPTS[route],
)
self.total_cost+=result["cost_usd"]
result["running_total_usd"] =self.total_costresult["route_distribution"] =dict(self.route_counts)
returnresultif__name__=="__main__":
bot=MultiModelChat()
test_messages= [
"Hi, what can you help me with?",
"What is a REST API?",
"Compare ERNIE Tiny vs DeepSeek V4 Pro for a production chatbot ""handling 10K daily requests.",
"Thanks!",
"Write a Python function that implements a concurrent ""task queue with priority scheduling and retry logic.",
]
formsgintest_messages:
result=bot.chat(msg)
route_label="SIMPLE"ifresult["route"] =="simple"else"COMPLEX"print(f"Route: {route_label} -> {result['model']}")
print(f"Cost: ${result['cost_usd']:.6f}")
print(result["text"])
Understanding the Cost Flow
A realistic 5-message session:
Message
Route
Model
Est. Cost
"Hi, what can you help me with?"
Simple
ERNIE Tiny
$0.000003
"What is a REST API?"
Simple
ERNIE Tiny
$0.000012
Complex comparison question
Complex
DeepSeek V4 Pro
$0.000137
"Thanks!"
Simple
ERNIE Tiny
$0.000004
Concurrent task queue code
Complex
DeepSeek V4 Pro
$0.000182
Session total: ~$0.000338 for 5 real queries.
At 10,000 daily requests where ~60% are simple: $6/day vs $10/day — that's $120/month saved by routing intelligently.
Production Considerations
Replace heuristic router with a classifier. Use a free model like GLM-4.7-Flash ($0 on AIWave) to classify complexity with higher accuracy:
defml_classify(message: str) ->str:
response=client.chat.completions.create(
model="glm-4.7-flash", # Free on budget tiermessages=[
{"role": "system", "content": (
"Classify the user message. Reply ONLY 'simple' or 'complex'."
)},
{"role": "user", "content": message}
],
max_tokens=10,
temperature=0.0
)
returnresponse.choices[0].message.content.strip().lower()
Add caching. Identical queries shouldn't hit the API twice. Use hash-based cache with TTL.
Monitor routing distribution. If 90% route to "complex," your threshold is too low.
Get Started
All models in this article are available on AIWave with an OpenAI-compatible API. New accounts get $5 free credit.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Why Multi-Model Routing Matters
Most AI chatbots use a single model for everything — simple but expensive. A smarter approach: route different tasks to different models based on complexity, and let your routing logic handle cost optimization automatically.
The cost difference between models isn't marginal — it's orders of magnitude:
A greeting ("Hi, how are you?") costs ~$0.000024 with ERNIE Tiny. Sending it to DeepSeek V4 Pro costs ~$0.000058 — 2.4x more expensive for zero quality gain. At scale, routing becomes a real lever.
The Complete Project
Here's a production-ready multi-model chatbot using AIWave's OpenAI-compatible API:
config.py
router.py — The Intelligence Layer
models.py — Model Interaction
bot.py — Putting It Together
Understanding the Cost Flow
A realistic 5-message session:
Session total: ~$0.000338 for 5 real queries.
At 10,000 daily requests where ~60% are simple: $6/day vs $10/day — that's $120/month saved by routing intelligently.
Production Considerations
Add caching. Identical queries shouldn't hit the API twice. Use hash-based cache with TTL.
Monitor routing distribution. If 90% route to "complex," your threshold is too low.
Get Started
All models in this article are available on AIWave with an OpenAI-compatible API. New accounts get $5 free credit.
Discussion: Have you implemented multi-model routing in your projects? What heuristics or classifiers work best for you? Share your experience below!
Resources
All reactions