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

splitmapto modelText"Unbelievable!"TokenizerBPE vocabTokensUn · believ · able · !IDs[1844, 31141, 481, 0]
Text becomes token IDs before the model sees it (IDs illustrative).

Tokens per content type

English prose (1,000 chars)≈250 tokensPython code (1,000 chars)≈330Minified JSON (1,000 chars)≈400Hindi text (1,000 chars)often much higher
Illustrative token counts per 1,000 characters; actual numbers depend on the tokenizer.

Where tokens matter

Limit or costCounted inPractical effect
Context windowInput + output tokensHow much you can send and receive in one call
Max output tokensOutput tokensLong answers can be cut off
PricePer million input / output tokensOutput is usually pricier than input
Rate limitsTokens per minuteThroughput ceiling for your app
Prompt cachingCached input tokensRepeated 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.048

Shrink 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.

More AI articles