Skip to main content

From text to tokens: representing language

The conceptual path from text to model representations

Lesson objective. Explain the text → tokens → IDs → initial vectors chain, compare two tokenizers on the same input, distinguish IDs from numerical quantities, and understand why tokenizer and weights form a contract.

Lesson skeleton
  • Text context → Token + Token IDs · Trace text to IDs and explain what can change between two tokenizers.

See the path

Review this lesson

In the overview, the model predicts the next token from the prefix “What have the Romans ever done for us?” In what form does it receive that question?

A tokenizer splits text into units called tokens. A token is not necessarily a whole word: it can be a word, a word fragment, a punctuation mark, or a fragment that includes a space. Its vocabulary maps each token to an integer identifier (ID). That ID is a label local to the tokenizer’s vocabulary; it does not represent a meaningful numerical quantity. The model receives the IDs in text order.

From the prefix to token IDs

Exact prefix to copy: What have the Romans ever done for us?. The GPT-2 tokenizer produces nine tokens. The last is the question mark ("?"), with ID 30.

The shorter text hello world lets you follow the entire sequence. The quotes mark the contents of each token.

Token GPT-2 ID
"hello" 31373
" world" 995

The space before world is part of the second token. Across the two examples, spaces and punctuation affect the split. The reproducible Python trace checks the IDs, prints the full decomposition of both prefixes, and illustrates table lookup using synthetic vectors.

A different tokenizer can give the same text different tokens and IDs. Even when two tokenizers recognize the same fragment, they can assign it different numbers. Use the tokenizer paired with the model weights so that each ID selects the intended embedding row.

Compare two encodings of the exact same input, 2 + 2 = 4, using tiktoken==0.14.0. The symbol ␠ makes spaces that belong to fragments visible:

Encoding Token fragments IDs Total
gpt2 2 · ␠+ · ␠2 · ␠= · ␠4 17, 1343, 362, 796, 604 5
cl100k_base 2 · ␠+ · ␠ · 2 · ␠= · ␠ · 4 17, 489, 220, 17, 284, 220, 19 7

Both sequences decode to 2 + 2 = 4. IDs are local to each vocabulary, so their numerical values are not comparable across encodings. This contrast demonstrates an effect on representation length, not a difference in linguistic quality. The reproducible trace checks the fragments, IDs, and full decode.

Why do two tokenizers produce different sequences?

The same text can produce different sequences because pre-tokenization rules and learned vocabularies differ. The BPE, WordPiece, and unigram mechanisms that explain these differences are covered in the tokenization-strategy deep dive. For the core learning path, remember that each ID sequence must be interpreted with the tokenizer that produced it.

A thought experiment: what does the model actually receive?

Return to the prefix “What have the Romans ever done for us?”. The running narrative then continues with: “They built roads between distant towns.” Before discussing meaning or attention, follow one occurrence through three transformations:

  1. Text is split. The tokenizer applies its rules and produces an ordered sequence of tokens. Boundaries do not necessarily match visible words.
  2. Tokens become IDs. Each token is replaced by its index in the vocabulary. This index retrieves an entry; it is neither a measurement nor a semantic representation.
  3. IDs select vectors. For each ID, the network reads one row of the embedding matrix. With fixed parameters, two occurrences of the same ID therefore select the same initial vector.

The distinction matters when we change only one thing:

What changes What may change What does not automatically change
The tokenizer Token boundaries and their IDs The source text
Vocabulary numbering, with the embedding rows permuted in the same way The displayed ID numbers The vectors actually selected
The context around an occurrence Later states produced by the Transformer The initial lookup for a fixed ID

Key point: the tokenizer defines the symbolic interface between text and model; the embedding table then maps IDs to vectors. Contextualization belongs to later network computations, not to the tokenizer.

To verify the chain with concrete values, use the reproducible tokenizer and lookup trace, then the exercise that follows an ID to its embedding row.

The tokenizer is part of the model contract

An ID can only be interpreted with the vocabulary that assigned it. If the vocabulary contains \(V\) tokens, the embedding matrix generally has \(V\) rows and \(d\) columns: ID \(i\) selects row \(E_i\). In an autoregressive language model, the output projection also produces \(V\) logits; a logit index refers to a token in the same vocabulary. The interface therefore connects the network’s input lookup to its output classes.

A checkpoint is more than its weight tensors: it must be accompanied by the corresponding vocabulary and tokenizer configuration. The contract also includes the rules and version that determine encoding and text reconstruction: normalization, pre-tokenization, special tokens, and decoding. The same integer can refer to a different fragment in another tokenizer. Switching tokenizers without adapting vocabulary-indexed parameters would make the model read the wrong vectors and associate its logits with the wrong tokens. This is why the weights and exact tokenizer files must be kept together. The details of these components belong in the dedicated deep dives.

Some tokenizers also add special tokens (for example, an end marker or a separator). These are reserved IDs, not ordinary visible words; they can occupy positions in the sequence. Their roles and spellings depend on the model. The conventions are detailed in Tokenization strategies.

TipToken, ID, and embedding

The token is the unit produced by the split. The ID is its reference number in the vocabulary, like a catalogue number. The embedding is the learned vector that the model reads from the corresponding row. A table with \(V\) rows holds a vector with \(d\) components per row. Two occurrences of the same ID select the same initial vector with fixed parameters. The Transformer can then represent them differently depending on context.

1. Source text 2. Tokens 3. IDs 4. Embeddings 5. Position 6. Contextual states

Why the split matters

  • Capacity. How much text fits in a given context window (for example 8,000 tokens) depends on the split. The more tokens a passage uses, the less room there is for history, instructions, retrieved documents, tool descriptions, or the answer. How the budget is counted depends on the model and API. Context engineering.
  • Compute. A longer sequence adds positions to process. The effect on time and cost depends on the architecture, hardware, and service. Inference complexity.
  • Languages. Two translations of the same passage can use different numbers of tokens. A fixed context budget or, depending on the protocol, a fixed training budget can therefore cover different amounts of text across languages. Measure the efficiency of the split by language and text type, then evaluate quality separately on the target tasks. Tokenizer design and evaluation.

An order-of-magnitude example. Suppose the same passage uses 1,000 tokens with one tokenizer and 1,200 with another. In an 8,192-token context window, that consumes about 12.2% and 14.6% of the budget, respectively. For full dense attention, the number of position pairs grows as \(T^2\): a sequence that is 20% longer implies about 44% more pairs (\(1.2^2=1.44\)). This is not a direct runtime prediction: attention kernels, caching, batch padding, and architecture all affect actual cost. Token count is still an important operational signal.

Explain what can change when the text stays fixed. Check your understanding by following IDs to the rows of the embedding table.

Do not confuse encoding a prompt with preparing a tokenizer. At inference time, the tokenizer has already been selected and fixed with the model weights: it converts the prompt into IDs. When building a model, the corpus is prepared first, then the tokenizer is trained or fitted before this interface is frozen. That preparation is not repeated for every request; it belongs to the Build path (prepare a corpus).

In the next lesson, return to the last token of the prefix. With the GPT-2 tokenizer and its corresponding table, ID 30 tells the model which row to read. What does that row contain, and how does the model use it?

Language · Français · English

Practice

The questions and exercises for this lesson are collected on its dedicated practice page.

Further exploration

Supplementary resources Revisit the running example, the full pipeline, and the conceptual boundaries.

Picking up the path to representations

The next lessons examine separately the objects this page has just distinguished: embedding lookup, positional mechanisms, and then contextual representations. These are not tokenizer operations.

Further reading

To choose a reading route by question—mechanisms, Unicode representation, or evaluation—use the tokenization and tokenizers domain map. The specialist pages below remain optional.