Lesson 20 Tokenization Deep Dive
Theme:

Homework

Complete these tasks before Lesson 21. You may work in a notebook, a script, or a combination. The goal is to deepen your understanding of tokenization beyond what the lab covered.

Task 1: Byte-Level BPE Extension

Goal: Extend your BPE implementation from the lab to operate on bytes instead of characters, matching the approach used by GPT-2/3/4 and tiktoken.

Instructions

  1. Take the BPE implementation you wrote in Exercise 1 of the lab.
  2. Modify it to start from UTF-8 byte sequences instead of Unicode characters:
    # Instead of splitting "Hello" into ['H','e','l','l','o']
    # Split into bytes: [72, 101, 108, 108, 111]
    text = "Hello"
    byte_seq = list(text.encode("utf-8"))
    print(byte_seq)  # [72, 101, 108, 108, 111]
  3. Train your byte-level BPE on a longer corpus — use at least 1000 words of English text (you can use any text file or a paragraph from Wikipedia).
  4. Run 200 merges and examine the resulting vocabulary.
  5. Compare the tokens your implementation produces with tiktoken's cl100k_base on 5 test strings. They will not match exactly (tiktoken was trained on a much larger corpus with 100K merges), but look for patterns:
    • Do both discover common English words as tokens?
    • How do they differ on rare words?
    • What about punctuation and whitespace?

Deliverable

A notebook or script that:

Self-check

Task 2: Llama 3 Tokenizer Research

Goal: Understand how tokenizer design has evolved to address multilingual efficiency.

Instructions

Research the changes between Llama 2's tokenizer (32K vocabulary, SentencePiece BPE) and Llama 3's tokenizer (128K vocabulary, tiktoken-based). Write a 200-word summary addressing:

  1. Vocabulary size: Why did Meta increase from 32K to 128K tokens? What are the trade-offs of a larger vocabulary?
  2. Training data: How did the composition of the tokenizer training data change, and why does this matter for multilingual performance?
  3. Measured impact: What concrete improvements were reported for non-English languages? (Look for specific numbers — e.g., "X% fewer tokens for language Y.")
  4. Architecture impact: A larger vocabulary means a larger embedding table. How does this affect total model size and inference speed?

Resources to Start With

Self-check

Task 3: Multilingual Cost Analysis

Goal: Apply tokenization knowledge to a realistic business scenario.

Instructions

You are designing a multilingual customer support chatbot that will serve users in English, Spanish, Japanese, Hindi, and Arabic. The expected usage is:

Language% of trafficAvg. conversation length (words)
English40%300
Spanish25%280
Japanese15%200
Hindi12%250
Arabic8%260

Total conversations per day: 50,000. Model: Claude Sonnet at $3/M input tokens, $15/M output tokens. Assume each conversation involves 4 API calls (2 user messages + 2 assistant responses), with output averaging 60% of input token length.

  1. Using your token ratios from Exercise 3 of the lab, calculate the weighted average token multiplier across your user base.
  2. Calculate the annual API cost broken down by language.
  3. Calculate how much you would save if a hypothetical "perfectly balanced" tokenizer reduced all non-English languages to 1.0x English efficiency.
  4. Write a brief recommendation (100–150 words) for the engineering team on whether tokenizer efficiency should influence model selection for this project.

Deliverable

A spreadsheet, notebook, or script that produces:

Self-check

Submission

There is no formal submission for this course. These tasks are for your own development. However:

Next lesson: Lesson 21 — Transformers from Scratch — where we build the architecture that consumes these tokens.