Special Tokens Explained: [UNK], [PAD], [CLS], [SEP], BOS, and EOS

What the square-bracket tokens mean: [UNK], [PAD], [CLS], [SEP], [MASK], and the BOS and EOS markers, and why a model cannot work without them.

Aug 11, 20266 min readFollow

Topics You Will Master

Why models need tokens that are not real words
What [UNK], [PAD], [CLS], [SEP], and [MASK] each do
How BOS and EOS control where generation starts and stops
Why the attention mask matters when you pad a batch

Open any tokenizer's vocabulary and you will find strange entries near the top: [CLS], [SEP], [PAD], [UNK]. They are not words. Nobody writes them. Yet without them, a Transformer cannot do its job.

In simple words, these are control tokens. They carry instructions rather than meaning, a little like punctuation marks that only the model can read. Let's meet them one by one.

Bestseller

LLM Fine-Tuning with Hugging Face: LoRA, QLoRA, PEFT

Fine-tune BERT, T5, ViT, LLaMA-style models and Qwen3-TTS using Hugging Face Transformers, custom datasets, LoRA, QLoRA.

Enroll on Udemy 30 day refund, lifetime access

Every Special Token in One Real Input


Why a Model Needs Fake Words

A Transformer only ever sees numbers. Every instruction we want to give it has to arrive as a token ID, because that is the only channel it has.

So when we want to say "this is where the sentence starts", or "ignore this position", we cannot use a comment or a flag. We invent a token, give it an ID, and let the model learn what it means during training.

An Instruction Becomes a Row Number


[UNK]: The Unknown Token

[UNK] stands for unknown. It is the fallback when the tokenizer meets something it cannot represent at all.

With modern subword tokenizers this is rare, because the vocabulary always contains single characters, and byte-level methods contain every possible byte. [UNK] is the last resort, not the normal path.

PYTHON
from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("bert-base-uncased")
tok.tokenize("𓂀")     # a glyph outside the vocabulary -> ['[UNK]']

The Fallback Ladder to [UNK]


[PAD]: Making Sentences the Same Length

Models process text in batches, and every row in a batch must be the same length. Real sentences are not. So we pad the short ones with a filler token until they match the longest.

[PAD] carries no meaning. It exists purely to square off the batch.

Why a Batch Must Be Rectangular


The Attention Mask: Telling the Model to Ignore Padding

Padding creates a new problem. If we add filler tokens, the model might pay attention to them and learn from noise.

The fix is the attention mask, a simple list of ones and zeros that travels alongside the token IDs. One means "this is real, look at it". Zero means "this is padding, ignore it".

PYTHON
enc = tok(["the cat sat", "it rained"], padding=True)
enc["attention_mask"]     # [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0]]

What the Attention Mask Switches Off


[CLS]: The Summary Slot

BERT-style models put [CLS] at the very front of every input. It starts as an empty slot with no meaning of its own.

As the model runs, attention lets that slot gather information from every other token. By the final layer it holds a summary of the whole sentence, which is exactly what a classifier needs. When you fine-tune BERT for sentiment, the prediction is read from this one position.

How [CLS] Fills Up Layer by Layer


[SEP]: Marking the Boundary

[SEP] marks where one part of the input ends and the next begins. It matters whenever a task involves two pieces of text, like a question and a passage, or two sentences being compared.

Without it, the model would see one long run of words and have no idea where the question stopped and the answer material started.

PYTHON
enc = tok("is it waterproof?", "the case is water resistant")
tok.convert_ids_to_tokens(enc["input_ids"])   # [CLS] ... [SEP] ... [SEP]

What [SEP] Tells the Model


[MASK]: The Token That Trains BERT

[MASK] is the token behind BERT's training trick. During training, some words are hidden and replaced with [MASK], and the model must guess what was there.

That single game is how BERT learns language. It never sees [MASK] at prediction time in normal use; it is a training-only device.

One Round of Masked Language Training


BOS and EOS: Start and Stop

Generative models like GPT need two more markers. BOS means beginning of sequence, the signal to start. EOS means end of sequence, the signal to stop.

EOS is the important one at generation time. The model generates one token at a time, and it keeps going until it produces EOS. That token is how a model decides an answer is finished.

EOS Is the Only Thing That Stops Generation


Let me tabulate them for your better understanding.

Token Job Where it appears
[UNK] Text the tokenizer cannot represent Rare, last resort only
[PAD] Filler so a batch is rectangular End of short sequences
[CLS] Summary slot for the whole input Front of BERT inputs
[SEP] Boundary between two parts Between and after segments
[MASK] Hidden word the model must guess Training only
BOS Start of generation Front of generated text
EOS Stop generating here End of generated text

Where the Special Tokens Live

Special tokens sit at the very start of the vocabulary, usually holding the lowest IDs. That is a convention, not a rule, but it makes them easy to find and easy to reserve.

Different families use different names for the same ideas. BERT uses [CLS] and [SEP]. GPT-style models often use <|endoftext|> for EOS. The names differ; the jobs do not.

Where Special Tokens Sit in the Vocabulary


Recap

This is what the square-bracket tokens are for. They are not words, they are instructions delivered through the only channel a model has, its token IDs.

[UNK] catches text the tokenizer cannot represent. [PAD] squares off a batch, and the attention mask tells the model to ignore that padding. [CLS] gathers a summary of the whole sentence for classification, [SEP] marks a boundary between two inputs, and [MASK] powers BERT's fill-in-the-blank training. BOS and EOS start and stop generation.

In the next article, we look at the trade-off hiding behind every tokenizer: a bigger vocabulary means shorter sequences, but it also costs memory, and that tension shapes how models are designed.

Found this useful? Keep building with me.

New tutorials every week on YouTube: or go deeper with a full structured course.

Find this tutorial useful?

Subscribe to our YouTube channels for more practical production walk-throughs.

Discussion & Comments