๐Ÿงญ Roadmap
Difficultyโญโญ / 5 โฑ Read time2-3 hours (for overview + one deep dive) nlptokenizationmilestone-1

๐Ÿ”ค Tokenization: Teaching Your AI to Read

Milestone 1 of 6 - The Foundation of ALL Modern AI


๐ŸŽฏ Where This Fits in Your AI Journey

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  YOUR AI AGENT BUILDING JOURNEY                             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                             โ”‚
โ”‚  [1] Tokenization โ† YOU ARE HERE                           โ”‚
โ”‚       โ†“ (Teaching AI to read)                              โ”‚
โ”‚  [2] Vectorization                                          โ”‚
โ”‚       โ†“ (Teaching AI to understand meaning)                โ”‚
โ”‚  [3] Attention                                              โ”‚
โ”‚       โ†“ (Teaching AI to focus)                             โ”‚
โ”‚  [4] Transformer                                            โ”‚
โ”‚       โ†“ (Teaching AI to process)                           โ”‚
โ”‚  [5] LLM                                                    โ”‚
โ”‚       โ†“ (Teaching AI to generate)                          โ”‚
โ”‚  [6] Agent                                                  โ”‚
โ”‚       โ†“ (Teaching AI to act)                               โ”‚
โ”‚                                                             โ”‚
โ”‚  FINAL: Your Own AI Agent! ๐ŸŽ‰                              โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Without tokenization: Your AI can't read text at all! With tokenization: Your AI can read ANY text in ANY language!


๐ŸŒ Where This Fits in the AI Landscape

The Complete Timeline

1950s-1980s: Rule-Based AI
โ””โ”€ NO TOKENIZATION (worked with symbols/rules)

1990s-2000s: Statistical NLP
โ””โ”€ Word-level tokenization (split by spaces)
   Problem: Can't handle unknown words!

2013: Word Embeddings Era
โ””โ”€ Still word-level tokenization
   Problem: Huge vocabulary, unknown words fail

2016: SUBWORD TOKENIZATION BREAKTHROUGH โ† THIS CHAPTER!
โ”œโ”€ BPE (Byte Pair Encoding)
โ”œโ”€ WordPiece (BERT)
โ””โ”€ SentencePiece (multilingual)
   REVOLUTION: Can handle ANY word!

2017-Present: Transformer Era
โ”œโ”€ BERT (2018) - Uses WordPiece
โ”œโ”€ GPT-2/3/4 (2019-2023) - Uses BPE
โ””โ”€ EVERY modern LLM uses subword tokenization!

Who Uses This?

Every Major AI Company:

  • OpenAI (ChatGPT, GPT-4): BPE tokenization, ~100K vocabulary
  • Google (BERT, T5, Gemini): WordPiece & SentencePiece
  • Meta (LLaMA): BPE, 32K vocabulary
  • Anthropic (Claude): Custom tokenization

Impact: Trillions of tokens processed daily worldwide!

Infographic takeaway: what changed over time

The infographic progression from older language models to modern LLMs highlights one big shift:

  • older systems often depended on whole words
  • modern systems depend on subword pieces
  • that change made it possible to handle:
    • rare words
    • new words
    • misspellings
    • domain-specific terms
    • multilingual text

In short:

Word-level NLP  โ†’  fragile vocabulary
Subword NLP     โ†’  flexible vocabulary
Transformer LLM โ†’  scalable token-based intelligence

That is why tokenization is not a preprocessing detail โ€” it is part of the modelโ€™s core design.


๐ŸŒŸ Real-World Scenario

You're texting: "I'll meet you at 5pm tomorrow! ๐Ÿ˜Š"

Your friend reads it instantly. But AI can't read English!

What AI sees:

# Option 1: Tokens (pieces)
["I", "'ll", " meet", " you", " at", " 5", "pm", " tomorrow", "!", " ๐Ÿ˜Š"]

# Option 2: Numbers (IDs)
[40, 3278, 1826, 345, 379, 642, 4426, 0, 50118]

The crazy part:

  • "ChatGPT" = 2 tokens = $0.00004 (GPT-4 pricing)
  • "Chat GPT" = 3 tokens = $0.00006 (50% more expensive!)
  • Tokenization affects: Cost, speed, understanding!

๐Ÿ—บ๏ธ Learning Path

Phase 1: Understand the Basics (30 minutes)

Read this overview file to understand:

  • Why tokenization matters
  • How it fits in AI history
  • What you'll build

Phase 2: Deep Dive (1-2 hours)

Choose topics to explore:

Phase 3: Build Your Own (2 hours)

Phase 4: Practice (1 hour)

  • Run code examples
  • Test on real text
  • Compare with GPT tokenizer

Live Tokenization Lab

For hands-on exploration, use the live mini-project collection in tokenization_lab_v1:

See the full guide in README.md.


๐Ÿง  Core Concept: What is Tokenization?

The Problem

AI models work with numbers, not text. We need to convert:

"Hello world" โ†’ [15496, 995]

Three Approaches

1. Word-Level Tokenization โŒ

"Hello world" โ†’ ["Hello", "world"]

Problem:

  • Huge vocabulary (millions of words)
  • Can't handle unknown words
  • "running" and "run" are completely different

2. Character-Level Tokenization โŒ

"Hello world" โ†’ ["H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]

Problem:

  • Very long sequences
  • Loses word meaning
  • Slow to process

3. Subword Tokenization โœ… (Modern Approach)

"Hello world" โ†’ ["Hello", " world"]
"running" โ†’ ["run", "ning"]
"unbelievable" โ†’ ["un", "believ", "able"]

Benefits:

  • Small vocabulary (~50K tokens)
  • Handles unknown words
  • Shares meaning ("run", "running", "runner")

๐Ÿ’ก Why This Matters

For Your AI Agent

Without Tokenization:
โ”œโ”€ โŒ Can't read text at all
โ”œโ”€ โŒ Can't handle new words
โ””โ”€ โŒ Can't work in multiple languages

With Tokenization:
โ”œโ”€ โœ… Can read ANY text
โ”œโ”€ โœ… Handles unknown words automatically
โ”œโ”€ โœ… Works in 100+ languages
โ””โ”€ โœ… Efficient (50K tokens vs millions of words)

For Your Wallet

API Pricing (GPT-4):
โ”œโ”€ Input: $0.03 per 1,000 tokens
โ””โ”€ Output: $0.06 per 1,000 tokens

Example:
โ”œโ”€ "don't" = 1 token = $0.00003
โ”œโ”€ "do not" = 2 tokens = $0.00006 (2x more!)
โ””โ”€ Optimization can save 30-50% on costs!

๐ŸŽจ Visual Understanding

The Tokenization Pipeline

Raw Text
    โ†“
Normalization (lowercase, unicode)
    โ†“
Pre-tokenization (split on spaces)
    โ†“
Tokenization Algorithm (BPE/WordPiece/SentencePiece)
    โ†“
Post-processing (add special tokens)
    โ†“
Token IDs (numbers)

Example Flow

Input: "I'm learning AI!"
    โ†“
Normalize: "i'm learning ai!"
    โ†“
Pre-tokenize: ["i'm", "learning", "ai", "!"]
    โ†“
Tokenize: ["i", "'m", "learn", "ing", "ai", "!"]
    โ†“
To IDs: [72, 1101, 4673, 278, 9552, 0]

๐Ÿ” Quick Example

# Simple tokenization
text = "Hello, world! I'm learning AI."

# Word-level (naive)
tokens_word = text.split()
# Output: ["Hello,", "world!", "I'm", "learning", "AI."]
# Problem: Punctuation attached, contractions not split

# Subword-level (modern)
tokens_subword = tokenize(text)  # Using BPE
# Output: ["Hello", ",", " world", "!", " I", "'m", " learn", "ing", " AI", "."]
# Better: Handles punctuation, splits contractions, shares "learn"

๐Ÿ“š Deep Dive Topics

Choose Your Path:

๐ŸŽฏ For Building AI Agent (Recommended)

  1. Start with BPE - Most common (GPT uses this)
  2. Then BUILD Your Tokenizer
  3. Skip to next milestone (Vectorization)

๐Ÿ”ฌ For Complete Understanding

  1. BPE - How GPT tokenizes
  2. WordPiece - How BERT tokenizes
  3. SentencePiece - Multilingual approach
  4. Multilingual Challenges - All languages
  5. BUILD Your Tokenizer

โšก For Quick Start

  1. Read this overview
  2. Go straight to BUILD Your Tokenizer
  3. Learn algorithms as needed

๐Ÿ”จ What You'll Build

Project: Your Own Tokenizer

By the end of this milestone, you'll have:

# Your tokenizer in action!
tokenizer = YourTokenizer()

# Tokenize text
text = "I'm building my own AI!"
tokens = tokenizer.tokenize(text)
print(tokens)
# Output: ["I", "'m", "build", "ing", "my", "own", "AI", "!"]

# Count tokens
count = len(tokens)
print(f"Token count: {count}")
# Output: Token count: 8

# Estimate cost
cost = (count / 1000) * 0.03  # GPT-4 pricing
print(f"Estimated cost: ${cost:.6f}")
# Output: Estimated cost: $0.000240

# Encode to IDs
ids = tokenizer.encode(text)
print(ids)
# Output: [40, 1101, 1382, 278, 616, 898, 9552, 0]

# Decode back to text
decoded = tokenizer.decode(ids)
print(decoded)
# Output: "I'm building my own AI!"

๐ŸŽฏ What You Can Build After This

Immediate Projects (With just tokenization)

1. Token Counter App โญ

Input: Any text
Output: Token count, estimated cost
Use: Before calling expensive APIs
Time: 30 minutes

2. Text Optimizer โญโญ

Input: Long prompt
Output: Shorter version (same meaning)
Use: Save 30-50% on API costs
Time: 1 hour

3. Language Detector โญโญ

Input: Text in any language
Output: Detected language
Use: Route to appropriate model
Time: 1 hour

Advanced Projects (After learning more)

4. Semantic Search (+ Vectorization) โญโญโญ

Tokenize โ†’ Embed โ†’ Search
Find similar documents

5. Text Summarizer (+ Attention) โญโญโญโญ

Tokenize โ†’ Embed โ†’ Attend โ†’ Summarize
Extract key points

6. AI Agent (Full Stack) โญโญโญโญโญ

Tokenize โ†’ Embed โ†’ Attend โ†’ Transform โ†’ Generate โ†’ Act
Your ultimate goal!

๐Ÿš€ Getting Started

Step 1: Read This Overview โœ… (You're doing it!)

Step 2: Choose Your Path

Option A: Quick Start (2-3 hours)

1. Read [BPE](01-Foundations--01-Tokenization--01.a-BPE.html) (45 min)
2. Build [Your Tokenizer](01-Foundations--01-Tokenization--01-BUILD-Tokenizer.html) (2 hours)
3. Move to Milestone 2

Option B: Deep Dive (4-5 hours)

1. Read all algorithm files (3 hours)
2. Build [Your Tokenizer](01-Foundations--01-Tokenization--01-BUILD-Tokenizer.html) (2 hours)
3. Move to Milestone 2

Option C: Hands-On First (2 hours)

1. Jump to [BUILD](01-Foundations--01-Tokenization--01-BUILD-Tokenizer.html) (2 hours)
2. Learn algorithms as you code
3. Move to Milestone 2

Step 3: Build Your Tokenizer

Follow the step-by-step guide in BUILD: Your Own Tokenizer

Step 4: Test and Celebrate!

# Test your tokenizer
text = "I just built my own tokenizer! ๐ŸŽ‰"
tokens = your_tokenizer.tokenize(text)
print(f"Success! {len(tokens)} tokens")

๐Ÿ“Š Progress Checklist

Track your progress:

  • Read this overview
  • Understand why tokenization matters
  • Choose a learning path
  • Read at least one algorithm deep dive
  • Build your own tokenizer
  • Test on real text
  • Compare with GPT tokenizer
  • Celebrate! ๐ŸŽ‰

๐Ÿ”— Quick Links

Deep Dive Topics

Build Project

Navigation


Vocabulary Size by Tokenizer Type
Larger vocab = fewer tokens per sentence, but more memory
๐Ÿ“š Subword tokenizers (BPE, WordPiece) hit the sweet spot between flexibility and efficiency