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:
- Estimate the token count of the prompt.
- Calculate the cost (Assume $0.00005 per token).
- If cost > budget ($0.01), reject the request.
- Otherwise, proceed.
Requirements:
- Use
len(prompt) // 4as a rough token estimator. - Price: $0.00005 / token.
- Budget: $0.01 / request.
- Raise
ValueErrorif 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