Zenaique

You are about to ship a feature that calls GPT-5.5 per request. Walk through how you estimate the token cost before launch.

Short answer·Medium·4.0 · 0·~3 min·Asked atAirbnbCrestaOpenAI
Attempt it

You are about to ship a feature that calls GPT-5.5 per request. Walk through how you estimate the token cost before launch.

Free · 2 AI evals / day
TL;DR

Use o200k_base (tiktoken has no gpt-5.5 mapping; GPT-4o and GPT-5 use it), encode the full prompt (system + tools + user + per-message overhead), multiply by prices, and validate against usage.prompt_tokens.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Think of an Uber ride. The fare is per mile, but the meter starts at pickup, not when you sit down at your destination. To estimate the fare you need to know the right rate card (which depends on the city), the actual distance the car will cover (not just the part you remember), and a way to check the estimate against the real receipt. Estimating API cost is the same. You pick the encoding for the specific model (the rate card), tokenize everything the API will send through the model (the full distance), and confirm against the response's usage field (the receipt).

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Per call cost estimation looks like simple multiplication: tokens times price. The math is trivial. The work is making sure the token count is right. Most cost estimates made before shipping are wrong. The cause is rarely the price. The estimator counted only part of what the API sends through the model.

Step 1: resolve the encoding by model name

The encoding is a model property. tiktoken.encoding_for_model('gpt-5') returns the o200k_base encoding. The model was trained with this encoding, and the API uses it to count input tokens for billing. tiktoken 0.13 has no mapping for gpt-5.5, so encoding_for_model('gpt-5.5') raises an error. Use tiktoken.get_encoding('o200k_base'), the encoding of GPT-4o and GPT-5, as the estimate, and confirm it against a real call. Calling tiktoken.get_encoding('cl100k_base') directly works but is fragile. That is the GPT-4 era encoding, and using it on an o200k model produces wrong counts. The drift is near zero on English and code but 15 percent or more on many other languages.

The pattern that holds across model upgrades is to always resolve by model name. When you upgrade from GPT-4o to GPT-5, both use o200k_base, so the encoding does not change. When you upgrade from a cl100k_base model to an o200k_base model, the encoding changes and resolving by model name catches the change automatically.

For Anthropic models, use client.messages.count_tokens(model=..., messages=...). Anthropic does not expose its tokenizer the way OpenAI does, so the only authoritative count is the server side count returned by this endpoint. For Gemini, use model.count_tokens(...) on the Vertex SDK. For open weight models (Llama 4, Qwen 3, DeepSeek V3), AutoTokenizer.from_pretrained(...) gives you the canonical tokenizer.

Step 2: assemble the full prompt as the API sends it
Step 3 and 4: count input, estimate output
Step 5: validate against a real call and account for the cache
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
python
import json
import tiktoken

# Step 1: resolve the encoding by model name. tiktoken 0.13 has no
# 'gpt-5.5' mapping, so use o200k_base (the GPT-4o and GPT-5 encoding)
enc = tiktoken.get_encoding('o200k_base')

# Step 2-3: assemble and count
system_msg = "You are a helpful assistant."
tool_schema = {"name": "search", "parameters": {"q": "string"}}
user_msg = "Find papers on RoPE scaling."

per_message_overhead = 3  # OpenAI chat completion accounting
input_tokens = (
    len(enc.encode(system_msg)) + per_message_overhead
    + len(enc.encode(json.dumps(tool_schema)))
    + len(enc.encode(user_msg)) + per_message_overhead
)

# Step 4: estimate output (use a measured median)
estimated_output_tokens = 400

# Step 5: cost (example per 1k token prices; replace with current)
INPUT_PRICE = 0.0025
OUTPUT_PRICE = 0.010
estimated_cost = (
    input_tokens / 1000 * INPUT_PRICE
    + estimated_output_tokens / 1000 * OUTPUT_PRICE
)

# Validate: send a real call and compare usage.prompt_tokens to input_tokens
Model familyEncodingHow to count input
GPT-5.5No tiktoken mapping; estimate with o200k_basetiktoken.get_encoding('o200k_base') + .encode(), then check usage.prompt_tokens
GPT-4 era (4o, 4-turbo)o200k_base / cl100k_baseResolve via encoding_for_model per specific model
Claude Opus 4.7Anthropic internalclient.messages.count_tokens(...)
Gemini 3.1 ProGoogle internalmodel.count_tokens(...) on the Vertex SDK
Llama 4 / open weightModel specificAutoTokenizer.from_pretrained(...).encode(text)

Real products, models, and research that use this idea.

  • OpenAI GPT-5: tiktoken.encoding_for_model('gpt-5') returns o200k_base; every response carries usage.prompt_tokens and usage.completion_tokens.
  • Anthropic Claude Opus 4.7: client.messages.count_tokens(model='claude-opus-4-7', messages=[...]) returns the server side input token count without consuming a generation.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QIf your estimate disagrees with usage.prompt_tokens by 20 percent, what are the first three things you check?
A

First, the encoding (cl100k_base vs o200k_base mismatch). Second, whether tool schemas are being counted (the API adds them to the prompt; the estimator might not). Third, per message overhead and chat template wrappers, which add a small but real per message cost.

1 more follow-up an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Estimating cost from len(user_message.split()) only. You miss the system message, the tool schemas, the per message overhead, and the chat template wrappers, all of which contribute to the billed input tokens.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Pick the encoding via tiktoken.encoding_for_model, and name the fallback (o200k_base) when a model such as GPT-5.5 has no mapping

  • List the prompt components that contribute to input tokens beyond the user message

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Why use subword tokens, and what is wrong with word level or character level?
Flashcard·Easy