You are about to ship a feature that calls GPT-5.5 per request. Walk through how you estimate the token cost before launch.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
You are about to ship a feature that calls GPT-5.5 per request. Walk through how you estimate the token cost before launch.
Resolve encoding via `encoding_for_model('gpt-5.5')` (returns `o200k_base`), encode the full prompt (system + tools + user + per-message overhead), multiply by prices, and validate against `usage.prompt_tokens`.
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).
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: resolve encoding by model, assemble the full prompt (system + tools + user + overhead), count input tokens, estimate output from a real sample, validate against `usage.prompt_tokens`, and adjust for the cached input discount.
import json
import tiktoken
# Step 1: resolve the encoding by model name
enc = tiktoken.encoding_for_model('gpt-5.5') # 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 (per 1k token prices in 2026; 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 family (2026) | Encoding | How to count input |
|---|---|---|
| GPT-5.5 | o200k_base | `tiktoken.encoding_for_model('gpt-5.5')` + `.encode()` |
| GPT-4 era (4o, 4-turbo) | o200k_base / cl100k_base | Resolve via `encoding_for_model` per specific model |
| Claude Opus 4.7 | Anthropic internal | `client.messages.count_tokens(...)` |
| Gemini 3.1 Pro | Google internal | `model.count_tokens(...)` on the Vertex SDK |
| Llama 4 / open weight | Model specific | `AutoTokenizer.from_pretrained(...).encode(text)` |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.