LLM Tokens and Tokenization Explained (and Why They Affect Cost)
What tokens are, how BPE tokenizers split text, why code and non-English text use more tokens, and how tokens drive context limits and API pricing.
Published December 1, 2026
Language models do not read characters or words; they read tokens. Every limit and price you meet when building with LLMs, context window size, max output length, cost per request, is measured in tokens. Understanding how text becomes tokens helps you estimate costs, fit more into context and avoid surprising truncations.
The concept
Most modern models use subword tokenizers such as Byte-Pair Encoding (BPE). The tokenizer has a fixed vocabulary of tens of thousands to a few hundred thousand pieces learned from training data. Common words become single tokens; rare words split into several pieces; whitespace and punctuation are often attached to neighbouring pieces. A rough rule for English prose is about four characters, or three-quarters of a word, per token.
Token counts vary by content and by model family. Code, JSON, numbers, URLs and many non-English scripts often take more tokens per character than English prose. Because each provider uses its own tokenizer, the same text can have different token counts across models, so always count with the tokenizer for the model you call.
From text to token IDs
Tokens per content type
Where tokens matter
| Limit or cost | Counted in | Practical effect |
|---|---|---|
| Context window | Input + output tokens | How much you can send and receive in one call |
| Max output tokens | Output tokens | Long answers can be cut off |
| Price | Per million input / output tokens | Output is usually pricier than input |
| Rate limits | Tokens per minute | Throughput ceiling for your app |
| Prompt caching | Cached input tokens | Repeated prefixes billed cheaper |
Practical examples
Count tokens with tiktoken (OpenAI tokenizers)
Use the tokenizer matching your model; other providers expose token counting APIs.
import tiktoken
enc = tiktoken.get_encoding("o200k_base")
text = "Unbelievable! Tokens drive cost."
ids = enc.encode(text)
print(len(ids), [enc.decode([i]) for i in ids])Estimate request cost
Plug in your model's current prices.
def cost_usd(input_tokens, output_tokens, in_per_m, out_per_m):
return input_tokens / 1e6 * in_per_m + output_tokens / 1e6 * out_per_m
print(cost_usd(12_000, 800, in_per_m=3.0, out_per_m=15.0)) # 0.048Shrink JSON before sending it
Drop unused fields and whitespace to save tokens.
compact = json.dumps({k: row[k] for k in ("id", "status", "total")}, separators=(",", ":"))Common mistakes
- Estimating tokens by word count for code, JSON or non-English text.
- Sending verbose API responses into the prompt unfiltered.
- Forgetting that output tokens count toward the context window and usually cost more.
- Assuming token counts are identical across model providers.
Key takeaways
- Models read tokens, typically subword pieces from a BPE vocabulary.
- ~4 characters per token for English prose; more for code, JSON and many languages.
- Context limits, pricing and rate limits are all token-based.
- Count with the right tokenizer and trim what you send.
FAQ
Why do LLMs struggle to count letters in a word?
Because they see tokens, not individual characters, a word like "strawberry" may be a few tokens with no direct view of each letter.
Do spaces count as tokens?
Usually spaces are merged into the following word's token, but runs of whitespace or indentation can add tokens.
Are images and audio tokens too?
Multimodal models convert images and audio into tokens as well, with provider-specific counting rules.