方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Track and alert on LLM cost per feature

Track and Alert on LLM Cost Per Feature

Without cost visibility, you will discover that your most popular feature is generating $30,000/month in LLM costs via the monthly billing invoice — after the fact. The solution is instrumented cost tracking that captures cost at request time, grouped by feature.

What to build

Implement a CostLedger that:

  1. record(event: CostEvent) -> None — record a cost event with feature, model, input_tokens, output_tokens, user_id.
  2. daily_report(date_str: str | None = None) -> DailyReport — costs grouped by feature for today. Include total cost, per-feature breakdown, and top 3 costliest requests.
  3. check_alerts(budgets: dict[str, float]) -> list[BudgetAlert] — check if any feature has exceeded its daily budget.
  4. hourly_burn_rate(feature: str) -> float — average cost per hour over the last 24 hours for a feature.

Use these rates per 1000 tokens (input/output):

  • gpt-4o-mini: $0.00015 / $0.0006
  • gpt-4o: $0.0025 / $0.01
  • claude-3-5-haiku-20241022: $0.0008 / $0.004

Constraints

  • Standard library only.
  • Use datetime for timestamps.
  • Events stored in-memory.

Evaluation / medium / Step 16 of 36

Practice stage

Evaluation and review loops

Hint

Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.

Success criteria
  • - Produces a useful signal, not decorative output
  • - Makes regression review easier
  • - Would support a benchmark or observability loop
Review checklist
  • - Would this output help decide what to fix next?
  • - Are important failure modes visible?
  • - Does the score hide any ambiguity I should record?

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.