Grokking LLM Essentials
Vote

0% completed

​

Tokens: How Text Becomes Numbers

Part 1: Text Is Cut Into Tokens

Part 2: The Vocabulary and Its IDs

Part 3: Common Words Are One Token, Rare Words Are Several

Part 4: A Toy Tokenizer

Part 5: Why Limits and Prices Are Counted in Tokens

Key Takeaways

The sentence about the chef has 10 words and 48 characters. A model that receives this sentence does not receive any of its letters. It receives 10 numbers:

976, 27005, 88244, 290, 29684, 326, 2059, 480, 6118, 945

Each number represents one piece of the sentence. This is step 1 of the five steps in The Life of a Prompt. This lesson explains what the pieces are, where the numbers come from, and why limits and prices are counted in these pieces.

Part 1: Text Is Cut Into Tokens

A model calculates only with numbers, so text must be turned into numbers before the model can use it. A separate program called a tokenizer does this work. It cuts the text into small pieces called tokens, and it replaces each token with a number called a token ID.

A token is often a whole word. It can also be a part of a word, a punctuation mark, or a single character. The diagram below shows the tokens of the sentence about the chef.

One widely used tokenizer cuts the sentence about the chef into 10 tokens and gives each token an ID
One widely used tokenizer cuts the sentence about the chef into 10 tokens and gives each token an ID

Look at the two rows of boxes. One widely used tokenizer cuts this sentence into exactly 10 tokens, one for each word, and the ID of each token is in the box below it.

One detail does not appear in the diagram. A token for a word inside a sentence includes the space before it. The fifth token is " soup" with its space, and not "soup".

Part 2: The Vocabulary and Its IDs

A tokenizer has a fixed list of every token it can produce. This list is called the vocabulary. The vocabulary of the tokenizer used in this lesson has about 200,000 tokens. Other tokenizers have other vocabularies, so the same sentence can have different IDs and a different number of tokens in another model.

Each token in the vocabulary has one ID. The ID of "The" is 976, and the ID of " chef" is 27005.

An ID identifies a token and gives no information about what the token means. The same word can also have two IDs. In the diagram, "The" at the start of the sentence is 976, and " the" in the middle is 290. The capital letter and the space make them two different pieces of text, so the vocabulary lists them as two tokens.

Part 3: Common Words Are One Token, Rare Words Are Several

A vocabulary cannot hold every word. People write in many languages, and new names and new words appear all the time.

Tokenizers handle this problem with pieces that are smaller than a word. A common word usually has a token of its own. A rare word is built from several shorter tokens, as the table shows.

TextTokensNumber of tokens
salt"salt"1
saltiness"salt" + "iness"2
minestrone"min" + "estr" + "one"3

Each word in the table is counted as it appears inside a sentence, with the space before it. The token "salt" has the ID 16345 in both of the first two rows. The word "saltiness" adds a second token, "iness", with the ID 1811.

Because short pieces can be combined, a tokenizer can cut any text, including a word that is in no dictionary. Such a word is cut into many small tokens, and sometimes into single characters.

The pieces do not follow grammar. The "one" in "minestrone" has no connection to the number one. A real tokenizer learns its vocabulary from a large amount of text, and the pieces that appear most often become tokens.

Part 4: A Toy Tokenizer

The playground below is a toy tokenizer. Its vocabulary has only 20 tokens, and their IDs are made up. To cut a word, the program takes the longest vocabulary entry that matches the start of the word. It then repeats this with the rest of the word.

The toy is a simplification. A person wrote its vocabulary by hand, and the program ignores capital letters and spaces. A real tokenizer learns a far larger vocabulary from text, keeps capital letters and spaces, and can use a different rule to cut. Run the program and look at how "saltiness" and "minestrone" are cut.

Python3
Python3

. . . .

For "saltiness", the program prints this result:

Text: saltiness Pieces: salt | iness IDs: [10, 11] Count: 2 tokens

The toy vocabulary has no entry for "saltiness", so the longest match at the start of the word is "salt", and the rest matches "iness". The last text in the output, "unsalted peppery soups", has 3 words and is cut into 7 tokens.

Now add "saltiness": 21 to VOCAB and run the program again. The word becomes one token, because the whole word is now the longest match. The way a text is cut depends on the vocabulary.

Part 5: Why Limits and Prices Are Counted in Tokens

A model reads tokens and produces tokens, so the amount of work it does depends on the number of tokens and not on the number of words. For this reason, each model has a maximum number of tokens that it can read at one time. The companies that offer models also usually charge for each token read and each token produced.

For English text, one token is about four characters, or about three quarters of a word. This is a rough estimate, and it makes a text of 750 words about 1,000 tokens.

The sentence about the chef shows how rough the estimate is. Its 48 characters suggest about 12 tokens, and the real count is 10, because every word in it is common enough to be one token. Text with many rare words or names usually needs more tokens for each word.

Key Takeaways

  • A tokenizer cuts text into tokens. A token is often a whole word, and it can also be a part of a word.
  • Every token has an ID from a fixed vocabulary. The model receives the list of IDs and never the letters.
  • Common words are one token, and rare words are several. This is how a vocabulary of about 200,000 tokens can cover any text.
  • Limits and prices are counted in tokens. In English, one token is roughly four characters, or three quarters of a word.

The 10 token IDs of the sentence hold no meaning yet. The next lesson, Embeddings: How Numbers Hold Meaning, explains how each ID is replaced by a list of numbers that holds meaning.

Reading Progress

0%


Vote for new content

On This Page

Part 1: Text Is Cut Into Tokens

Part 2: The Vocabulary and Its IDs

Part 3: Common Words Are One Token, Rare Words Are Several

Part 4: A Toy Tokenizer

Part 5: Why Limits and Prices Are Counted in Tokens

Key Takeaways