方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

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:

  1. Maintains a list of ModelTier configs, each with a model name, a cost weight, and capability flags
  2. Implements a route method that accepts a RoutingRequest and returns the most appropriate ModelTier
  3. 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)
  4. Respects explicit overrides (if force_model is set, use it)
  5. Falls back to the cheapest capable model if no tier fully matches

The signal rules (implement these)

  • code or reasoning tasks → prefer Sonnet-tier or above
  • free user 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

Practice stage

General drill

Hint

Keep the solution explicit and reviewable.

Success criteria

Make the solution explicit, debuggable, and easy to explain.

Review checklist

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.