What is a padding token?
A padding token is a special token used to make sequences in the same batch have equal length.
LLMs often process multiple sequences together for efficiency, but those sequences may contain different numbers of tokens. Padding fills the unused positions in shorter sequences.
Why is padding needed?
Suppose a batch contains two token sequences:
Sequence A → 8 tokens
Sequence B → 5 tokensTo process them together as a rectangular tensor, the shorter sequence can be padded:
Sequence A → 8 real tokens
Sequence B → 5 real tokens + 3 padding tokensConceptually:
A: token token token token token token token token
B: token token token token token <PAD> <PAD> <PAD>Does the model treat padding as normal text?
Usually no.
An attention mask is commonly used to tell the model which positions contain real tokens and which positions are padding.
Conceptually:
Real token → attend
Padding token → ignoreThis prevents padding positions from influencing the meaningful parts of the sequence.
What does a padding token look like?
Its representation depends on the tokenizer.
It may appear conceptually as:
<PAD>Internally, it has a reserved token ID.
Some model families have a dedicated padding token, while others may reuse another special token for padding depending on their tokenizer and serving setup.
Left padding vs. right padding
Padding can be added:
- After the real tokens — right padding
- Before the real tokens — left padding
For example:
Right padding:
Hello world <PAD> <PAD>
Left padding:
<PAD> <PAD> Hello worldWhich approach is appropriate depends on the model architecture and inference implementation.
Is padding always necessary?
No.
A single sequence does not usually need padding.
Some modern serving systems also use techniques that reduce or avoid unnecessary padding when batching variable-length requests.
Padding is mainly useful when sequences must be arranged into equal-sized batches.
For the broader category, see “What are special tokens in an LLM?”
In short: a padding token is a reserved token used to fill unused positions so sequences of different lengths can be processed together efficiently, while an attention mask usually prevents those padding positions from affecting the model's actual computation.
Learning Sequence
Large Language Models Curriculum
Explore Related Questions
Deepen your understanding across related Large Language Models topics.