Special Context Tokens
Tokenization cuts text into individual tokens, and a new text sample is cut in the same way before any numbers are assigned.
Why Text Must Become Numbers First
A language model does its arithmetic on numbers, not on words. Before any text can reach the model, each piece of it needs a numeric stand-in. Converting text into that numeric form is a two-part job. The first part is cutting text into smaller units called tokens. The second part is looking each token up in a structure called a vocabulary, which gives every distinct token a unique integer label. Once both are in place, converting new text is mostly a matter of looking things up.
This article follows that translation in both directions. It starts with text that is new to the system and expresses it as a sequence of token IDs. It then follows the reverse journey, turning numeric output back into readable words, because a model whose output cannot be read back into text is of little use. Along the way it introduces two special context tokens, <|unk|> and <|endoftext|>, that solve problems ordinary vocabulary entries cannot solve.
What do you think happens?
A new text sample contains the word 'dog', and 'dog' was never part of the vocabulary. What does the tokenizer emit in its place?
Reveal answer
An <|unk|> token takes the word's place in the token sequence.
The tokenizer is modified so that a word not in the vocabulary is replaced by an <|unk|> token, and tokenization continues rather than stopping or discarding the text.
Tracing a Sample Through the Vocabulary
Tokenization is the first step whenever a new text sample arrives. The sample is cut into tokens in exactly the same manner that was applied to the training set, so its pieces can be compared with the pieces the vocabulary already knows. 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 lookup step works the same way regardless.
A vocabulary is a dictionary that associates each token with a unique integer label. Each token is the key and its integer is the value. No integer is shared by two words, and the vocabulary stores each token only once, even if a sentence uses the same word several times. The vocabulary is built from the entire training set, but once it exists it is not tied to that original set. It can be applied to the training set itself and also to new text samples.
[0, 1, 2]- The dictionary is the vocabulary: each token maps to exactly one integer.
split()applies the simple space-splitting rule to the new sample.- The list comprehension looks up each token in order and keeps the original sequence order.
Converting 'the cat sat' to token IDs
Using the vocabulary the=0, cat=1, sat=2, on=3, mat=4, convert the new sample 'the cat sat' into token IDs.
- 1Tokenize
Apply the same space-splitting rule to the sample, giving the tokens the, cat, sat in their original order.
- 2Look up 'the'
The vocabulary maps the to 0, so the first ID is 0.
- 3Look up 'cat'
The vocabulary maps cat to 1, so the second ID is 1.
- 4Look up 'sat'
The vocabulary maps sat to 2, so the third ID is 2. Order is preserved throughout.
The token ID sequence is 0, 1, 2.
Reading Numbers Back into Words
The conversion so far runs from text toward numbers. A language model, however, produces numbers as its output, and those numbers must be turned back into text before a person can read them. This is the job of the inverse vocabulary. It is the same vocabulary read the other way: given a token ID, it returns the token that ID stands for. Applying this lookup to every ID in an output sequence, in order, reconstructs the text.
| Structure | Direction | Question it answers | Example |
|---|---|---|---|
| Vocabulary | Token to ID | Which number stands for this token? | the maps to 0 |
| Inverse vocabulary | ID to token | Which token does this number stand for? | 3 maps to on |
The two structures are mirror images, and a system that converts text in both directions needs both.
Running an output sequence backward
A model outputs the ID sequence 0, 1, 2, 3, 0, 4. Using the inverse vocabulary, recover the text.
- 1Read ID 0
The inverse vocabulary maps 0 to the.
- 2Read ID 1
ID 1 maps to cat.
- 3Read ID 2
ID 2 maps to sat.
- 4Read ID 3
ID 3 maps to on.
- 5Read ID 0 again
The repeated ID 0 maps to the again, showing that the same ID always returns the same token.
- 6Read ID 4
ID 4 maps to mat.
The text is the cat sat on the mat.
Why the Vocabulary Needs Special Entries
Every tokenizer works from a fixed vocabulary. Real text is open-ended, though, and sooner or later a tokenizer meets a word that was never part of the material it was built from. A new product name, a rare dialect form, or a misspelling can all appear at the moment of use. To handle such cases, a vocabulary can be extended with special context tokens. A special context token is an additional entry that does not come from the text itself. Its purpose is to carry a meaning that ordinary words cannot express, such as 'this word was unknown' or 'a new document starts here'.
Adding a special entry works the same way as adding any other entry: the new token is given its own integer ID, placed alongside the existing ones, so the tokenizer can emit it like any other token. The source's illustration assigns the unknown-word token the ID 783 and the document-boundary token the ID 784. Those specific numbers depend on how many entries the existing vocabulary already holds, so the lesson to keep is that once added, special tokens are ordinary vocabulary members.
| Entry | Comes from | Meaning it carries | Example ID in source illustration |
|---|---|---|---|
| the | Training text | An ordinary word | 0 |
| <|unk|> | Added special entry | This word was not in the vocabulary | 783 |
| <|endoftext|> | Added special entry | A new unrelated text source begins here | 784 |
Ordinary words and special entries sit side by side in the vocabulary, each with its own ID.
When a Word Is Missing: The Unknown Token
The first special token, <|unk|>, is the tokenizer's fallback for unfamiliar words. The tokenizer is modified so that when it encounters a word that is not part of the vocabulary, it emits an <|unk|> token instead. The tokenizer does not need to stop or discard the text at that point. It substitutes the marker and keeps going, so tokenization can continue through the rest of the sample.
The marker occupies one position in the token sequence. Even though the original word cannot be recovered from the token itself, the model still receives a signal that something was there. This is the core trade-off: the unknown token preserves the position and the fact of an unfamiliar word, but not its spelling.
Tokenizing a sample containing an unknown word
Using the vocabulary the=0, cat=1, sat=2, on=3, mat=4 and the unknown-word token <|unk|> with ID 783, tokenize the sample 'the dog sat'.
- 1Tokenize
Split the sample into the, dog, sat.
- 2Look up 'the'
the is in the vocabulary, so it emits 0.
- 3Look up 'dog'
dog is not in the vocabulary, so the tokenizer emits the <|unk|> token instead, with ID 783. It does not stop.
- 4Look up 'sat'
sat is in the vocabulary, so it emits 2. Tokenization finishes the sample.
The token ID sequence is 0, 783, 2.
When the unknown token is converted back through the inverse vocabulary, the output reads <|unk|> rather than the original misspelled or new word. The original word is not recoverable from the ID alone.
Marking Where One Document Ends
The second special token, <|endoftext|>, solves a different problem. When a GPT-like language model is trained on many independent documents or books, those texts are usually concatenated into one long stream of tokens. Without a marker, the model would see one sentence flow straight into an unrelated one. The common practice is to insert an <|endoftext|> token before each document that follows a previous text source. The token tells the model that the surrounding material, though joined together for training, does not belong to one continuous narrative.
The timeline shows the arrangement. Four independent passages, covering a sports result, a fairy tale, a financial report, and a romantic moment, are placed in sequence. Each passage after the first is preceded by an <|endoftext|> token, producing a single concatenated stream. The first passage has no marker in front of it. The same token plays a dual role: it closes one document and opens the next, so the boundary is visible from either side.
Preparing a Mixed Batch of Documents
Putting the pieces together, a set of independent documents becomes one prepared training input through a sequence of steps. Each document is tokenized using the same cutting rule, each token is looked up in the vocabulary, the separator token is inserted before every document that follows another text source, and the results are concatenated into one stream of token IDs.
Preparing two short documents for training
Two independent documents, 'the cat sat' and 'the mat', are to be prepared as one training stream. Using the vocabulary the=0, cat=1, sat=2, on=3, mat=4, and the separator <|endoftext|> with ID 784, produce the token ID stream.
- 1Tokenize and look up document one
'the cat sat' becomes 0, 1, 2.
- 2Tokenize and look up document two
'the mat' becomes 0, 4.
- 3Insert separator
Document two follows another text source, so an <|endoftext|> token (784) is placed before it. The first document gets no leading marker.
- 4Concatenate
Join the pieces in order into one stream.
The training stream is 0, 1, 2, 784, 0, 4.
Mistakes Beginners Make with Special Tokens
Common mistake Assuming an unknown word is dropped from the sequence
What it looks likeTokenizing 'the dog sat' and expecting only two IDs.
Why it failsThe tokenizer emits an <|unk|> token in place of the missing word and keeps going, so the position is preserved.
Do this insteadExpect a placeholder ID in the unknown word's position, here giving 0, 783, 2.
Common mistake Expecting the inverse vocabulary to recover a missing word
What it looks likeConverting the ID 783 back and hoping to see 'dog'.
Why it failsThe unknown token carries only the fact that a word was unfamiliar, not its spelling.
Do this insteadRead the output as <|unk|> and accept that the original word is not recoverable from the ID.
Common mistake Treating a special token as ordinary text
What it looks likeThinking <|endoftext|> is a word that appears inside a story.
Why it failsIt is a boundary marker that separates unrelated sources, and it is added as its own vocabulary entry.
Do this insteadRead it as the seam between two documents, with the one before it ending and the one after it starting.
Common mistake Looking up tokens in a different order than the sample
What it looks likeProducing 2, 0, 1 for 'the cat sat'.Why it failsConverting text to token IDs keeps the original order of the tokens, and the order carries the sentence's meaning.
Do this insteadLook up each token in its original sequence position.
Common mistake Forgetting that the vocabulary stores each token only once, so a repeated word should reuse its existing ID rather than receiving a new one.
Practice: Tracing Your Own Sample
Using the vocabulary the=0, cat=1, sat=2, on=3, mat=4, and the unknown token <|unk|> with ID 783, convert the sample 'the mat sat on the rug' into token IDs. Then reverse the output 0, 4, 2 back into text.
Hints
- Split the sample into its tokens first, keeping their order: the, mat, sat, on, the, rug.
- Check each token against the vocabulary. Words that are present emit their ID; words that are absent emit the unknown token.
- rug is not in the vocabulary, so it becomes 783. Reversing 0, 4, 2 means reading IDs 0, 4, and 2 through the inverse vocabulary.
Two documents, 'the cat sat' and 'on the mat', are joined for training. Write the complete token ID stream, placing the <|endoftext|> token (ID 784) at the correct boundary.
Hints
- Tokenize and look up each document separately before joining them.
- A separator belongs before the second document, since it follows another text source.
Key Takeaways
- Text is cut into tokens by a fixed rule, and a new sample is cut the same way before its pieces are looked up in the vocabulary to produce token IDs in their original order.
- The inverse vocabulary is the same mapping read backward, and it converts a model's numeric output into readable text, one ID at a time.
- Special context tokens are added to the vocabulary as ordinary entries with their own IDs, and they carry meanings that ordinary words cannot express.
- The <|unk|> token replaces any word missing from the vocabulary so tokenization can continue, though the original word cannot be recovered from the ID.
- The <|endoftext|> token is inserted between unrelated documents in a concatenated training stream, marking where one source ends and another begins.