Dollar Cost of a Token-Priced Call
Problem Statement
Compute the dollar cost of an LLM call given separate per-million prices for input and output tokens.
Background
Providers price input and output tokens differently, quoted per million tokens. The cost is **(input_tokens/1e6)input_price + (output_tokens/1e6)output_price.
Your Task
def call_cost(input_tokens, output_tokens, input_price, output_price):
Return the cost in dollars, rounded to 6 decimals.
Input Format
- input_tokens, output_tokens (int); input_price, output_price (float, per 1M tokens).
Output Format
- A float rounded to 6 decimals.
Sample
print(call_cost(1000, 500, 3.0, 15.0))
Output:
0.0105
Example:
print(call_cost(1000, 500, 3.0, 15.0))
0.0105
- Calculate the cost for input tokens by dividing the token count by one million to normalize the unit, then multiplying by the per-million price: 1,000,0001000​×3.0=0.001×3.0=0.003 dollars.
- Calculate the cost for output tokens using the same normalization and the higher output price: 1,000,000500​×15.0=0.0005×15.0=0.0075 dollars.
- Sum the input and output costs to determine the total expense for the call: 0.003+0.0075=0.0105 dollars.
- Round the total cost to 6 decimal places as required by the output format; since the value is already exact, it remains 0.0105.
- The final output is 0.0105
Constraints:
- Prices are per 1,000,000 tokens.
- Cost = in/1e6in_price + out/1e6out_price.
- Round to 6 decimals.
1. Background Knowledge
In Large Language Model (LLM) inference, providers charge based on the number of tokens processed. A token is a sub-word unit of text, and the total cost of a request depends on two distinct components: the input tokens (the prompt sent to the model) and the output tokens (the generated response). These two components are priced separately because generating output tokens is computationally more expensive than processing input tokens.
Prices are typically quoted in dollars per million tokens (e.g., 3.00per1Minputtokens).Thisunitconventionmeansthatifyouprocess1,000inputtokensatarateof3.00 per million, the cost is not 3.00,butratherafractionofthatprice.Theconversionfactoris10^6$ (one million). Understanding this scaling is critical to avoid off-by-orders-of-magnitude errors.
The total cost is a linear combination of the input and output costs. Since the pricing model is linear (no volume discounts or tiered rates in this problem), the total cost is simply the sum of the individual component costs. This makes the calculation straightforward arithmetic, but precision handling becomes important when dealing with floating-point numbers.
2. Algorithm Approach
This is a direct arithmetic computation problem. There is no complex algorithmic pattern (like sorting, searching, or graph traversal) involved. The approach is:
- Scale the token counts to the "per million" unit by dividing by 106.
- Multiply each scaled token count by its respective price.
- Sum the two resulting costs.
- Round the final result to the required precision (6 decimal places).
The key is to ensure the order of operations and the scaling factor are applied correctly. You are essentially converting from "tokens" to "millions of tokens" before applying the per-million price.
3. Step-by-Step Strategy
- Identify the scaling factor: The prices are per 1,000,000 tokens. Define a constant or use the literal 1_000_000 (or 1e6) for clarity.
- Calculate input cost: Divide input_tokens by the scaling factor, then multiply by input_price.
- Formula: input_cost=106input_tokens​×input_price
- Calculate output cost: Divide output_tokens by the scaling factor, then multiply by output_price.
- Formula: output_cost=106output_tokens​×output_price
- Sum the costs: Add input_cost and output_cost to get the total raw cost.
- Round the result: Use Python's built-in round() function to round the total to 6 decimal places.
- return round(total_cost, 6)
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.