AI Engineer Portal
Your personal operating system for career transition.
Exercise
Implement a cost-aware model router
Implement a CostAwareRouter that selects the cheapest model capable of handling a request:
- Takes a list of
ModelOptionobjects:name: str,cost_per_1k_tokens: float,max_context_tokens: int,supports_json_mode: bool route(prompt_tokens: int, requires_json: bool = False) -> ModelOption— returns the cheapest model that:- Has
max_context_tokens >= prompt_tokens + 512(leave headroom for completion) - If
requires_json=True, only consider models wheresupports_json_mode=True
- Has
- Raises
ValueErrorif no model can handle the request estimate_cost(model: ModelOption, input_tokens: int, output_tokens: int) -> float— static method returning estimated cost in USD- Track routing decisions and expose
routing_stats() -> dictwith counts per model name
Test with three models: a cheap small model, a mid-tier model, and an expensive large model. Verify that simple requests route to the cheap model and long-context requests route to the only model that can handle them.
Deployment / medium / Step 6 of 6
General drill
Keep the solution explicit and reviewable.
This is the end of the current mini-sequence.
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.