Special Tokens
The punctuation of the vocabulary
Special tokens are reserved vocabulary entries with syntax power: end-of-sequence, padding, role markers, tools and JSON boundaries. Misusing them silently breaks fine-tunes and chats.
What you will learn
- Identify the core special tokens
- Explain their roles in chat and training
- Avoid EDGE cases that corrupt them
What is this?
Beyond subword pieces, vocabularies reserve special tokens — end-of-sequence (EOS), begin/end markers, role delimiters (system/user/assistant), tool-call tokens and JSON boundaries. Generation ends at EOS; chat formats mark roles with them; structured output models leg them into JSON. They are first-class signals, not ordinary text.
Why do you need it?
- Generation relies on EOS to stop — misconfiguring it produces infinite runs.
- Chat/tool templates depend on exact role tokens; drift breaks parsing.
- Structured-format models output JSON through reserved delimiters.
- Fine-tune data built with wrong specials creates irreproducible checkpoints.
Mental model
Punctuation in a transcript
Special tokens
A period stops the sentence; quote marks fence quoted speech; '[end of tape]' tells the transcriber to stop. If the transcriber confuses quotes with narration, the whole interview is misread. Special tokens do exactly this structural work.
How it works
- The vocab reserves dedicated IDs (e.g. <|endoftext|>, <|system|>, <|assistant|>).
- Chat protocols interleave reserved role markers around user/assistant content.
- Tool schemes add <|tool_call|>/<|tool_response|> openings.
- Structured-output checkpoints emit JSON between boundary tokens for reliable extraction.
- LLM training maximizes EOS probability at stop points — conditioning you must preserve.
Special tokens orchestrating a chat.
In production
- Trust the SDK to place role tokens; never hand-roll templates without review.
- Tool-calling parsers must tolerate escaped boundary tokens inside tool args.
- Fine-tune data must follow the exact role-special scheme of the target checkpoint.
Best practices
- Use the provider's message format — do not re-invent role markers.
- Validate that EOS actually terminates; cap max_tokens as a safety net.
- Sanitize user content so it cannot forge special tokens.
Common mistakes
- Stripping or renaming specials and 'fixing' the template yourself.
- Letting user text smuggle <|assistant|>-like tokens (format injection).
- Training on data whose roles don't match the deployed template.
Common issues
Generation loops foreverEOS not recognized in stop-sequence config
Set the correct stop tokens; raise max_tokens guard.
Tool parser mis-splits on boundary tokens in argsEscaping not applied to embedded specials
Escape or encode the argument payload before template insertion.
Debugging tips
- Runs never end → EOS absent or masked; check stop-sequence config.
- Assistant parsers see user-tagged blocks → role tokens misaligned or escaped wrong.
- Fine-tuned model stops immediately → your data ended everything with EOS rather than the task answer.
Interview checkpoint
Hands-on lab
- Count tokens for a realistic chat multi-turn prompt
- Verify rule-of-thumb estimates against a real tokenizer
- Fix a budget blowout by trimming the largest slot
Token-Count Every Major Slot
30 min · beginner
Quick quiz
Which special token's presence normally ends generation?
User text containing role-like tokens is harmless inside a chat template.