Cost & Pricing Intermediate
AI API pricing is based on token usage. Understanding how pricing works - and the significant differences between models - is essential for building cost-effective applications.
How API Pricing Works
Most AI providers charge per million tokens (MTok). Two key principles:
- Input and output are priced separately. Output tokens typically cost 2-5x more than input tokens because they require more computation.
- You pay for every token processed, including system prompts, conversation history, and formatting tokens.
Price Comparison Across Models
| Model | Input (per 1M) | Output (per 1M) | Tier |
|---|---|---|---|
| Claude Opus 4 | $15.00 | $75.00 | Premium |
| Claude Sonnet 4 | $3.00 | $15.00 | Standard |
| Claude Haiku 3.5 | $0.80 | $4.00 | Budget |
| GPT-4o | $2.50 | $10.00 | Standard |
| GPT-4o-mini | $0.15 | $0.60 | Budget |
| Gemini 1.5 Pro | $1.25 | $5.00 | Standard |
| Gemini 1.5 Flash | $0.075 | $0.30 | Budget |
Calculating Costs
# Cost per request: cost = (input_tokens * input_price_per_token) + (output_tokens * output_price_per_token) # Example: Claude Sonnet 4 request # Input: 2,000 tokens | Output: 500 tokens input_cost = 2000 * ($3.00 / 1,000,000) = $0.006 output_cost = 500 * ($15.00 / 1,000,000) = $0.0075 total_cost = $0.006 + $0.0075 = $0.0135 per request # At 10,000 requests per day: daily_cost = $0.0135 * 10,000 = $135.00 / day monthly_cost = $135.00 * 30 = $4,050.00 / month
def estimate_cost(input_tokens, output_tokens, model="claude-sonnet"): pricing = { "claude-opus": {"input": 15.0, "output": 75.0}, "claude-sonnet": {"input": 3.0, "output": 15.0}, "claude-haiku": {"input": 0.8, "output": 4.0}, "gpt-4o": {"input": 2.5, "output": 10.0}, "gpt-4o-mini": {"input": 0.15, "output": 0.6}, } p = pricing[model] cost = (input_tokens * p["input"] + output_tokens * p["output"]) / 1_000_000 return round(cost, 6) # Example usage cost = estimate_cost(2000, 500, "claude-sonnet") print(f"Cost per request: ${cost}") # $0.0135
Batch Pricing & Prompt Caching
Batch API Discounts
Both Anthropic and OpenAI offer batch APIs that process requests asynchronously at a discount:
- Anthropic Batches: 50% discount on both input and output tokens
- OpenAI Batch API: 50% discount, results within 24 hours
Prompt Caching
Prompt caching lets you reuse previously processed input tokens at a reduced cost:
| Provider | Cache Discount | How It Works |
|---|---|---|
| Anthropic | 90% off cached input | Mark static content with cache_control. Cached tokens cost 10% of normal input price. |
| OpenAI | 50% off cached input | Automatic caching for prompts over 1,024 tokens. No configuration needed. |
Monthly Budget Planning
# Step 1: Estimate tokens per request avg_input_tokens = 1500 # system + history + user message avg_output_tokens = 400 # typical response # Step 2: Estimate request volume daily_requests = 5000 monthly_requests = daily_requests * 30 # = 150,000 # Step 3: Calculate monthly tokens monthly_input = avg_input_tokens * monthly_requests # = 225M tokens monthly_output = avg_output_tokens * monthly_requests # = 60M tokens # Step 4: Calculate cost (Claude Sonnet) monthly_cost = (225 * $3.00) + (60 * $15.00) = $675 + $900 = $1,575 / month # Step 5: Add 20% buffer for variance budget = $1,575 * 1.20 = $1,890 / month
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX