方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Implement a cost-aware model router

Implement a CostAwareRouter that selects the cheapest model capable of handling a request:

  1. Takes a list of ModelOption objects: name: str, cost_per_1k_tokens: float, max_context_tokens: int, supports_json_mode: bool
  2. 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 where supports_json_mode=True
  3. Raises ValueError if no model can handle the request
  4. estimate_cost(model: ModelOption, input_tokens: int, output_tokens: int) -> float — static method returning estimated cost in USD
  5. Track routing decisions and expose routing_stats() -> dict with 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

Practice stage

General drill

Hint

Keep the solution explicit and reviewable.

Next drill

This is the end of the current mini-sequence.

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.