Tokens and embeddings: turning text into numbers

10 min read

A language model never sees letters or words directly. It sees tokens, and each token becomes a list of numbers.

Tokenization

A tokenizer splits text into chunks from a fixed vocabulary, typically 50,000 to 250,000 entries. Common words are one token; rarer words are split into pieces; spaces and punctuation are tokens too.

For example, "Tokenization is unbelievable" might become: Token | ization | is | un | believ | able. Exact splits depend on the tokenizer. Most use a method called byte-pair encoding (BPE), which starts from single characters and repeatedly merges the most frequent pairs found in training text.

Why it matters to you:

  • Pricing and limits are counted in tokens. In English, one token is roughly three-quarters of a word, so 1,000 tokens ≈ 750 words. Other languages often need more tokens for the same meaning, which makes them costlier.
  • Odd failures come from tokenization. Counting the letters in a word, reversing strings and some arithmetic are hard partly because the model sees chunks, not characters.

From tokens to vectors

Each token ID is looked up in an embedding table that turns it into a vector of several thousand numbers. These embeddings are learned during training, so tokens used in similar ways end up with similar vectors.

The model also needs to know word order, since "dog bites man" differs from "man bites dog." A positional encoding adds information about each token's position to its vector.

From here on, everything inside the model is arithmetic on these vectors. The final layer turns the last vector back into a score for every token in the vocabulary, and softmax turns those scores into probabilities for what comes next.

Check your understanding

Check 1 of 2Module 5 · Transformers and large language models

Why might a model struggle to count the letter 'r' in 'strawberry'?

Check 2 of 2Module 5 · Transformers and large language models

Roughly how many English words fit in 1,000 tokens?

Answer the checks, then mark this lesson complete
Completion and scores are saved in this browser.