๐ค 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:
- Byte Pair Encoding - How GPT tokenizes (45 min)
- WordPiece - How BERT tokenizes (45 min)
- SentencePiece - Multilingual approach (45 min)
- Multilingual Challenges - Handling all languages (30 min)
Phase 3: Build Your Own (2 hours)
- BUILD: Your Own Tokenizer - Step-by-step project
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:
V1: Tokenization Playgroundโ compare word, character, and GPT-style tokenizationV2: GPT Token Inspectorโ inspect GPT tokens and token IDs interactivelyV3: Real Vocabulary Explorerโ explore real tokenizer assets likevocab.jsonandmerges.txtV4: Interactive BPE Intuition Labโ understand how repeated character pairs become subword units
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)
- Start with BPE - Most common (GPT uses this)
- Then BUILD Your Tokenizer
- Skip to next milestone (Vectorization)
๐ฌ For Complete Understanding
- BPE - How GPT tokenizes
- WordPiece - How BERT tokenizes
- SentencePiece - Multilingual approach
- Multilingual Challenges - All languages
- BUILD Your Tokenizer
โก For Quick Start
- Read this overview
- Go straight to BUILD Your Tokenizer
- 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
- Byte Pair Encoding (BPE) - How GPT tokenizes
- WordPiece - How BERT tokenizes
- SentencePiece - Multilingual approach
- Multilingual Challenges - All languages
Build Project
- BUILD: Your Own Tokenizer - Step-by-step guide
Navigation
- Back to Roadmap - See the big picture
- Next: Vectorization - Milestone 2
