Calculate Streaming Token Usage Costs
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
# Calculate Streaming Token Usage Costs
Implement a usage calculator for language-model API requests. Input and output tokens have different prices, and output usage may arrive through multiple streaming chunks.
```python
def calculate_cost(
requests: list[dict],
input_price_per_million: int,
output_price_per_million: int,
) -> tuple[int, int, int]:
...
```
Each request contains `input_tokens` and either `output_tokens` for a non-streaming response or `stream_chunks`, a list whose elements contain the number of newly reported output tokens in that chunk. Return total input tokens, total output tokens, and total cost in millionth-of-a-currency-unit so the computation is exact:
```text
cost = input_tokens * input_price_per_million
+ output_tokens * output_price_per_million
```
## Constraints and Clarifications
- Token counts and prices are nonnegative integers.
- A request supplies exactly one output representation.
- Streaming chunk counts are deltas, not cumulative totals.
- Empty streams and empty request lists are valid.
- Do not use binary floating point for billing.
- Reject malformed negative counts rather than silently correcting them.
## Hints
- Normalize both response forms to one per-request usage record.
- Keep aggregation separate from formatting into human-readable currency.
- Test zero prices, mixed response forms, many tiny chunks, and very large totals.
Quick Answer: Implement an exact usage calculator for language-model API requests with separate input and output token prices. Normalize streaming chunk deltas and non-streaming totals, reject malformed counts, avoid floating-point billing errors, and handle empty or very large workloads.
Aggregate input and output token usage across direct and streaming responses, then calculate exact integer billing units from separate prices. Each stream_chunks element is an object of the exact shape {'output_tokens': nonnegative_int}.
Constraints
- Counts and prices are nonnegative integers
- Each request has exactly one output representation
- Every stream_chunks element is {'output_tokens': nonnegative_int}, and these counts are deltas
Examples
Input: {'requests': [], 'input_price_per_million': 5, 'output_price_per_million': 9}
Expected Output: (0, 0, 0)
Explanation: No requests consume no tokens.
Input: {'requests': [{'input_tokens': 10, 'output_tokens': 4}], 'input_price_per_million': 2, 'output_price_per_million': 3}
Expected Output: (10, 4, 32)
Explanation: A nonstreaming request uses its direct output count.
Hints
- Normalize each output representation to one token count.
- Keep all billing arithmetic integral.