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.
Pricing Notation: Prices are typically listed as "per 1M tokens" (MTok). For example, "$3 / 1M input tokens" means every million input tokens costs $3.00, or $0.000003 per token.

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
Prices Change: These prices are approximate and change frequently. Always check the provider's current pricing page for the latest rates.

Calculating Costs

Cost Formula
# 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
Python
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.
Huge Savings: If your system prompt is 2,000 tokens and you make 100,000 requests/day with Anthropic prompt caching, you save: 2,000 * 100,000 * $3.00/1M * 0.9 = $540/day compared to uncached.

Monthly Budget Planning

Budget Calculator
# 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.