Concepts / Special Context Tokens

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.

  • Programming

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?

  • The ID of the closest known word

  • An <|unk|> token

  • Nothing, because the word is silently dropped

Reveal answer
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.

python
Output (expected)
[0, 1, 2]
Notes on the code
  • 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.
theID 0catID 1satID 2order keptorder kept
For the sample 'the cat sat', which vocabulary entry does each token look up, and what ID does each one produce?
Worked example

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.

  1. 1
    Tokenize

    Apply the same space-splitting rule to the sample, giving the tokens the, cat, sat in their original order.

  2. 2
    Look up 'the'

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

  3. 3
    Look up 'cat'

    The vocabulary maps cat to 1, so the second ID is 1.

  4. 4
    Look up 'sat'

    The vocabulary maps sat to 2, so the third ID is 2. Order is preserved throughout.

Result

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.

Language modelproduces numbersToken IDs0, 1, 2, 3, 0, 4Inverse vocabularyID to token lookupReadable textthe cat sat on the matnumeric outputeach ID in ordertokens joined
How does a model's numeric output travel through the inverse vocabulary to become readable text, and where does that step sit relative to the model?
StructureDirectionQuestion it answersExample
VocabularyToken to IDWhich number stands for this token?the maps to 0
Inverse vocabularyID to tokenWhich 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.

Worked example

Running an output sequence backward

A model outputs the ID sequence 0, 1, 2, 3, 0, 4. Using the inverse vocabulary, recover the text.

  1. 1
    Read ID 0

    The inverse vocabulary maps 0 to the.

  2. 2
    Read ID 1

    ID 1 maps to cat.

  3. 3
    Read ID 2

    ID 2 maps to sat.

  4. 4
    Read ID 3

    ID 3 maps to on.

  5. 5
    Read ID 0 again

    The repeated ID 0 maps to the again, showing that the same ID always returns the same token.

  6. 6
    Read ID 4

    ID 4 maps to mat.

Result

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.

EntryComes fromMeaning it carriesExample ID in source illustration
theTraining textAn ordinary word0
<|unk|>Added special entryThis word was not in the vocabulary783
<|endoftext|>Added special entryA new unrelated text source begins here784

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.

Next word in sampleIn vocabulary?Emit its IDordinary lookupContinue with nextwordEmit <|unk|>fallback markerMarker's ID783 in source illustrationlookupyesnomarker takes a positionsequence continuessequence continues
When a word is not found in the vocabulary, which branch does the tokenizer take, and where does the unknown word end up as an ID?

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.

Worked example

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'.

  1. 1
    Tokenize

    Split the sample into the, dog, sat.

  2. 2
    Look up 'the'

    the is in the vocabulary, so it emits 0.

  3. 3
    Look up 'dog'

    dog is not in the vocabulary, so the tokenizer emits the <|unk|> token instead, with ID 783. It does not stop.

  4. 4
    Look up 'sat'

    sat is in the vocabulary, so it emits 2. Tokenization finishes the sample.

Result

The token ID sequence is 0, 783, 2.

Edge case

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.

Sports result<|endoftext|>boundaryFairy tale<|endoftext|>boundaryFinancial report<|endoftext|>boundaryRomantic momentendsnext startsendsnext startsendsnext starts
In a single training stream, where do the separator tokens fall between documents, and what comes right before and after each boundary?

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.

Independentdocumentstokenized and looked upInsert separatortokens<|endoftext|> before eachlater documentConcatenate into onestreamoriginal order keptTraining inputone sequence of token IDsIDs for each documentmarked documentssingle stream
What sequence of steps turns several independent documents into one prepared training input, and where are the separator tokens inserted?
Worked example

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.

  1. 1
    Tokenize and look up document one

    'the cat sat' becomes 0, 1, 2.

  2. 2
    Tokenize and look up document two

    'the mat' becomes 0, 4.

  3. 3
    Insert separator

    Document two follows another text source, so an <|endoftext|> token (784) is placed before it. The first document gets no leading marker.

  4. 4
    Concatenate

    Join the pieces in order into one stream.

Result

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 like

    Tokenizing 'the dog sat' and expecting only two IDs.

    Why it fails

    The tokenizer emits an <|unk|> token in place of the missing word and keeps going, so the position is preserved.

    Do this instead

    Expect 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 like

    Converting the ID 783 back and hoping to see 'dog'.

    Why it fails

    The unknown token carries only the fact that a word was unfamiliar, not its spelling.

    Do this instead

    Read 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 like

    Thinking <|endoftext|> is a word that appears inside a story.

    Why it fails

    It is a boundary marker that separates unrelated sources, and it is added as its own vocabulary entry.

    Do this instead

    Read 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 like
    Producing 2, 0, 1 for 'the cat sat'.
    Why it fails

    Converting text to token IDs keeps the original order of the tokens, and the order carries the sentence's meaning.

    Do this instead

    Look 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

MEDIUM

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.
EASY

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.

Related concepts

More concepts starting with S