Resources/Model training/Tokens and context

Concept

Tokens

The chunks of text a model actually reads and writes — how they are made, why counts differ between models, and why every limit and price in training is measured in them.

Chapter 23 min read5 references

What a token is

A language model never sees letters or words. Before any text reaches it, a tokenizer cuts the text into pieces taken from a fixed vocabulary and replaces each piece with its number in that vocabulary. The model reads a sequence of those numbers and writes its answer as numbers too, which the tokenizer turns back into text.

“How do I reset my password?”
How2437␣do466␣I314␣reset13259␣my616␣password9206?30
27 characters6 words≈ 7 tokens
One sentence, seven tokens. A space usually travels with the word after it, which is why “ do” and “do” are different tokens.

Vocabularies today hold from tens of thousands to a few hundred thousand entries. Frequent words get their own entry, usually with the space before them attached. Less frequent words are spelled out of smaller pieces: an unusual product name might become four or five tokens.

How tokenizers are built: byte-pair encoding

Most current tokenizers use a form of byte-pair encoding (BPE), adapted for language models by Sennrich, Haddow and Birch. The vocabulary is learned from a large sample of text before the model itself is trained:

  1. Start from the smallest units: single characters, or the 256 possible bytes.
  2. Count every pair of neighbouring units in the sample and find the most frequent pair.
  3. Merge that pair into a new unit and add it to the vocabulary.
  4. Repeat until the vocabulary reaches its target size.

The vocabulary ends up spending its entries on whatever was common in that sample. Byte-level versions start from bytes rather than characters, so any input — any language, emoji or stray symbol — can still be encoded, just less efficiently when it is unusual.

How BPE learns “ing”
  1. The sample contains “walking”, “talking” and “making” many times. At first each is split into characters: w a l k i n g.
  2. i followed by n is the most frequent pair, so it becomes one unit: w a l k in g.
  3. Now in followed by g is the most frequent pair: w a l k ing.
  4. After tens of thousands of merges a frequent word like “walking” is likely to be a single token, while a rare one like “unwalkable” is still built from several pieces.

Why the same text gives different counts

Kind of textTypical behaviour
Everyday EnglishAbout 4 characters, or ¾ of a word, per token
Numbers, IDs and datesSplit into short chunks: 2024-03-17 can take five or more tokens
Code and JSONBrackets, quotation marks and indentation all cost tokens
Other languagesOften more tokens for the same meaning; one 2023 study measured differences of up to 15 times for the same translated text
Rare names and jargonSplit into several sub-word pieces

Each model family trains its own tokenizer, so a document that is 1,000 tokens for one model might be 1,150 for another. Any count made with a different tokenizer, or estimated from the number of characters, is an approximation.

Why it matters for training

  • Cost. Training is billed per token processed, and use per input and output token.
  • Limits. The context window and the per-example training limit are counted in tokens, not in messages or words.
  • Odd weaknesses. A model sees “strawberry” as a few tokens, not ten letters, which is why counting letters or reversing a word is surprisingly hard for it.

Common mistakes

  • Treating a token as a word when estimating cost or limits.
  • Assuming a count made for one model applies to another.
  • Budgeting for non-English data with English characters-per-token ratios.
  • Forgetting that formatting — JSON, markup, long IDs — consumes tokens too.

References and further reading

External sources open in a new tab.

  1. 01Neural Machine Translation of Rare Words with Subword UnitsSennrich, Haddow & Birch, 2016 · arXiv · arxiv.orgThe paper that introduced BPE tokenization to NLP.
  2. 02Byte-Pair Encoding tokenizationHugging Face LLM Course · huggingface.coBPE training worked through step by step, with code.
  3. 03Language Model Tokenizers Introduce Unfairness Between LanguagesPetrov et al., 2023 · arXiv · arxiv.orgHow much more the same content costs in some languages.
  4. 04openai/tiktokenOpenAI · GitHub · github.comA widely used BPE tokenizer you can run to count tokens yourself.
  5. 05Understand and count tokensGemini API docs · ai.google.devAnother provider's explanation of tokens and how to count them.

Turn weeks of expert work into minutes

See how Scalata fits your team, your data, and your controls.