AI & GENERATIVE AI / SYSTEM CONCEPT BRIEF

Tokenization

Language models do not read characters or whole words.

BeginnerPhase 09 / Topic 8 of 30RequirementsTrade-offsFailure modes
01

Overview

Language models do not read characters or whole words. They read tokens, which are frequent chunks of text learned from training data. Common words like "the" are one token, while a rare word like "tokenization" may split into "token" and "ization". Each token maps to an integer ID, and the model only ever sees those IDs.

Tokenization affects almost every production concern. Pricing, rate limits, and context windows are measured in tokens. Non-English text, code, and numbers often use more tokens per character, which raises cost. Odd model behavior, such as miscounting letters in a word, often comes from the model seeing tokens instead of letters.

Shorthand notes

A stenographer does not write every letter. Common words and phrases get short symbols, while rare words are spelled out in pieces. Tokenization is the model's shorthand. Frequent text is compact, and unusual text takes more symbols.

02

When to use it

  • Estimating LLM cost and latency.
  • Checking whether a prompt and retrieved documents fit in the context window.
  • Chunking documents for retrieval by token count.
  • Explaining strange model behavior with spelling, numbers, or rare words.
03

Where it shows up in interviews

Token budgeting

Recognize it when: the design has long documents, chat history, or per-token billing.

  • Design a RAG system
  • Design an LLM gateway
04

Where it is used in real software

API billing

LLM providers charge separately for input and output tokens, so tokenization directly sets cost.

Chat history trimming

Chat apps count tokens to decide which older messages to summarize or drop.

Multilingual products

Teams measure tokens per language because some scripts need two to three times more tokens for the same meaning.

05

Key terms

Token
A unit of text the model reads, often a word or word piece.
Vocabulary
The fixed set of tokens a model knows, typically 30,000 to 200,000 entries.
Byte-pair encoding (BPE)
A method that builds tokens by repeatedly merging the most frequent adjacent pairs of symbols.
Special tokens
Reserved tokens that mark roles, message boundaries, or the end of text.
06

How it works, step by step

  1. 1
    Normalize the text

    Some tokenizers adjust whitespace or Unicode before splitting.

  2. 2
    Split into pieces

    The tokenizer applies learned merge rules to break text into the largest known chunks.

  3. 3
    Map to IDs

    Each chunk becomes an integer from the vocabulary.

  4. 4
    Model processes IDs

    IDs are turned into embedding vectors and processed by the network.

  5. 5
    Decode the output

    Generated IDs are mapped back to text pieces and joined.

From text to IDs
Step 1 / 4
Text
Pieces
Token IDs
Model

STEP 1Input text is "Tokenization helps".

07

Token counts vary by content

Approximate token counts for 100 characters of different content (exact numbers depend on the tokenizer).

Step 1 / 4
ContentApprox. tokensWhy
Plain English prose22-25Common words are single tokens
Source code30-40Symbols and indentation split often
Long numbers40-60Digits split into small groups
Non-Latin scripts40-80Fewer merged pieces in the vocabulary

NOWContent: Plain English prose | Approx. tokens: 22-25 | Why: Common words are single tokens

Always measure tokens with the model's own tokenizer instead of guessing from characters.

08

Implementation

import tiktoken enc = tiktoken.get_encoding("cl100k_base")text = "Tokenization helps models read text."ids = enc.encode(text)print(len(ids), ids)print([enc.decode([i]) for i in ids])       # see each piece def fits(prompt: str, context_limit: int, reserve_for_output: int = 500) -> bool:    return len(enc.encode(prompt)) + reserve_for_output <= context_limit
09

Complexity and performance

Encodingroughly O(n) in text length

Fast compared with model inference.

Rule of thumbabout 4 characters per token

For English prose only.

10

Trade-offs

Vocabulary size

Larger vocabularies make text shorter in tokens but increase the embedding table size and can waste capacity on rare tokens.

Byte-level vs word-level

Byte-level tokenizers never hit unknown words but split rare text into many small pieces.

11

Variants and related techniques

WordPiece

Used by BERT-style models; similar goal to BPE with a different merge score.

SentencePiece

Treats text as raw characters including spaces, which suits languages without spaces.

12

Common mistakes

  • Estimating cost from word counts.

    Fix: Count tokens with the exact tokenizer for the model you call.

  • Chunking documents by characters only.

    Fix: Chunk by tokens so each chunk reliably fits the embedding model and prompt budget.

13

Interview questions

Why can an LLM struggle to count the letters in a word?

It sees tokens, not letters. A word may be one or two tokens, so individual characters are not directly visible to the model.

How would you keep a long chat within the context window?

Count tokens per message, keep the system prompt and the most recent turns, summarize older turns into a short memory, and reserve space for the answer.

14

Practice problems

ProblemDifficultyWhat it trains
Build a token counter that warns before a prompt exceeds the limitEasyBudgeting.
Design a chat memory policy with summarizationMediumContext management.