Multilingual Tokenization and Vocabulary Allocation › The cost of a single token
Language AI HandbookAll chapters The English word cat occupies three ASCII bytes. Its Hindi equivalent बिल्ली contains six Unicode code points. Each is a three-byte Devanagari code point in UTF-8, for eighteen bytes total.
Measure the two encodings directly to see where the difference comes from:
The six units in the Hindi string are code points, including attached signs. They are distinct from user-perceived characters. Python's len(text) counts code points, and len(text.encode("utf-8")) counts the bytes in the UTF-8 encoding. To find the average number of bytes per code point, divide the byte count by the code-point count. To compare how compactly tokenizers encode the same losslessly represented text, use bytes per token.
With one token per code point for both strings, the token ratio is . With one token per byte for both strings, it is . A mixed tokenizer could keep cat as one piece and encode the Hindi string as eighteen bytes. A tokenizer without a way to represent the Hindi code points can instead collapse a run into an unknown ID, which makes its token count look small while losing text.
Fertility is the average number of subword tokens the tokenizer produces per word:
Fertility is a property of a tokenizer applied to a text sample under a specified word segmentation convention. Rust et al. (2021) use subword fertility in their analysis of multilingual tokenizers.
English and several other languages often mark word boundaries with spaces. Chinese, Japanese, and Thai generally do not use spaces between every word. A whitespace split can count a whole Thai sentence as one word. Its resulting fertility would compare incompatible units with an English whitespace count.
Use an explicit language-specific word segmenter when the word is the unit of interest. If the question is how densely the tokenizer encodes text, count UTF-8 bytes instead. This avoids word segmentation and keeps the measurement sensitive to the script and sample.
where:
- : bytes per token for language
- : the total number of bytes the text occupies in UTF-8 encoding
- : the total number of subword tokens the tokenizer produces on that text
A higher means fewer tokens for the same byte count. It can be large for text with many long, learned pieces. Pure byte encoding has when there are no additional tokens. Code-point encoding uses one token per code point and each scalar occupies at least one byte. A value below one needs another explanation, such as extra tokens or preprocessing that changes the text being counted.
The comparison still depends on the material. The UTF-8 width of code points varies, and a translated message can contain more bytes than the original. The metric measures average encoded bytes per position for the sample. It does not measure how predictable the text is.
A high bytes-per-token value can coexist with a high premium. Imagine a translation that is twice the source's byte length, encoded at eight bytes per token while the source is encoded at five. The translation compresses more bytes into each position and still occupies times as many positions. These illustrative numbers explain why neither column in a tokenizer comparison substitutes for the other.
The tokenization premium of language relative to English is the ratio of token counts on the same content, which you obtain by using a parallel corpus where each sentence appears translated into every language:
where:
- : the tokenization premium of language relative to English
- : the token count produced by the tokenizer for a given string
- : the -th sentence translated into language
- : the -th sentence in English
- : the sum over all parallel sentences in the corpus
means the translations occupy three times as many token positions in aggregate. The input charge is three times as high for these tokens when the input rate is fixed and identical.
Holding content approximately constant with translations makes the premium relevant to a user-facing workload. The ratio of total token counts includes both translation length and compression. In fact, the premium equals the ratio of the two texts' UTF-8 byte totals times the English bytes-per-token divided by the target language's bytes-per-token. Keep the byte counts and the compression measurements together if you want to see how the two factors contributed. This chapter uses a ratio of totals across pairs. Each of the three diagnostics requires a positive denominator. Petrov et al. (2023) define the premium on sentence pairs and evaluate tokenizers on a much larger parallel corpus.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!