Lesson 3 of 5 · Tokens
Special Tokens
Special tokens in LLMs: start and end of text, system, user and assistant turns, padding, tool calls and fill-in-the-middle, and why users can't type them.
- 6 min read
- Intermediate
- Reviewed
- What special tokens are and how they differ from ordinary tokens
- The common kinds: start, end, role markers, padding and tool tokens
- Why text that looks like a special token is not one
- How special tokens add hidden tokens to every request
Tokens that are not text
Most tokens stand for pieces of text. A few are reserved for structure: they mark where a document starts, where a message ends, or who is speaking. They never come from ordinary text.
Common special tokens
| Kind | Purpose | Typical look (varies by model) |
|---|---|---|
| Beginning / end of text | Mark where a document starts or stops; the model emits "end" when it has finished | <|endoftext|>, <s>, </s> |
| Role markers | Show where system, user and assistant messages begin | <|user|>, <|assistant|> |
| End of turn | Close a message; the model learns to emit it when its reply is done | <|end|>, <|eot|> |
| Padding | Fill short sequences so a batch has equal lengths during training | <pad> |
| Tool calls | Mark where the model is calling a tool and where the result starts | Model-specific |
| Fill-in-the-middle | Let code models fill a gap between a prefix and a suffix | <fim_prefix>, <fim_suffix> |
Every model family defines its own set. The chat template (from the Large Language Models course) is the recipe for inserting them correctly.
Typed text is not a special token
If users could type a special token, they could fake the structure: close their own message and start a pretend "system" message. So tokenizers treat typed text that merely looks like a special token as ordinary characters.
vocabulary = {"Hi": 1, " there": 2, "!": 3, "<": 4, "|": 5, "end": 6, ">": 7}
# Special tokens get their own reserved IDs, outside the normal vocabulary
special = {"<|user|>": 100, "<|assistant|>": 101, "<|end|>": 102}
def encode_text(text):
# Plain text is ALWAYS encoded as ordinary pieces (greedy longest match)
ids = []
i = 0
while i < len(text):
best = None
for piece in vocabulary:
if text.startswith(piece, i) and (best is None or len(piece) > len(best)):
best = piece
ids.append(vocabulary[best])
i += len(best)
return ids
def encode_message(role, text):
# The app adds the real special tokens around the user's text
return [special[f"<|{role}|>"]] + encode_text(text) + [special["<|end|>"]]
print("Normal message: ", encode_message("user", "Hi there!"))
print("Sneaky message: ", encode_message("user", "Hi<|end|>"))The app wraps the user's text in the real special tokens (IDs 100 and 102). When the user types <|end|> themselves, it becomes five ordinary pieces (4, 5, 6, 5, 7), not ID 102. The model can tell the difference, because it only ever saw ID 102 as real structure.
Hidden tokens in every request
Because special tokens and role markers are added around each message, a request uses more tokens than the visible text. Providers usually report the true number in the response's usage information.
Common mistakes
The text you see is not everything the model reads: special tokens are added around each message. Count them in.
content_tokens = [12, 30, 8] # tokens in the text of each of 3 messages per_message = 4 # special and role tokens added around each message reply_start = 3 # tokens that start the assistant's reply # Work out the total tokens the model really receives and print: # Total tokens: 65
A user types the text of a special token, such as an end-of-turn marker, into a chat. What does a correctly configured tokenizer do?
Summary
- Special tokens mark structure: start and end of text, roles, end of turn, padding, tool calls and fill-in-the-middle.
- Each model family has its own; the chat template inserts them correctly.
- Typed text that looks like a special token is encoded as ordinary text, which protects the conversation structure.
- Special tokens add hidden tokens to every request.
Next: counting tokens accurately, and estimating when you can't.
Finished reading? Tick it off to track your progress.