Skip to main content

Tokenizers and BPE (Technical Deep Dive)

:::note Prerequisite Assuming you've seen the concept of "tokens" in 03. How an LLM works, this document covers how tokens are actually produced. It is the entrance to all computation (attention). :::

1. Why a tokenizer is needed​

A model handles neither characters nor words but arrays of integer IDs. A tokenizer is the dictionary that converts text ↔ IDs.

"security" ──(encode)──▶ [3034, 1793] ──model──▶ ... ──(decode)──▶ "security"

There were three options:

UnitProblem
CharacterSequences too long (inefficient), weak as a meaning unit
WordVocabulary explosion; can't handle typos/neologisms/out-of-vocabulary (OOV)
SubwordA compromise of the two — the modern LLM standard

2. BPE (Byte-Pair Encoding) — the core algorithm​

The most widely used subword method. It builds the vocabulary by repeatedly merging frequently co-occurring pairs.

Training (vocabulary building):

Start: every character is a token
low, lower, newest, widest ...
→ l o w / l o w e r / n e w e s t ...

Repeat: merge the most frequent "adjacent pair" into one
1st: (e, s) most frequent → create "es"
2nd: (es, t) → "est"
3rd: (l, o) → "lo"
... repeat until the target vocabulary size is reached

At inference (encoding), the learned merge rules are applied in order to split text into tokens.

:::info Variants

  • Byte-level BPE (GPT family): BPE over bytes rather than Unicode characters → any character (emoji, rare glyphs) is representable with no OOV
  • WordPiece (BERT): merge criterion is likelihood increase rather than frequency
  • SentencePiece (multilingual): treats whitespace as a symbol (▁) to work without language boundaries :::

3. Vocabulary​

  • Vocabulary size is usually 30k–300k (e.g., 32k, 100k, 256k)
  • Each token ID maps 1:1 to a row of the embedding table (attention doc)
  • Special tokens exist: <bos> (start), <eos> (end), <pad>, <unk>, and role tokens for chat templates

4. Why Korean uses more tokens​

For the same meaning, Korean consumes more tokens than English.

"security" → [sec][urity] 2 tokens
"보안" → [보][안] or [보안] 1–2 tokens (model-dependent)
"정보보호" → [정][보][보][호] possible up to 4 tokens

Causes:

  • English-centric vocabularies are rich in English subwords; Korean tends to split into character units
  • In byte-level BPE, one Hangul character = 3 UTF-8 bytes → may split more finely

Impact:

  • Cost (per-token billing) and context-length limits are less favorable for Korean
  • Models with multilingual/Korean-specialized tokenizers are more token-efficient for Korean

5. Practical notes​

  • Tokens ≠ character count: length limits and cost are token-based
  • Different tokenizers give different token counts for the same sentence → mind this when comparing cross-model cost
  • How code/numbers/whitespace are handled differs by model, creating performance differences

6. Summary​

ConceptOne line
TokenizerText ↔ integer-ID conversion dictionary
BPEBuild a subword vocabulary by repeatedly merging frequent pairs
byte-levelByte-based, so no OOV (GPT family)
VocabularyToken ID ↔ embedding row, 1:1
Korean inefficiencyVocabulary bias + UTF-8 bytes raise token count