AI Engineer Portal
Your personal operating system for career transition.
Exercise
Implement model routing by task complexity
Implement Model Routing by Task Complexity
Routing every request to your most powerful model is expensive. Routing every request to your cheapest model produces weak results on complex tasks. The right engineering move is a routing layer that matches model capability to task complexity automatically.
What you are building
Build a ModelRouter class that:
- Maintains a list of
ModelTierconfigs, each with a model name, a cost weight, and capability flags - Implements a
routemethod that accepts aRoutingRequestand returns the most appropriateModelTier - Routes based on measurable signals:
- Input length in tokens (longer inputs need more capable models)
- Task type (
code,reasoning,extraction,summarization,chat) - User tier (
free,pro,enterprise)
- Respects explicit overrides (if
force_modelis set, use it) - Falls back to the cheapest capable model if no tier fully matches
The signal rules (implement these)
codeorreasoningtasks → prefer Sonnet-tier or abovefreeuser tier → cap at Haiku-tier- Input tokens > 8000 → require a model with
supports_long_context = True - All other tasks → Haiku-tier is sufficient
Why this matters
Model routing is one of the highest-leverage cost optimizations in a real LLM application. Typical production systems route 70–90% of requests to cheap models and reserve expensive models for complex tasks. The savings at scale are significant.
Llm Foundations / medium / Step 3 of 6
General drill
Keep the solution explicit and reviewable.
Make the solution explicit, debuggable, and easy to explain.
Review where the boundary is, what gets validated, and what would be hard to debug later.
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.