Concepts / Token IDs

Token IDs

Token embeddings are the last preparation step before LLM training: token IDs are converted into embedding vectors.

  • Programming
Interactive lab

Try it: Text Vectorization: Tokens to Vectors

The text-to-tensor pipeline: standardize, split into tokens, build a frequency-ranked vocabulary with padding and [UNK], map tokens to integer ids, pad or truncate, then one-hot or multi-hot encode and look the ids up in an embedding table.

How it works

  1. Standardize: lowercase and delete punctuation.
  2. Split on whitespace into tokens.
  3. Build the vocabulary: 0 = padding, 1 = [UNK], then words by frequency (ties: first appearance), capped at max tokens.
  4. Map each token to its id (1 if it did not make the vocabulary), then pad with 0 or truncate to the sequence length.
  5. Encode: one-hot rows per position or one multi-hot row per text; embed: id k selects row k of the embedding table.

Default run (18 steps): Raw text (59 characters): "The movie was great. The plot was great, the acting wasn't!". A model cannot read strings, so it is turned into numbers step by step. … Embedding lookup: id k selects row k of a fixed 8 x 3 table, giving a (8, 3) dense matrix instead of (8, 8) sparse one-hots. Row 0 (padding) is looked up too; a trained Embedding layer would learn these rows.

Simplified: Bounded ASCII text (up to 200 characters, 40 tokens), whitespace splitting only, and a small FIXED embedding table (a real Embedding layer learns its rows). The multi-hot vector keeps the padding slot (always 0) so its positions line up with the ids.

Educational simulation

Loading the simulation…

Why a Model Needs Numbers Instead of Words

A language model does its arithmetic on numbers, not on words. Before any text can reach the model, each piece of it has to be given a numeric stand-in. Suppose you have already split a sentence into tokens and assigned each token an integer. You now have a list of numbers that stands in for the sentence. The question this article answers is what happens next: what does the model receive as its input once those integers exist?

The short answer is that the integers are converted into embedding vectors, and those vectors are what the LLM works with during training. Keep this sequence in mind as we go: text comes first, then tokens, then token IDs, and finally embedding vectors. Each stage changes the form of the data while keeping track of the same content. The last stage is the one that gives the model numeric input it can adjust while it learns.

What do you think happens?

If a sentence is turned into integers, and the integers are then turned into vectors, which stage does the model actually learn from?

  • The raw text

  • The tokens

  • The token IDs

  • The embedding vectors

Reveal answer
Answer:

The embedding vectors

The embedding vectors are the numeric input the LLM works with during training. The earlier stages prepare the data, and the last stage is the one the model adjusts while learning.

The Three Preparation Stages

The preparation path has three transitions. The first tokenizes the text, cutting it into individual tokens. The second converts each token into a token ID, which is a single integer that identifies that token. The third converts each token ID into an embedding vector, which is the representation the model receives as input. The path runs from input text to tokenized text, then to token IDs, then to embedding vectors.

Input textthe cat sat on the matTokensthe, cat, sat, on, the, matToken IDs0, 1, 2, 3, 0, 4Embedding vectorsone vector per IDtokenizelook up in vocabularyconvert to embedding
What representation does the text take after each preparation stage, and in what order do the stages happen?

Each stage keeps the same sentence in a different form. Nothing in the content is lost between stages; the data is simply expressed in a more useful shape. The left side of this pipeline is the preparation the article describes. Once the embedding vectors exist, the prepared input moves on into the model itself, which is the only part of the overall path that involves the model.

Tokenizing Text Before Any Numbers Are Assigned

Two ingredients make the conversion possible. The first is a way of cutting text into smaller units called tokens. The second is a lookup structure, the vocabulary, that assigns each distinct token a unique integer label. Once both are in place, converting new text is mostly a matter of looking things up.

Tokenization is the first step whenever a new text sample arrives. The sample is cut into tokens in exactly the same manner that was used for the training set, so that its pieces can be compared with the pieces the vocabulary already knows about. A token is therefore not a fixed property of language in general. It is whatever unit the tokenizer produces from the text in front of it.

The examples in this article use a deliberately simple rule: split on spaces, and keep only plain lowercase words. Real tokenizers can be considerably more sophisticated, but the mapping step that follows works the same way regardless of how the tokens were cut. Only after the cutting is finished does any number get assigned.

Building a Vocabulary from the Training Set

Definition

A vocabulary is a dictionary that associates each token with a unique integer label. It is built from the entire training set and can be applied to new text. Converting text to token IDs means looking up each token in the vocabulary and keeping the original order of the tokens.

Once the vocabulary exists, it is not tied to the original training set. It can be applied to the training set itself and also to new text samples, which is what makes it useful for the conversion task. Each word is paired with one integer, and no integer is shared by two words. Reading a vocabulary from left to right, the token is the key and the integer is the value.

Token (key)Token ID (value)
the0
cat1
sat2
on3
mat4

A five-entry vocabulary (an invented example used in the source material)

Notice that the vocabulary stores each token only once, even though a sentence might use the same word several times. The word 'the' appears twice in the sentence used below, but it has one entry and one ID in the vocabulary.

Tracing a New Sentence Through the Vocabulary

Worked example

Producing token IDs for the sentence 'the cat sat on the mat'

Using the five-entry vocabulary, find the token ID for every token in the sentence 'the cat sat on the mat', keeping the original order.

  1. 1
    Tokenize

    Split the sentence on spaces, giving the tokens the, cat, sat, on, the, mat. Each piece is a plain lowercase word, so nothing needs to be removed.

  2. 2
    Look up the first token

    The first token is 'the'. The vocabulary maps 'the' to 0, so the first ID is 0.

  3. 3
    Look up the middle tokens

    'cat' maps to 1, 'sat' maps to 2, and 'on' maps to 3. These are written in the same order as the tokens.

  4. 4
    Look up the repeated token

    The fifth token is 'the' again. The lookup is the same every time, so it again gives 0.

  5. 5
    Look up the last token

    The final token 'mat' maps to 4.

  6. 6
    Assemble the sequence

    Keeping the original order gives the token IDs 0, 1, 2, 3, 0, 4.

Result

The sentence 'the cat sat on the mat' becomes the token IDs 0, 1, 2, 3, 0, 4.

the0cat1sat2on3mat4
When a new sentence is split into tokens, how does each token look up its ID in the existing vocabulary?

The worked example shows a property worth remembering: identical tokens always receive identical IDs, because the lookup is the same every time. This is what allows the numeric sequence to preserve which words recur. The second 'the' in the sentence produces the same 0 as the first, so a reader of the ID sequence can see the repetition.

Token IDs Versus Embedding Vectors

The last transition is the least obvious. A token ID is a single integer that identifies a token. An embedding vector is a representation of that token in a form the model can use for learning. The conversion keeps the identity of each token while changing its form, so each ID in the sequence is paired with its own embedding vector. One token ID leads to one embedding vector.

ID 0embedding vector for 'the'ID 1embedding vector for 'cat'ID 3embedding vector for 'on'ID 4embedding vector for 'mat'
How does a single token ID select one specific embedding vector, and why are the ID and the vector different kinds of representation?

The diagram is a pairing, not a calculation. It tells you which vector belongs to which ID. The internal arithmetic of this lookup is not described in the material, so the idea to hold onto is the correspondence itself. One way to picture it, offered here as a teaching aid rather than as a claim about the implementation, is a table with one row per token ID, where the ID tells you which row to read.

PropertyToken IDEmbedding vector
What it isA single integerA representation of the token for learning
Where it comes fromOutput of tokenization and vocabulary lookupOutput of the embedding conversion
Role for the modelIdentifies which token is presentThe input the model learns from
Changes during trainingNo, the ID is a fixed labelYes, the values are optimized

Token IDs and embedding vectors both represent the same tokens, but play different roles

Why Embedding Weights Start Random

The embedding vectors are not handed to the model already meaningful. The weights that produce them start with random values. These weights are then optimized during LLM training, so the values that begin random are gradually adjusted as the model learns from data.

This is why the last preparation stage matters so much. The integer IDs are fixed labels, but the embedding vectors are the part the model can change. The conversion gives the model a numeric input whose values it can adjust during learning, which is the reason the vectors, and not the IDs, are what the LLM works with during training.

Turning Output IDs Back into Text

The conversion so far runs in one direction, from text toward numbers. A language model produces numbers as its output, and those numbers have to be turned back into text before a person can read them. A model that cannot be read back into text is of little use, so the reverse journey is part of the same system.

Definition

An inverse vocabulary maps token IDs back to text tokens. It is simply the vocabulary read the other way: given a token ID, it returns the token that the ID stands for.

0the1cat2sat3on4mat
How does the model's numeric output get mapped back from token IDs to readable text tokens?

What do you think happens?

Using the inverse vocabulary above, which word does the token ID 3 return?

Reveal answer
Answer:

on

The inverse vocabulary is the example vocabulary read backwards, and 3 maps to 'on'.

Applying this lookup to every ID in an output sequence, in order, reconstructs the text. Running the earlier sentence backwards takes the sequence 0, 1, 2, 3, 0, 4 back to the words the, cat, sat, on, the, mat. The two structures are mirror images, and each answers a different question. The vocabulary answers 'which number stands for this token?' The inverse vocabulary answers 'which token does this number stand for?' A system that converts text in both directions needs both.

StructureDirectionQuestion it answers
VocabularyToken to IDWhich number stands for this token?
Inverse vocabularyID to tokenWhich token does this number stand for?

The vocabulary and inverse vocabulary are mirror images

Mistakes Beginners Make with Token IDs

  • Common mistake

    Assuming the token ID is the same thing as the embedding vector

    What it looks like

    Treating the integer 4 as if it were the representation the model learns from.

    Why it fails

    The ID is a single integer label, while the embedding vector is the representation the model receives and adjusts during training.

    Do this instead

    Keep the two apart: the ID identifies the token, and the embedding vector is the input the model works with.

  • Common mistake

    Giving a repeated word a new ID each time it appears

    What it looks like

    Assigning 0 to the first 'the' in the sentence and 5 to the second 'the'.

    Why it fails

    The vocabulary stores each token once, and the lookup is the same every time, so identical tokens always receive identical IDs.

    Do this instead

    Look up the word in the existing vocabulary and reuse its ID, giving 0 for every 'the'.

  • Common mistake

    Assigning IDs before the text has been tokenized

    What it looks like

    Numbering whole sentences or raw character strings instead of individual tokens.

    Why it fails

    Tokenization comes first, and the IDs are looked up for each token that the tokenizer produces.

    Do this instead

    Cut the text into tokens with the same rule used for the training set, then look up each token.

  • Common mistake

    Reversing the lookup direction when decoding model output

    What it looks like

    Looking up the word 'on' to find its ID when the model has produced the number 3 and needs readable text.

    Why it fails

    Model output is numeric, so turning it into readable text requires the inverse vocabulary, which maps IDs to tokens.

    Do this instead

    Use the inverse vocabulary to map each output ID back to its token, in order.

Practice Tracing a New Sentence

EASY

Using the five-entry vocabulary (the=0, cat=1, sat=2, on=3, mat=4), tokenize the sentence 'the cat sat on the cat' and write its token IDs in order. Then use the inverse vocabulary to turn your ID sequence back into words and check that you recover the original sentence.

Hints
  • Split the sentence on spaces first, then look up each token separately.
  • The word 'cat' appears twice, so its ID should appear twice in your answer.
  • Your ID sequence should be 0, 1, 2, 3, 0, 1.

Key Takeaways

  • Preparing text for an LLM has three stages: tokenizing the text, converting each token to a token ID, and converting each token ID to an embedding vector.
  • A vocabulary assigns each distinct token one unique integer, and new text is converted by tokenizing it the same way and looking up each token in that vocabulary in order.
  • A token ID is a fixed integer label, while an embedding vector is the representation the model receives, and its weights start random and are optimized during training.
  • An inverse vocabulary maps token IDs back to text tokens, which is needed to turn a model's numeric outputs into readable text.

Related concepts

More concepts starting with T