Programming Fundamentals · Data Structures Behind AI

Vocabulary & Trie: How Tokenizers Cut Words

You saw tokenization in the LLM fundamentals part—now the underbelly: how a prefix tree recognizes “五花肉” (streaky pork) as one chunk. Walk a Trie and tokenize by hand

THE QUESTION THIS PAGE ANSWERS

ANSWER FIRST

What is the key idea behind “Vocabulary & Trie: How Tokenizers Cut Words”?

You saw tokenization in the LLM fundamentals part—now the underbelly: how a prefix tree recognizes “五花肉” (streaky pork) as one chunk. Walk a Trie and tokenize by hand

DECISION RULE

Make the claim earn its place. Use this page as a decision aid, not a definition to memorize. Connect the idea to one real task, one observable result, and one failure that would change your mind.

TRY NEXT

Write one question you could answer with evidence after trying this idea.

WATCH FOR

A conclusion that sounds complete but leaves the key assumption untested.

First, the organizing · how a vocabulary hangs as a tree

Suppose the vocabulary has: , 五月, 五花肉, 今天, , 天气, , , . Hang them by first character, then second… shared prefixes share branches—“五月” and “五花肉” crowd the same “五” branch. A green ✓ means “a complete word ends here”; note “五→花” has no ✓—it’s only a waypoint (“五花” isn’t a word).

Pick a sentence:
Hit “Start tokenizing”—the cursor walks the tree character by character. Watch two things: at a ✓ it doesn’t cut yet (greedy—try going longer); when stuck it backs up to the nearest ✓ and cuts.
Tokens:
About 15 seconds, with narration each step
This walk is “greedy longest match”: go as deep as you can; when stuck, back up to the nearest word end and cut. Why greedy? Cutting “五花肉” as one chunk beats “五 / 花 / 肉” on tokens and meaning. Trie’s trick: each step only asks “does this node have a branch for this character?”—no need to rescan the whole vocabulary. Tens of thousands of words, one lookup per step. Again: organize well, look up fast.
Under the hood · real LLMs use BPE—same idea

🧩 BPE: repeatedly merge the “most co-occurring character pairs” into chunks

Real tokenizers (GPT and DeepSeek both use BPE) build vocabularies more wildly: shatter text into tiny fragments, count which two fragments sit next to each other most, glue them into the vocab; count again, glue again, tens of thousands of times. Common combos “grow” into big tokens—same idea as Trie’s “common words stored whole.”

五花肉 → high frequency, each a whole chunk
“饕餮” 饕(frag1) 饕(frag2) 餮(frag1) 餮(frag2) → rare, shattered into byte fragments

So the phenomena you’ve seen make sense: “的” and “,” always cost one token; rare characters get split into several. That’s also why Chinese usually costs more tokens than English—most vocabularies train on English-heavy corpora, so English common words earn whole tokens while Chinese gets fewer, forcing more cuts. Same sentence, Chinese bill often higher—roots in who that “dictionary” vocabulary organized.

How “First, the organizing · how a vocabulary hangs as a tree” changes an answer

“Suppose the vocabulary has: 五 , 五月 , 五花肉 , 今天 , 天 , 天气 , 吃 , 花 , 好 .” shows that a model does not process the “word count” we see. It processes Token pieces. Tokenization affects input length, how much context fits, and how much computation a request consumes.

Length, information, and context are different

As “Real tokenizers (GPT and DeepSeek both use BPE ) build vocabularies more wildly: shatter text into tiny fragments, count which two fragments sit next to each other most , glue them…” grows, separate three questions: how many Tokens the text becomes, which pieces can change the current decision, and whether older material has fallen outside the context window. Removing repetition is often more useful than simply making the window larger.

  • A vocabulary is a dictionary : it organizes all tokens; tokenization is “look up the dict and cut the sentence into chunks”
  • Trie organizes by shared prefixes : longest-match becomes one lookup per step—no backtracking the whole list
  • Greedy longest match : go as deep as you can; when stuck, back up to the nearest word end and cut

Keep what can change the decision

Use “So the phenomena you’ve seen make sense: “的” and “,” always cost one token;” as an A/B test: keep the same question while removing repeated background, compressing format, and trimming irrelevant history. Compare answer quality, latency, and Token count.

From “First, the organizing · how a vocabulary hangs as a tree” to “Under the hood · real LLMs use BPE—same idea”

“First, the organizing · how a vocabulary hangs as a tree” grounds the problem in “Suppose the vocabulary has: 五 , 五月 , 五花肉 , 今天 , 天 , 天气 , 吃 , 花 , 好 . Hang them by first character, then second… shared prefixes share branches—“五月” and “五花肉” crowd the same “五” branch. A green ✓ means “a comple…”. “Under the hood · real LLMs use BPE—same idea” then moves it toward “Real tokenizers (GPT and DeepSeek both use BPE ) build vocabularies more wildly: shatter text into tiny fragments, count which two fragments sit next to each other most , glue them into the vocab; count again…”. Together, they show that the lesson is not just a conclusion to remember, but a claim with conditions.

Carry the judgment into the next situation

For long text, keep what can change the conclusion before compressing format and history. A larger context is worth its cost only when the added information is useful.

  • “First, the organizing · how a vocabulary hangs as a tree”: Suppose the vocabulary has: 五 , 五月 , 五花肉 , 今天 , 天 , 天气 , 吃 , 花 , 好 . Hang them by first character, then second… shared prefixes share branches—“五月” and “五花肉” crowd the same “五” branch. A green ✓ means “a comple…
  • “Under the hood · real LLMs use BPE—same idea”: Real tokenizers (GPT and DeepSeek both use BPE ) build vocabularies more wildly: shatter text into tiny fragments, count which two fragments sit next to each other most , glue them into the vocab; count again…
  • “The closing point”: Why Chinese costs more tokens : English-leaning vocabularies, fewer whole Chinese tokens, so more cuts

The final “The closing point” brings the discussion to “Why Chinese costs more tokens : English-leaning vocabularies, fewer whole Chinese tokens, so more cuts”. The useful thing to carry forward is knowing which judgments must be revisited when input, scale, or risk changes.

What this lesson wants to share

  • A vocabulary is a dictionary: it organizes all tokens; tokenization is “look up the dict and cut the sentence into chunks”
  • Trie organizes by shared prefixes: longest-match becomes one lookup per step—no backtracking the whole list
  • Greedy longest match: go as deep as you can; when stuck, back up to the nearest word end and cut
  • BPE shares the same idea: repeatedly merge the most co-occurring pairs into chunks—common stays cheap, rare gets shattered
  • Why Chinese costs more tokens: English-leaning vocabularies, fewer whole Chinese tokens, so more cuts
Mark as learned Your reading progress updates automatically
← PreviousNext →

Keep reading

The next useful article in the thread.

ARTICLE DISCUSSION

Leave one useful thought here.

Keep the idea that clicked, the question that stayed open, or a small note for the next learner.

Discussing Vocabulary & Trie: How Tokenizers Cut Words Data Structures Behind AI
3discussionsArticle discussion · synced with the Circle
View in the learning circle
AM
Asha MorganContent editor
INSIGHTField note

I turned one judgment from this article into a small experiment I could run today. Knowing what to observe next is more useful than simply remembering the conclusion.

ARTICLE DISCUSSION7 helpful
LH
Lin HarperIndie developer
INSIGHTInsight

After reading this, I first looked for the conditions behind the idea instead of copying the method into a project. That order made the later trade-offs much clearer.

ARTICLE DISCUSSION5 helpful
KM
Kiki MooreProduct operations
QUESTIONQuestion

When this judgment reaches real work, which constraint should be added first? I am curious which step matters most between reading and the first practical attempt.

ARTICLE DISCUSSION4 helpful