BasicsLLM OpsPricingOpenAIAnthropicGoogle
Problem
Implement estimate_api_cost(input_tokens, output_tokens, input_price_per_m, output_price_per_m) returning the total USD cost as a float when model API prices are quoted per 1,000,000 (1M) tokens.
Why It Matters: Production LLM engineering requires accurate unit-economics forecasting—especially because output tokens typically cost 3×–5× more than input tokens.
Function Signature
def estimate_api_cost(
input_tokens: int,
output_tokens: int,
input_price_per_m: float,
output_price_per_m: float,
) -> float:
"""Return total USD cost for an LLM API call."""
Sample Input & Output
Example 1: Standard API Request
Input:
input_tokens = 500_000, output_tokens = 100_000, input_price_per_m = 2.0, output_price_per_m = 8.0
Expected Output:
1.8
Explanation: 0.5M * $2.0 = $1.00 input + 0.1M * $8.0 = $0.80 output = $1.80 total.
Example 2: Single Small Chat Turn
Input:
input_tokens = 1_500, output_tokens = 500, input_price_per_m = 1.0, output_price_per_m = 4.0
Expected Output:
0.0035
Explanation: (1500 * 1.0 + 500 * 4.0) / 1_000_000 = 3500 / 1_000_000 = $0.0035.
Example 3: Zero Output Tokens
Input:
input_tokens = 250_000, output_tokens = 0, input_price_per_m = 0.40, output_price_per_m = 1.60
Expected Output:
0.1
Explanation: When output_tokens = 0, cost depends purely on input tokens (0.25 * 0.40 = 0.10).
Constraints & Edge Cases
- Return total cost in USD as a
float. - Divide by
1_000_000.0(1e6), not1_000.0. - Return a Python
float.
Constraints & Evaluation Criteria
- Return total cost in USD as a `float`.
Hints
Learn the Concept First
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
500k @ $2/M, 100k @ $8/M
Expected Output / Criteria
1.8