Cost-Aware Model Router

easy
Unit 2 · RAG & LLMs0/3 to milestone

Not every request needs the biggest model. A router sends easy prompts to a cheap model and hard ones to a strong model.

Models are dicts with "name", "cost" (per 1K tokens) and "quality" (0–1). Implement:

  • difficulty(prompt): a heuristic score from 0 to 1, starting at 0 and adding 0.3 if the prompt has more than 200 words; 0.3 if it contains code (a triple backtick or the word def / class ); 0.2 for words like "prove", "derive", "analyze"/"analyse", "step by step" or "debug"; and 0.2 if it asks several questions (more than one ?). Cap at 1.0.
  • route(prompt, models, min_quality_for=lambda d: 0.5 + 0.5 * d): compute the difficulty, then pick the cheapest model whose quality is at least the required level (ties by name). If none qualifies, pick the highest-quality model.
#llm#routing#cost2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Cost-Aware Model Router · TechGradify