Approach: compute tokens per request (prompt + completion), convert prices per 1K tokens to per-token, project requests per month from requests per minute and peak windows handling (base rate + extra burst windows), and sum prompt/completion costs. Allow configuration for multiple peak windows (start_minute, duration_minutes, requests_per_minute). Provide simple Monte Carlo or deterministic weighting; here I implement deterministic aggregation.
python
from typing import List, Dict
def estimate_azure_openai_cost(
avg_prompt_tokens: float,
avg_completion_tokens: float,
base_rpm: float, # requests per minute steady-state
peak_windows: List[Dict], # each: {'start_min':int,'duration_min':int,'rpm':float}
prompt_price_per_1k: float, # USD per 1000 prompt tokens
completion_price_per_1k: float,
days_per_month: int = 30
) -> float:
"""
Returns projected monthly cost in USD.
peak_windows allows modeling bursts. Pricing converted to per-token.
Deterministic: sum minutes in month with respective RPMs.
"""
minutes_per_month = days_per_month * 24 * 60
# per-token prices
pp_token = prompt_price_per_1k / 1000.0
cp_token = completion_price_per_1k / 1000.0
# Build an array or aggregate minute-by-minute could be heavy; instead compute total requests:
total_requests = base_rpm * minutes_per_month
# Add peak windows
for pw in peak_windows:
duration = pw.get("duration_min", 0)
rpm = pw.get("rpm", base_rpm)
# assume peak rpm replaces base_rpm during that window (not additive)
# if multiple windows overlap, caller should merge/adjust
total_requests += (rpm - base_rpm) * duration * (days_per_month / 30) # scale if start_min used across month
# Ensure non-negative
total_requests = max(total_requests, 0)
# tokens and cost
total_prompt_tokens = total_requests * avg_prompt_tokens
total_completion_tokens = total_requests * avg_completion_tokens
cost = total_prompt_tokens * pp_token + total_completion_tokens * cp_token
return round(cost, 4)
Key points:
- Time complexity O(W) for W peak windows.
- Handles bursts by specifying windows; assumes peak replaces base rate (adjust if additive).
Edge cases:
- Overlapping windows need pre-processing.
- Very large RPMs -> check float precision.
Alternatives:
- Monte Carlo sampling to model stochastic traffic.
- Minute-by-minute simulation for fine-grained billing (if needed).