Course contentsShow
← AI Engineering
Lesson 1181 of 1,88629. Cost OptimizationFree lesson

Model-Specific Cost Calculation

Converting token counts to actual costs based on provider pricing for different models.

Model-Specific Cost Calculation

What you'll learn: How to translate your tracked token counts into accurate dollar amounts by applying the correct pricing for each model you use.

Why Model-Specific Calculation Matters

You've been tracking token usage per request, aggregating metrics, and even attributing costs by feature. But here's the catch: not all tokens cost the same. GPT-4 tokens are dramatically more expensive than GPT-3.5 tokens. Claude Sonnet differs from Claude Haiku. If you multiply all your tokens by a single price, your budget projections will be wildly wrong.

The Core Idea

Each LLM provider publishes a pricing sheet with distinct rates for:

  • Input tokens (prompt processing)
  • Output tokens (generation)
  • Model tier (e.g., GPT-4-turbo vs GPT-3.5-turbo)
  • Special features (like cached prompts, which may be cheaper)

Your job is to maintain a pricing configuration that maps each model to its current rates, then multiply your tracked token counts by the correct price.

Implementation Pattern

# Pricing lookup (cents per 1k tokens)
MODEL_PRICING = {
    "gpt-4-turbo": {"input": 1.0, "output": 3.0},
    "gpt-3.5-turbo": {"input": 0.05, "output": 0.15},
    "claude-3-sonnet": {"input": 0.3, "output": 1.5}
}

def calculate_cost(model_name, input_tokens, output_tokens):
    rates = MODEL_PRICING[model_name]
    input_cost = (input_tokens / 1000) * rates["input"]
    output_cost = (output_tokens / 1000) * rates["output"]
    return input_cost + output_cost

Analogy: Think of models like shipping carriers—UPS, FedEx, and local courier all charge different rates per pound. You need to know which carrier you used and the weight to calculate the actual cost.

Key Takeaway: Accurate cost tracking requires model-specific pricing tables that differentiate between input and output tokens, updated regularly as providers change rates.