Special tokens

Also known as: control tokens

On this page 8

Special tokens, also called control tokens, are reserved entries in a tokenizer vocabulary that carry structural meaning instead of text. In language models and across artificial intelligence more broadly, they mark where a sequence begins or ends, separate segments, pad batches to equal length, hide positions for training objectives and delimit conversational roles in chat formats.

Unlike ordinary tokens, special tokens are never produced by splitting normal text. The tokenizer inserts them deliberately, and the model learns their function during pretraining or fine-tuning. A model that receives them in the wrong place or format often produces degraded, truncated or runaway output, which makes them a frequent source of subtle bugs in deployment.

How special tokens work inside the tokenizer

Special tokens work by occupying dedicated vocabulary IDs that the tokenizer protects from normal segmentation. When a tokenizer based on byte-pair encoding, WordPiece or SentencePiece processes input, it first scans for registered special strings and maps each one to a single fixed ID. The surrounding text is then split into subword tokens in the usual way.

Each special token has an embedding vector like any other token, so the model treats it as an input position with learned meaning. Its function is not hard-coded in the network. A beginning-of-sequence token becomes useful only because training data consistently placed it at the start of documents, and the model learned the statistical role that position implies.

Many tokenizers also distinguish between parsing special strings from user text and adding them programmatically. Safe pipelines add control tokens through the template code and escape or ignore matching strings that appear inside untrusted input, which prevents a user from forging a role boundary.

Common types of special tokens

Most special tokens fall into a small set of functional families, although each model family names and formats them differently. The table below lists the main categories and representative examples.

Token familyTypical examplePurpose
Beginning of sequenceBOS tokenSignals the start of a document or prompt
End of sequenceEOS tokenTells the decoder to stop generating
Padding[PAD]Fills shorter sequences in a batch
Classification[CLS]Collects a sequence summary in encoder models
Separator[SEP]Divides two segments, such as question and passage
Mask[MASK]Hides positions for masked language modeling
Unknown[UNK]Replaces characters outside the vocabulary
Role delimitersim_start, im_end markersBound system, user and assistant turns
Main families of special tokens and their roles

Byte-level tokenizers rarely need an unknown token, because any byte sequence can be represented. Recent chat and tool-calling models add further reserved tokens for function calls, tool results, image placeholders and reasoning segments, and vocabularies often keep hundreds of unused reserved IDs for future extensions.

Special tokens in chat templates and decoding

In modern chat models, special tokens are the backbone of the chat template. The template converts a list of messages into one token sequence, wrapping each message in role delimiters so the model can tell system instructions from user requests and its own earlier replies. Instruction tuning teaches the model to answer only after the assistant marker.

During generation, the end-of-sequence or end-of-turn token is the normal stopping signal. The decoding loop halts when the model samples that token, which is more reliable than a textual stop sequence. If the serving stack does not register the correct stop ID, the model may continue writing a fake user turn and then answer it.

The following table compares how the three main architecture families depend on special tokens.

ArchitectureKey special tokensMain dependency
Encoder-only modelCLS, SEP, MASK, PADPretraining objective and classification head
Decoder-only modelBOS, EOS, role markersStopping and turn structure
Encoder-decoder modelEOS, PAD, task prefixesSeparating source from target sequences
Special token use by model architecture

Special tokens are often confused with stop sequences, system prompts and glitch tokens, yet each concept operates at a different layer of the pipeline. A stop sequence is a plain text pattern checked by the serving software, while a special token is a single vocabulary ID that the model itself predicts.

ConceptLevelRelation to special tokens
Stop sequenceServing softwareText match that can supplement the EOS token
System promptMessage contentWrapped by role delimiters, not a token itself
Glitch tokenVocabulary anomalyOrdinary token with an undertrained embedding
Reasoning tokensGenerated contentOften bounded by dedicated start and end markers
Logit biasSampling controlCan suppress or force a special token ID
How special tokens differ from neighboring concepts

Reserved special tokens that never appeared during training share one weakness with glitch tokens: their embeddings remain close to initialization. Feeding such an ID to a model can trigger erratic output, which is why fine-tuning on new control tokens usually requires training the embedding and output layers as well.

Configuring special tokens in practice

Correct configuration means matching the tokenizer, template and model exactly. Practitioners should load the tokenizer shipped with the checkpoint, apply its official chat template and verify the final token IDs before training or evaluation. The decision table below covers frequent situations.

SituationRecommended actionReason
Fine-tuning a chat modelUse the original chat templateMismatched delimiters break learned turn structure
Adding a new control tokenResize embeddings and train themNew rows start untrained
Batching variable lengthsSet a pad token and attention maskPadding must not influence outputs
Serving through an APIRegister every end-of-turn ID as a stopPrevents runaway generation
Processing untrusted textDisable parsing of special stringsBlocks forged role boundaries
Decision table for handling special tokens

A common shortcut is to reuse the EOS token as the pad token when a model lacks a padding token. This works only if the loss ignores padded positions; otherwise the model learns to predict EOS too rarely or too often, producing outputs that never stop or end abruptly.

Failure modes and security risks

Most problems with special tokens are silent configuration errors rather than crashes. Typical symptoms include duplicated BOS tokens, missing end-of-turn markers and models that ignore system instructions.

  • Double BOS insertion occurs when both the template and the tokenizer add a start token, which slightly shifts model behavior.
  • Missing EOS in training data teaches a fine-tuned model never to stop.
  • Special token injection lets an attacker who can type delimiter strings impersonate the system or assistant role.
  • Template drift between training and inference lowers quality even when every message is correct.
  • Truncation that removes the final marker leaves the model unsure whose turn it is.

Because these errors seldom raise exceptions, robust pipelines include automated tests that decode the token IDs of a sample conversation and compare them with the expected template.

A plain-language summary

A language model reads text as small pieces called tokens. Special tokens are hidden markers mixed in with those pieces. They do not stand for words. They work like punctuation for the machine.

One marker says where a text starts. Another says where it ends. Others show who is speaking: the system, the user or the assistant. The model learns these signals during training. If the markers are wrong, the model gets confused. It may stop too early or keep talking.

An example from practice

A support team fine-tunes an open-weight chat model on 20,000 past customer conversations. The first trained version answers well but then keeps writing invented customer questions and answers them, often filling the full limit of 1,024 new tokens.

An engineer decodes a training sample back into token IDs and finds the cause: the custom formatting script wrapped each reply in role markers but omitted the end-of-turn token. The model never saw a reply end. After the team switches to the official chat template and registers the end-of-turn ID as a stop token, average reply length drops from about 900 tokens to about 180 tokens, and the runaway turns disappear.