Module 1: AI Cost Anatomy

Tokens: The Unit of Cost in AI Systems

Capsule overview

Every cent you pay OpenAI, Anthropic or any LLM provider is measured in tokens. Not in characters, not in words, not in requests — in tokens. If you don't understand what a token is, how it's generated, and why output tokens cost more than input tokens, you're making cost decisions blindly.

This capsule gives you the foundation: what a token is, how tokenization works (BPE), the critical difference between input and output tokens, and how the context window impacts your bill. By the end, you'll be able to look at any LLM request and calculate its exact cost with pen and paper — before automating it with tiktoken in the next capsule.


What Is a Token?

They're not characters, they're not words

The most common mistake is thinking a token is a word or a character. It's neither. A token is a subword unit — a fragment of text the model uses internally to process language.

Text: "Tokenización es fascinante"

❌ If they were words: ["Tokenización", "es", "fascinante"] → 3 tokens
❌ If they were characters: ["T","o","k","e","n",...] → 27 tokens
✅ Real tokens: ["Token", "ización", " es", " fascin", "ante"] → 5 tokens

Why subwords? Because it's the sweet spot between efficiency and representational capacity:

  • Characters: Tiny vocabulary (26 letters + symbols), but very long sequences. Processing "Tokenización" takes 13 steps.
  • Whole words: Short sequences, but infinite vocabulary. What do you do with "tokenizaremos" or "anti-tokenización"?
  • Subwords: Finite vocabulary (~100K tokens) that can represent any text. "Tokenización" breaks down into reusable pieces.

The rule of thumb (and why it's only an approximation)

You'll read everywhere that "1 token ≈ 4 characters in English" or "1 token ≈ ¾ of a word." This rule works for quick mental estimates, but it fails when you need precision:

"Hello" → 1 token (5 characters, 5:1 ratio)
"AI" → 1 token (2 characters, 2:1 ratio)
"anthropomorphization" → 4 tokens (20 characters, 5:1 ratio)
"こんにちは" → 3 tokens (5 characters, 1.7:1 ratio)
" " (space) → 1 token (1 character, 1:1 ratio)

The ratio varies dramatically depending on language, technical vocabulary, and format. In Spanish, the ratio is usually worse than in English (more tokens per word) because tokenizers are optimized for English.

Bottom line: For mental estimates, use the 4-character rule. To calculate real costs, use tiktoken (next capsule). Never make budget decisions based on the 4-character rule.


How Tokenization Works: BPE

Byte Pair Encoding explained with an analogy

Imagine you have a keyboard with only 256 keys (one per byte). You can type any text, but character by character. It's slow.

Now imagine you can add custom keys. You notice "th" shows up constantly in English, so you add a key that types "th" in one stroke. Then you see "the" is even more frequent — you add that key too. You keep adding keys for the most frequent pairs and sequences until you have ~100,000 custom keys.

That's Byte Pair Encoding (BPE): an algorithm that analyzes a massive text corpus and, iteratively, merges the most frequent byte pairs into new tokens.

The simplified process

Step 0 - Initial vocabulary: all individual bytes (256)
         "lower" → ["l", "o", "w", "e", "r"]

Step 1 - Most frequent pair in the corpus: ("e", "r") → "er"
         "lower" → ["l", "o", "w", "er"]

Step 2 - Next most frequent pair: ("l", "o") → "lo"
         "lower" → ["lo", "w", "er"]

Step 3 - Next: ("lo", "w") → "low"
         "lower" → ["low", "er"]

Step 4 - Next: ("low", "er") → "lower"
         "lower" → ["lower"]

... (repeats ~100,000 times over a huge corpus)

The result is a vocabulary of ~100,000 tokens that can represent any text efficiently. Common words become a single token. Rare words break down into known subwords.

Practical implications for costs

This has direct consequences on your bill:

# Common English words → few tokens (efficient)
"the"1 token
"Hello world"2 tokens
"function"1 token

# Common Spanish words → more tokens (less efficient)
"función"2 tokens    # "func" + "ión"
"desarrollador"3 tokens  # "des" + "arroll" + "ador"

# Code → varies a lot
"print('hello')"4 tokens
"console.log('hello')"6 tokens

# JSON → many tokens (inefficient)
'{"name": "Juan", "age": 30}'13 tokens

Cost implication: A prompt in Spanish consumes ~20-30% more tokens than the same content in English. And responses in JSON format consume significantly more tokens than plain text.


Input Tokens vs Output Tokens

The difference that multiplies your bill

Every LLM call has two cost components:

Total cost = (input_tokens × input_price) + (output_tokens × output_price)
  • Input tokens: Everything you send to the model — system prompt, previous chat messages, the user's message, RAG context, etc.
  • Output tokens: Everything the model generates as a response.

Why output costs 2-4x more than input

Output tokens cost significantly more than input tokens across all providers:

GPT-4:
  Input:  $0.03 / 1K tokens
  Output: $0.06 / 1K tokens  → 2x more expensive

GPT-3.5-turbo:
  Input:  $0.0005 / 1K tokens
  Output: $0.0015 / 1K tokens → 3x more expensive

GPT-4o-mini:
  Input:  $0.00015 / 1K tokens
  Output: $0.0006 / 1K tokens  → 4x more expensive

Why? Output generation is computationally more intensive. To process input, the model does a single parallel forward pass over all tokens. To generate output, the model produces one token at a time, sequentially, and each token requires a forward pass that considers every previous token.

Input (parallel):
  [system prompt + user message] → 1 forward pass → internal representation
  
Output (sequential):
  Token 1 → forward pass → "The"
  Token 2 → forward pass (with "The") → " capital"
  Token 3 → forward pass (with "The capital") → " of"
  Token 4 → forward pass (with "The capital of") → " France"
  ...each token is more expensive than the last

A real cost calculation example

Imagine a chatbot that answers questions. A typical request:

Input (500 tokens):
  - System prompt: 200 tokens
  - Chat history: 150 tokens
  - User question: 50 tokens
  - RAG context: 100 tokens

Output (300 tokens):
  - Model response: 300 tokens

Cost per request by model:

GPT-4:
  Input:  500 / 1000 × $0.03  = $0.015
  Output: 300 / 1000 × $0.06  = $0.018
  Total: $0.033 per request

GPT-3.5-turbo:
  Input:  500 / 1000 × $0.0005  = $0.00025
  Output: 300 / 1000 × $0.0015  = $0.00045
  Total: $0.0007 per request

GPT-4o-mini:
  Input:  500 / 1000 × $0.00015 = $0.000075
  Output: 300 / 1000 × $0.0006  = $0.00018
  Total: $0.000255 per request

The difference is brutal:

Same request, different model:
  GPT-4:       $0.033     (reference)
  GPT-3.5:     $0.0007    (47x cheaper)
  GPT-4o-mini: $0.000255  (129x cheaper)

A request that costs 3.3 cents on GPT-4 costs a fraction of a cent on GPT-4o-mini. Model selection is the biggest optimization lever — but you'll see that in Module 7.


Context Window and Its Relationship to Cost

What the context window is

The context window is the maximum number of tokens a model can process in a single call (input + output combined):

Model           Context Window
GPT-4           8,192 tokens
GPT-4-32k       32,768 tokens
GPT-4-turbo     128,000 tokens
GPT-4o          128,000 tokens
GPT-4o-mini     128,000 tokens

More context window ≠ use it all

A costly mistake is thinking "I have 128K tokens of context, so I can send everything." Every context token you send is billed as an input token.

# Scenario: RAG system that retrieves documents
# You have 128K of context window available

# ❌ Expensive approach: send ALL the retrieved context
rag_context = retrieve_documents(query, top_k=50)  # ~40,000 tokens
# Cost on GPT-4: 40,000 / 1000 × $0.03 = $1.20 in input alone

# ✅ Optimized approach: send only what's relevant
rag_context = retrieve_documents(query, top_k=5)   # ~4,000 tokens
# Cost on GPT-4: 4,000 / 1000 × $0.03 = $0.12 in input alone

10x less cost just from sending 5 documents instead of 50. And response quality is usually the same or better (less noise in the context).

The cumulative cost of chat history

In a multi-turn chatbot, the history grows with every message. Each time you send a request, you include the whole history as input tokens:

Turn 1: Input = system(200) + user(50) = 250 tokens
Turn 2: Input = system(200) + user(50) + assistant(300) + user(60) = 610 tokens
Turn 3: Input = system(200) + user(50) + assistant(300) + user(60) + assistant(280) + user(45) = 935 tokens
Turn 4: Input = 935 + assistant(250) + user(55) = 1,240 tokens
...
Turn 10: Input = ~3,500 tokens (cumulative)

Every chat turn is more expensive than the last because it includes the whole history. In capsule 07 you'll see how to calculate the cumulative cost of a full chat.


Tokens Across Different Types of Text

Code vs natural text vs JSON

Token count varies significantly by content type:

import tiktoken

enc = tiktoken.encoding_for_model("gpt-4")

texts = {
    "Natural text (ES)": "La inteligencia artificial está transformando la industria del software de maneras que nadie anticipó hace una década.",
    "Natural text (EN)": "Artificial intelligence is transforming the software industry in ways nobody anticipated a decade ago.",
    "Python code": "def calculate_total(items):\n    return sum(item.price * item.quantity for item in items)",
    "JSON": '{"user": {"name": "María García", "age": 28, "email": "maria@example.com", "roles": ["admin", "editor"]}}',
    "Markdown": "## Main Title\n\n- Point one with **bold**\n- Point two with `code`\n- Point three with [a link](https://example.com)",
}

for label, text in texts.items():
    tokens = len(enc.encode(text))
    chars = len(text)
    ratio = chars / tokens
    print(f"{label}:")
    print(f"  Characters: {chars}, Tokens: {tokens}, Ratio: {ratio:.1f} chars/token")
    print()
# Expected output:
Natural text (ES):
  Characters: 109, Tokens: 28, Ratio: 3.9 chars/token

Natural text (EN):
  Characters: 97, Tokens: 18, Ratio: 5.4 chars/token

Python code:
  Characters: 83, Tokens: 24, Ratio: 3.5 chars/token

JSON:
  Characters: 101, Tokens: 34, Ratio: 3.0 chars/token

Markdown:
  Characters: 140, Tokens: 42, Ratio: 3.3 chars/token

Notice the patterns:

  • English has the best ratio (~5.4 chars/token) because tokenizers are optimized for English
  • Spanish uses ~30% more tokens than English to express the same thing (~3.9 chars/token)
  • JSON is the most expensive format because of all the punctuation, braces, and quotes
  • Code varies a lot depending on the language and style

Practical implication

If your system responds in JSON when it could respond in plain text, you're paying a token premium:

Plain text response:
  "María García, 28 años, admin y editor"
  → ~12 tokens

JSON response:
  {"name": "María García", "age": 28, "roles": ["admin", "editor"]}
  → ~25 tokens

Same content, twice the tokens. Multiply by thousands of requests a day.


Comparison: Cost by Model

Quick reference table

┌─────────────────┬──────────────┬──────────────┬──────────────┐
│ Model           │ Input/1K     │ Output/1K    │ Typical      │
│                 │ tokens       │ tokens       │ request cost*│
├─────────────────┼──────────────┼──────────────┼──────────────┤
│ GPT-4           │ $0.03        │ $0.06        │ $0.033       │
│ GPT-4-turbo     │ $0.01        │ $0.03        │ $0.014       │
│ GPT-4o          │ $0.005       │ $0.015       │ $0.0070      │
│ GPT-4o-mini     │ $0.00015     │ $0.0006      │ $0.000255    │
│ GPT-3.5-turbo   │ $0.0005      │ $0.0015      │ $0.0007      │
└─────────────────┴──────────────┴──────────────┴──────────────┘

* Typical request: 500 input tokens + 300 output tokens

Projection at daily scale

If your system processes 1,000 requests/day with the same typical request (500 input + 300 output):

┌─────────────────┬──────────────┬──────────────┬──────────────┐
│ Model           │ Cost/day     │ Cost/month   │ Cost/year    │
├─────────────────┼──────────────┼──────────────┼──────────────┤
│ GPT-4           │ $33.00       │ $990.00      │ $12,045.00   │
│ GPT-4-turbo     │ $14.00       │ $420.00      │ $5,110.00    │
│ GPT-4o          │ $7.00        │ $210.00      │ $2,555.00    │
│ GPT-4o-mini     │ $0.26        │ $7.65        │ $93.08       │
│ GPT-3.5-turbo   │ $0.70        │ $21.00       │ $255.50      │
└─────────────────┴──────────────┴──────────────┴──────────────┘

The difference between GPT-4 and GPT-4o-mini at 1,000 requests/day is $982/month. If your use case doesn't require GPT-4's capability for every request, you're burning money.

When to use each model

GPT-4/GPT-4-turbo:
  ✅ Complex reasoning, deep analysis
  ✅ Tasks where quality justifies the cost
  ❌ Simple answers, classification, extraction

GPT-4o:
  ✅ Quality/cost balance for most tasks
  ✅ Multimodal (text + image)
  ❌ Trivial tasks where mini is enough

GPT-4o-mini:
  ✅ Classification, data extraction, simple summaries
  ✅ High volume, low budget
  ❌ Complex multi-step reasoning

GPT-3.5-turbo:
  ✅ Simple legacy tasks
  ⚠️ Being replaced by GPT-4o-mini (better quality, similar cost)

In Module 7 you'll learn to implement automatic routing that sends each request to the optimal model based on its complexity.


The Master Cost Formula

For an individual request

def calculate_request_cost(
    input_tokens: int,
    output_tokens: int,
    input_price_1k: float,
    output_price_1k: float
) -> dict:
    """Calculate the cost of an individual request."""
    input_cost = (input_tokens / 1000) * input_price_1k
    output_cost = (output_tokens / 1000) * output_price_1k
    total_cost = input_cost + output_cost

    return {
        "input_tokens": input_tokens,
        "output_tokens": output_tokens,
        "input_cost": input_cost,
        "output_cost": output_cost,
        "total_cost": total_cost,
        "output_percentage": (output_cost / total_cost * 100) if total_cost > 0 else 0,
    }


# Example: typical request on GPT-4
result = calculate_request_cost(
    input_tokens=500,
    output_tokens=300,
    input_price_1k=0.03,
    output_price_1k=0.06
)

print(f"Input cost:  ${result['input_cost']:.4f}")
print(f"Output cost: ${result['output_cost']:.4f}")
print(f"Total cost:  ${result['total_cost']:.4f}")
print(f"% that is output: {result['output_percentage']:.1f}%")
# Expected output:
Input cost:  $0.0150
Output cost: $0.0180
Total cost:  $0.0330
% that is output: 54.5%

Notice that output accounts for 54.5% of total cost despite being fewer tokens (300 vs 500). This pattern repeats in practically every scenario: optimizing output tokens has more impact than optimizing input tokens.


Connection to the Project

Cost Breakdown Calculator

The understanding of tokens you gain here is the foundation of the Cost Breakdown Calculator (this module's project). Without understanding that:

  • Output tokens cost 2-4x more than input tokens
  • Response format (JSON vs text) impacts the token count
  • The cumulative context window in multi-turn chats multiplies costs
  • Model selection can cut costs 100x

...the calculator would be a black box. Now you understand what it calculates, why, and how to interpret the results.

Immediate next step

In the next capsule you'll learn to use tiktoken to count tokens programmatically. That turns this capsule's formulas from "manual calculations" into "automated functions" that process any text or conversation.


Troubleshooting

Problem 1: "Are the prices you show up to date?"

Cause: LLM prices change frequently. OpenAI has cut prices multiple times since the GPT-4 launch.

Solution: The prices in this capsule are a reference for understanding the structure of costs (input vs output, ratios between models). For current prices, always check OpenAI Pricing. What matters isn't memorizing "$0.03/1K" but understanding that output always costs more than input and that smaller models are orders of magnitude cheaper.

Problem 2: "My Spanish text uses more tokens than the same content in English"

Cause: OpenAI's tokenizers (BPE) are trained predominantly on English text. Spanish words break down into more subwords.

Solution: This is a fact of the current ecosystem, not a bug. Keep it in mind when calculating costs: multiply your token estimates by 1.2-1.3 if your system processes Spanish. In the tiktoken capsule you'll learn to measure this precisely for your specific case.

Problem 3: "Why doesn't my bill match the tokens × price calculation?"

Cause: There are additional costs that aren't completion tokens: embeddings calls, fine-tuning, images (DALL-E), audio (Whisper), and API overhead. Plus, if you use chat completions, there's token overhead from the message format (metadata tokens like <|im_start|>).

Solution: Capsule 05 covers these hidden costs in detail. For now, understand that input + output tokens are the main component (~70-80% of a typical bill) but not the only one.

Problem 4: "How do I know how many tokens the model generates in output?"

Cause: You can't know in advance exactly how many tokens the model will generate. You can cap it with max_tokens, but the model may generate fewer.

Solution: Use max_tokens to put a ceiling on cost. The OpenAI API returns usage.completion_tokens in every response, telling you exactly how many tokens it generated. In the next capsule you'll see how to use tiktoken to estimate before calling, and the API response to verify afterward.


Exercises

Exercise 1: Calculate the cost of a request (Easy)

Your system sends a 150-token system prompt, an 80-token user message, and the model responds with 200 tokens. Calculate the cost on GPT-4 and on GPT-4o-mini.

See solution
Input tokens: 150 + 80 = 230
Output tokens: 200

GPT-4:
  Input:  230 / 1000 × $0.03 = $0.0069
  Output: 200 / 1000 × $0.06 = $0.0120
  Total: $0.0189

GPT-4o-mini:
  Input:  230 / 1000 × $0.00015 = $0.0000345
  Output: 200 / 1000 × $0.0006  = $0.00012
  Total: $0.0001545

Difference: GPT-4 costs 122x more than GPT-4o-mini for the same request.

Explanation: Most of the cost sits in the output tokens, even though there are fewer of them than input tokens. That's because of the higher price per output token. On GPT-4, output accounts for 63% of the total cost ($0.012 / $0.0189).

Exercise 2: Compare response formats (Easy)

Your system can respond in plain text or in JSON. The response contains: a person's name, age, city, and occupation.

  1. Write both versions of the response
  2. Estimate how many tokens each format uses (use the ~4 chars/token rule as an approximation)
  3. Calculate the annual cost difference if you have 5,000 requests/day on GPT-4o
See solution
Plain text response:
  "María García, 28 años, vive en Ciudad de México, trabaja como ingeniera de software"
  ~85 characters → ~21 tokens (4-char rule)

JSON response:
  {"name": "María García", "age": 28, "city": "Ciudad de México", "occupation": "ingeniera de software"}
  ~103 characters → ~26 tokens (4-char rule, but JSON has a worse ratio → ~30 real tokens)

Difference per request (~9 extra output tokens):
  GPT-4o output: 9 / 1000 × $0.015 = $0.000135

Daily difference: $0.000135 × 5,000 = $0.675
Monthly difference: $0.675 × 30 = $20.25
Annual difference: $20.25 × 12 = $243.00

Explanation: $243/year just for using JSON instead of plain text in one field. Multiply that across all your API endpoints and the difference becomes significant. This doesn't mean "never use JSON" — sometimes the format is necessary for the frontend. But if you can choose, choose the more token-efficient format.

Exercise 3: Estimate a system's daily cost (Medium)

Your chatbot has these characteristics:

  • 2,000 conversations per day
  • Each conversation averages 5 turns (5 user messages + 5 responses)
  • System prompt: 300 tokens (sent on every request)
  • Average user message: 60 tokens
  • Average model response: 250 tokens
  • Model: GPT-4o ($0.005/1K input, $0.015/1K output)

Calculate the total daily cost, accounting for the history accumulating.

See solution
system_prompt = 300
user_msg = 60
model_response = 250
turns = 5
conversations_per_day = 2000

total_cost = 0

for turn in range(1, turns + 1):
    # Input: system + accumulated history + current message
    history_tokens = (turn - 1) * (user_msg + model_response)
    input_tokens = system_prompt + history_tokens + user_msg
    output_tokens = model_response

    input_cost = (input_tokens / 1000) * 0.005
    output_cost = (output_tokens / 1000) * 0.015

    turn_cost = input_cost + output_cost
    total_cost += turn_cost

    print(f"Turn {turn}: input={input_tokens}, output={output_tokens}, cost=${turn_cost:.4f}")

print(f"\nCost per conversation: ${total_cost:.4f}")
print(f"Daily cost (2000 conv): ${total_cost * conversations_per_day:.2f}")
print(f"Monthly cost: ${total_cost * conversations_per_day * 30:.2f}")
# Expected output:
Turn 1: input=360, output=250, cost=$0.0056
Turn 2: input=670, output=250, cost=$0.0071
Turn 3: input=980, output=250, cost=$0.0086
Turn 4: input=1290, output=250, cost=$0.0102
Turn 5: input=1600, output=250, cost=$0.0118

Cost per conversation: $0.0433
Daily cost (2000 conv): $86.50
Monthly cost: $2,595.00

Explanation: The cost per turn grows linearly because the history accumulates. Turn 5 costs more than twice what turn 1 does. The monthly cost of $2,595 is substantial — and most of it comes from the accumulated history in the input. Strategies like truncating or summarizing history (Module 3) can cut this dramatically.

Exercise 4: Analyze the impact of output (Medium)

Your system has two types of endpoints:

  • Endpoint A (classification): 200 input tokens, 5 output tokens ("positive" or "negative")
  • Endpoint B (generation): 200 input tokens, 800 output tokens (long summary)

Both get 500 requests/day on GPT-4. Calculate the cost of each and determine which would benefit more from switching to GPT-4o-mini.

See solution
Endpoint A (classification) on GPT-4:
  Input:  200/1000 × $0.03 × 500 = $3.00
  Output: 5/1000 × $0.06 × 500   = $0.15
  Daily total: $3.15
  % output: 4.8%

Endpoint B (generation) on GPT-4:
  Input:  200/1000 × $0.03 × 500 = $3.00
  Output: 800/1000 × $0.06 × 500 = $24.00
  Daily total: $27.00
  % output: 88.9%

If you switch Endpoint A to GPT-4o-mini:
  Input:  200/1000 × $0.00015 × 500 = $0.015
  Output: 5/1000 × $0.0006 × 500    = $0.0015
  Daily total: $0.0165
  Savings: $3.15 - $0.0165 = $3.13/day → $94.01/month

If you switch Endpoint B to GPT-4o-mini:
  Input:  200/1000 × $0.00015 × 500 = $0.015
  Output: 800/1000 × $0.0006 × 500  = $0.24
  Daily total: $0.255
  Savings: $27.00 - $0.255 = $26.75/day → $802.35/month

Explanation: Endpoint B benefits 8.5x more from the model switch ($802 vs $94 in monthly savings) because it has many output tokens. Classification (Endpoint A) is a perfect candidate for GPT-4o-mini: the task is simple, quality holds up, and the savings are 99.5%. Endpoint B requires evaluating whether GPT-4o-mini maintains summary quality — but the economic incentive is enormous.

Exercise 5: Design a reduction strategy (Hard)

Your current system spends $3,000/month on OpenAI. After analyzing, you discover this distribution:

- 40% ($1,200): Output tokens in long responses (GPT-4)
- 25% ($750): Input tokens from an oversized context window (GPT-4)
- 20% ($600): Embeddings for RAG (text-embedding-ada-002)
- 10% ($300): Retries from rate limiting
- 5% ($150): Repetitive system prompts

Propose 3 concrete optimizations and estimate the savings from each (without implementing yet — just the strategy).

See solution

Optimization 1: Reduce output tokens

  • Technique: Ask for more concise responses, use text format instead of JSON where possible, set appropriate max_tokens.
  • Estimated savings: 30-40% of output cost → $360-$480/month

Optimization 2: Reduce context window

  • Technique: Go from top_k=20 to top_k=5 in RAG, truncate chat history to the last 3 turns, summarize old context.
  • Estimated savings: 50-60% of the input cost from context → $375-$450/month

Optimization 3: Eliminate retries from rate limiting

  • Technique: Implement exponential backoff, a request queue, respect rate limits with throttling.
  • Estimated savings: 80-90% of the retry cost → $240-$270/month

Total estimated savings: $975-$1,200/month (32-40% reduction)

Additional optimizations (later modules):

  • Caching identical responses (Module 5): an extra $200-400/month
  • Semantic caching (Module 6): an extra $150-300/month
  • Model selection — using GPT-4o-mini for simple tasks (Module 7): an extra $300-600/month

With every optimization applied, the reduction can reach 50-80%.

Explanation: Before touching any code, you can already prioritize optimizations by impact. Output tokens are the biggest spend → attack them first. Retries are money completely wasted → eliminate them. Context window is the optimization with the best effort/result ratio.

Exercise 6: Calculate a model break-even (Hard)

Your team is debating whether to use GPT-4 or GPT-4o for a legal document analysis system. GPT-4 has better quality but GPT-4o costs less. If every model error costs $50 in human review, and GPT-4 has a 2% error rate vs GPT-4o's 5% error rate:

  1. Calculate the total cost (API + errors) per 1,000 requests for each model
  2. Determine at how many requests/day GPT-4 is justified by its lower error rate

Assume 400 input tokens, 500 output tokens per request.

See solution
API cost per request:

GPT-4:
  Input:  400/1000 × $0.03  = $0.012
  Output: 500/1000 × $0.06  = $0.030
  API total: $0.042

GPT-4o:
  Input:  400/1000 × $0.005  = $0.002
  Output: 500/1000 × $0.015  = $0.0075
  API total: $0.0095

Total cost (API + errors) per 1,000 requests:

GPT-4:
  API: $0.042 × 1000 = $42.00
  Errors: 1000 × 2% × $50 = $1,000
  Total: $1,042.00

GPT-4o:
  API: $0.0095 × 1000 = $9.50
  Errors: 1000 × 5% × $50 = $2,500
  Total: $2,509.50

Break-even (where GPT-4 total cost = GPT-4o total cost):
  0.042x + 0.02x(50) = 0.0095x + 0.05x(50)
  0.042x + x = 0.0095x + 2.5x
  1.042x = 2.5095x
  → GPT-4 is ALWAYS cheaper once you include the cost of errors

But if the cost per error = $5 (instead of $50):
  GPT-4 total/1000:  $42 + $100 = $142
  GPT-4o total/1000: $9.50 + $250 = $259.50
  → GPT-4 still wins

If the cost per error = $0.50:
  GPT-4 total/1000:  $42 + $10 = $52
  GPT-4o total/1000: $9.50 + $25 = $34.50
  → GPT-4o wins when errors are cheap to fix

Explanation: The total cost of a model isn't just the API price. When errors are expensive (legal, medical, financial domains), a pricier but more accurate model can be cheaper overall. The break-even depends on the cost per error in your specific domain. This kind of analysis is what separates a cost decision made "on instinct" from one made "on data."


Summary

In this capsule you learned:

  • ✅ A token is a subword unit, not a character or a word — it's generated via Byte Pair Encoding (BPE) over a vocabulary of ~100K tokens
  • ✅ The "~4 characters per token" rule is an approximation that fails with non-English languages, code, and JSON — for real costs you need tiktoken
  • ✅ Output tokens cost 2-4x more than input tokens because generation is sequential and computationally more intensive
  • ✅ The cumulative context window in multi-turn chats makes every turn more expensive than the last
  • ✅ Model selection is the biggest optimization lever: GPT-4o-mini can be 100x cheaper than GPT-4
  • ✅ Response format directly impacts tokens: JSON costs more than plain text for the same content
  • ✅ Total cost includes more than API tokens — model errors have a real cost that affects model choice

Next capsule: tiktoken — programmatic token counting to calculate costs precisely.


Additional Resources

  1. OpenAI Pricing - Current prices for all OpenAI models
  2. OpenAI Tokenizer Tool - Visual tool to explore how text gets tokenized
  3. Byte Pair Encoding (Hugging Face) - Detailed explanation of the BPE algorithm
  4. tiktoken GitHub - Official library for token counting
  5. OpenAI Token Usage FAQ - Official FAQ on tokens and counting
  6. LLM Pricing Comparison (llmprices.dev) - Up-to-date price comparison across providers
  7. Anthropic Pricing - Pricing for Claude models, for comparison
  8. Understanding GPT Tokenization (Simon Willison) - Deep dive on tokenization in GPT

Created: March 2026 Version: 1.0