Beginner5 min read2 of 52

Token

The atomic unit of text that an LLM processes — a sub-word piece that balances vocabulary size and semantic meaning.

SPEC: Token

Definition

[Definition] A token is the atomic unit of text that an LLM processes. It is a piece of text — such as a word, sub-word, character, or punctuation symbol — that the model reads and generates one unit at a time.

Why Tokens (Not Characters or Words)?

  • Characters → too granular, very long sequences, poor semantic grouping
  • Words → vocabulary explodes (millions of rare/compound words), can't handle unknown words
  • Sub-word tokens → best balance: compact vocabulary (~32K–128K tokens), handles rare words by splitting them, retains common words whole

Common Tokenization Schemes

SchemeDescriptionUsed By
BPE (Byte Pair Encoding)Merges frequent byte pairs iterativelyGPT-2, GPT-4, LLaMA
WordPieceSimilar to BPE, maximizes language model likelihoodBERT
SentencePieceLanguage-agnostic, works on raw bytesT5, Gemini
TiktokenOpenAI's fast BPE implementationGPT-3.5, GPT-4

Token Examples (GPT-4 tokenizer)

TextTokensCount
"Hello, world!"["Hello", ",", " world", "!"]4
"tokenization"["token", "ization"]2
"LLM"["L", "LM"] or ["LLM"]varies

Key Properties

  • Vocabulary size: typically 32K–128K unique tokens
  • Token ≠ word: one word can be 1–4 tokens; one token can span multiple characters
  • Special tokens: <|endoftext|>, <s>, </s>, [CLS], [SEP], [PAD] — control model behavior
  • Whitespace matters: " hello" and "hello" are often different tokens

Token Counting Rules of Thumb

  • 1 token ≈ 4 characters (English)
  • 1 token ≈ 0.75 words (English)
  • Non-English languages are typically less efficient (more tokens per word)
  • Code is generally tokenized efficiently

Practical Implications

[Key Insight] - Cost: APIs charge per token (input + output) - Context limits: models have a maximum token count they can process at once (context window) - Latency: more tokens = slower generation - Prompt design: being concise saves tokens and cost

Related Concepts

  • Tokenization, Embeddings, Context Window, Vocabulary, LLM