The Token Economist

Difficulty: MEDIUMID: ai-token-budget

The Scenario

Your free-tier AI app is bleeding money. Users are sending massive 10,000-word prompts that cost $0.50 each. Your budget is $0.01 per request.

The Problem

Your code blindly sends every request to the LLM.

response = llm.generate(prompt)  # Cha-ching!

You only find out the cost after you've paid for it.

The Goal

Implement a Pre-flight Cost Check:

  1. Estimate the token count of the prompt.
  2. Calculate the cost (Assume $0.00005 per token).
  3. If cost > budget ($0.01), reject the request.
  4. Otherwise, proceed.

Requirements:

  • Use len(prompt) // 4 as a rough token estimator.
  • Price: $0.00005 / token.
  • Budget: $0.01 / request.
  • Raise ValueError if over budget.
solution.py
Loading...
⚠️ Do not include PII or secrets in your code.
SYSTEM_LOGS
5/5
// Waiting for execution trigger...
PREVIEW MODE — SOLVE PREVIOUS MISSIONS TO UNLOCK