Skip to content
ImagineWorld

How Words Become Numbers

node_idnode-book_llm-systems-engineering_module-02-how-words-become-numbers
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num2
chapter_titleHow Words Become Numbers
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Every large language model begins its work the same way, by turning written text into numbers. This conversion is the first operation in any pipeline that handles natural language with neural networks, and it happens long before any attention mechanism or weight matrix becomes relevant. Understanding how text becomes a sequence of integers helps explain many aspects of model behavior, including some of the stranger failures that emerge in deployed systems. This module examines that conversion in detail and explains the design choices that govern it. The discussion moves from the basic requirement that neural networks consume numerical input to the practical consequences that tokenization choices carry for cost and capability. By the end, you should be able to look at any modern tokenizer and understand both what it does and why its design matters so much.

Neural networks, including the transformers that power modern language models, are fundamentally mathematical objects. Every operation inside them, from matrix multiplication to gradient computation, requires numerical inputs. A character on a page or a word in a sentence carries no inherent numerical value that a neural network can consume directly. The text must therefore be converted into numbers before any learning or generation can take place, and the form of that conversion turns out to be a consequential design decision in its own right.

The way text is converted shapes every downstream computation the model performs. If two closely related strings receive entirely different numerical representations, the model has to learn that relationship purely from data. If the conversion process splits a common word into awkward fragments, the model has to work harder to reassemble its meaning across multiple positions. The conversion stage therefore acts as a gatekeeper, deciding which patterns will be easy for the model to learn and which patterns will require extra effort.

Another consideration is efficiency. A language model processes its input one position at a time within a fixed context window. The number of positions consumed by a given piece of text depends entirely on how that text is sliced into units. Shorter sequences are cheaper to process and free up context for additional content, while longer sequences strain memory and slow generation. The economics of inference and the practical limits on what a model can read in a single pass all flow from this initial choice of how to segment text into discrete pieces.

A token is the smallest unit of text that a language model treats as a single, indivisible symbol. Early approaches segmented text at the word level, treating each space-separated string as one token. This worked for small, controlled vocabularies, yet it failed badly on real-world data because any word the system had never seen before became impossible to represent. Character-level approaches solved that problem by reducing text to its smallest components, although they produced sequences so long that learning meaningful relationships across them became difficult and computationally expensive.

Modern systems strike a balance using subword tokenization. The core idea, introduced for neural machine translation by Sennrich et al. (2016), is to learn a vocabulary of common fragments by repeatedly merging the most frequent adjacent pairs of characters in a corpus. Frequent words such as “the” or “model” end up represented as single tokens, while rare or invented words are broken into smaller pieces that the system has seen before. The word “tokenization” might appear as a single unit in one vocabulary or be split into two recognizable fragments in another, depending on what the corpus contained when the tokenizer was trained.

flowchart TD
    R["&quot;The chemist said&quot;<br/>Raw input"] --> TK[Tokenizer]
    TK --> W[Word-level]
    TK --> S["Subword-level (BPE)"]
    W --> W1[The] & W2[chemist] & W3[said]
    S --> S1[The] & S2[chem] & S3[ist] & S4[said]
    W3 ~~~ WN[/3 Tokens/]
    S4 ~~~ SN[/4 Tokens/]

Figure 1-4. Word-Level vs. Subword Tokenization

Several algorithms exist for building these vocabularies. Byte Pair Encoding remains the most widely used, although unigram language modeling has been shown to recover units that align more closely with the morphological structure of words (Bostrom & Durrett, 2020). Each algorithm produces a fixed inventory of allowable fragments that together can represent any input text without ever encountering a truly unknown symbol. The result is a system flexible enough to handle technical terminology and typographical errors while keeping sequence lengths manageable for practical training and inference.

┌───────────────────────────────────────────────────────────┐
│ Step 1: Split initial corpus into characters │
└───────────────────────────────────────────────────────────┘
┌─────────────────────────┐ ┌──────────────────────────────┐
│ Corpus │ │ Vocabulary │
│ low_ x5 │ │ 11 base symbols: │
│ lower_ x2 │ │ d, e, i, l, n, o, │
│ newest_ x6 │ │ r, s, t, w, _ │
│ widest_ x3 │ │ │
└─────────────────────────┘ └──────────────────────────────┘
↓
┌───────────────────────────────────────────────────────────┐
│ Step 2: Count pairs and merge most frequent │
└───────────────────────────────────────────────────────────┘
┌─────────────────────────┐ ┌──────────────────────────────┐
│ Pair counts │ │ Merge rule │
│ (e, s) → 9 ← winner │ │ rule #1: e + s → es │
│ (s, t) → 9 │ └──────────────────────────────┘
│ (l, o) → 7 │ ┌──────────────────────────────┐
│ │ │ vocab += (es) → 12 │
└─────────────────────────┘ └──────────────────────────────┘
↓
┌───────────────────────────────────────────────────────────┐
│ Step 3: recount, merge again │
└───────────────────────────────────────────────────────────┘

Figure 1-5. Byte Pair Encoding Vocabulary Construction

Once a tokenizer has been trained, it holds a fixed list of every fragment it knows. This list is the vocabulary, and its size is one of the principal design choices in any language model. Vocabularies in modern systems typically range from around 30 thousand entries for smaller models up to 100 thousand or more for larger ones. Each entry in the vocabulary is assigned a unique integer identifier, and that integer is the actual input the model receives. The tokenizer’s job during inference is straightforward in principle: take an input string, break it into the longest matching fragments from its vocabulary, and replace each fragment with its corresponding integer ID.

The mapping is fully deterministic and reversible. Given the same vocabulary and the same input, a tokenizer always produces the same sequence of integers, and given a sequence of integers, the same fragments can be looked up and concatenated to recover the original text. This reversibility matters because it allows the model’s output, itself a sequence of predicted token IDs, to be converted back into readable language. Without a clean round trip between text and integers, neither training nor generation would be feasible in any practical sense.

Vocabulary size carries a real trade-off. A larger vocabulary captures more whole words and common phrases as single tokens, which shortens sequences and improves efficiency at inference time. It also inflates the size of the embedding matrix, since every entry in the vocabulary requires its own row of learnable parameters. A smaller vocabulary keeps the parameter count manageable but produces longer sequences, since more text has to be represented through fragments. Where a project lands on this spectrum depends on the languages it must support and the compute available for training and serving the resulting model.

Tokenization choices have consequences that surface throughout a model’s behavior, sometimes in surprising places. One well-studied example involves arithmetic. Singh and Strouse (2024) showed that the way numbers are split into tokens substantially affects how accurately frontier models perform addition. When digits are grouped from right to left, alignment between operands and answers is preserved, and accuracy improves. When the same numbers are tokenized from left to right, accuracy can drop by up to 20 percent, with errors that follow stereotyped, systematic patterns. This finding illustrates how a decision made before any learning takes place can constrain what a fully trained model is later able to do reliably.

Similar effects appear across languages. Petrov et al. (2023) examined seventeen tokenizers and found that the same text translated into different languages could produce sequences differing in length by a factor of up to fifteen. Languages whose scripts and morphology are well represented in the training corpus receive efficient encodings, while underrepresented languages are broken into many small fragments. These differences carry direct consequences for the operational cost of using a model and for the amount of content that fits inside a fixed context window, raising fairness concerns for users of less-resourced languages.

The lesson from these examples is that tokenization is best understood as a soft constraint on what a model can learn. It influences the cost of acquiring any given capability, even when it does not strictly prevent that capability from being acquired. A model whose tokenizer fragments domain terminology into many pieces will need more training data and more parameters to handle that domain well. A model whose tokenizer treats numbers haphazardly will struggle with quantitative reasoning unless its training data compensates heavily. Practitioners who treat the tokenizer as an afterthought often discover its influence only after the model has been trained, at which point the cost of changing it has become prohibitive. Treating tokenization as a deliberate engineering decision, informed by the languages and domains the model is expected to serve, is among the most consequential actions a development team can take before any training run begins.