
Vocabulary and Special Tokens
Meet the 'Invisible Architects'. Learn about BOS, EOS, and conversation-specific tokens that prevent your model from rambling forever.
Vocabulary and Special Tokens: The Invisible Architects
In the previous lesson, we learned how regular words like "Cat" are turned into tokens. But in every LLM's vocabulary, there is a set of "Special Tokens." These are tokens that represent Structure, not Language.
Imagine a script for a play. The words the actors say are the language. But the words like [Exit Stage Right] or [End of Scene] are instructions for the production. Special tokens are the "Stage Directions" for an LLM.
If you don't handle special tokens correctly during fine-tuning, your model will never stop talking, it will confuse who is speaking, and it will lose its ability to understand the boundary between a user's question and its own answer.
1. The Core Special Tokens
Every model architecture (Llama, Mistral, GPT-4) has its own unique names, but the functions are almost always the same.
BOS (Beginning of Sequence)
- Purpose: Tells the model "A new thought starts here."
- Example:
<s>in Llama 2 or<|begin_of_text|>in Llama 3.
EOS (End of Sequence)
- Purpose: Tells the model "Shut up now." This is the most important token for fine-tuning. If the model doesn't learn to output this token, it will ramble until it hits its context limit.
- Example:
</s>or<|end_of_text|>or<|eot_id|>.
PAD (Padding)
- Purpose: A "Null" token used to make all sentences in a training batch the same length. (We will cover this in Lesson 4).
- Example:
[PAD]or<pad>.
2. Conversation-Specific Tokens (Chat Tokens)
When we fine-tune for Chat (as we did in Module 6), we introduce tokens that define Roles.
In the ChatML format, when the text is tokenized, it looks something like this:
<|im_start|>user\nWho are you?<|im_end|>\n<|im_start|>assistant\n
<|im_start|>: Tells the model "A person is starting to speak."<|im_end|>: Tells the model "The speaker is finished."
Why this is critical for Agents
If your model is an agent calling a tool, it might have a special token like <|call_tool|>. When the model generates this token, your software "intercepts" it and runs the API. If you don't fine-tune the model to understand this specific token, it will just output the literal text and won't trigger the action.
Visualizing the Token Streams
graph TD
A["Raw Text: 'Hi!' 'Hello.'"] --> B["Token Stream"]
subgraph "With Special Tokens"
B --> C["[BOS] Hi! [EOS]"]
C --> D["[BOS] Hello. [EOS]"]
end
subgraph "The Control Logic"
D1["BOS -> Start Processing"]
D2["EOS -> Stop Generating"]
end
Implementation: Accessing Special Tokens in Python
You can view the special tokens for any model using the transformers library. This is the first thing you should do when working with a new model.
from transformers import AutoTokenizer
# 1. Load the tokenizer for Llama 3
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B")
# 2. Inspect the 'Invisible' tokens
print(f"BOS Token: {tokenizer.bos_token} (ID: {tokenizer.bos_token_id})")
print(f"EOS Token: {tokenizer.eos_token} (ID: {tokenizer.eos_token_id})")
print(f"Pad Token: {tokenizer.pad_token} (ID: {tokenizer.pad_token_id})")
# 3. Check for 'Additional' Special Tokens
# Many models hide their chat-roles here
print(f"Additional Special Tokens: {tokenizer.additional_special_tokens}")
The "Endless Room" Bug
If your fine-tuned model starts producing infinite text, or repeats the user's question over and over, you have an EOS Token Mismatch.
- This happens if your training data used
</s>but your inference engine is looking for<|end_of_text|>. - Because the engine never sees the token it's looking for, it keeps asking the model for "one more token" until it crashes.
Always ensure your training labels conclude with the precise EOS token ID of your base model.
Summary and Key Takeaways
- Special Tokens are structural markers, not linguistic content.
- BOS/EOS control the lifecycle of a generation.
- Role Tokens (im_start/im_end) define who is speaking in a conversation.
- Inference Failure: Most "infinite generation" bugs are caused by missing or mismatched EOS tokens in the training data.
In the next lesson, we will look at how to handle Long Contexts and Truncation, ensuring your training data doesn't get "cut off" by the model's memory limits.
Reflection Exercise
- If you are fine-tuning a model to write code, would you want a special token for
[START_CODE]and[END_CODE]? Why? - Why is the
Paddingtoken necessary if we are training on GPUs? (Hint: Can a matrix have "jagged" edges where some rows are longer than others?)
SEO Metadata & Keywords
Focus Keywords: BOS and EOS tokens LLM, ChatML special tokens, Llama 3 special tokens, Padding token AI, Endless Generation bug LLM. Meta Description: Meet the invisible architects of LLM conversations. Learn about BOS, EOS, and role-based tokens, and how failing to handle them leads to the 'Endless Generation' bug.