1. Text to tokens
encode splits a string into subword tokens and returns their integer ids.
const tokens = tokenizer.encode("unhappiness");Typorithm · medium
Step 1 of 8 · intro card
Tokenization & Embeddings
Byte-pair encoding (BPE) splits text into subword tokens drawn from a fixed vocabulary. Common words become a single token while rare words split into pieces, so any string is representable, and each token maps to an integer id that indexes the embedding table.
A transformer cannot read raw text — it works with integers. Tokenization converts a string into a sequence of token ids before anything else happens.
Byte-pair encoding (BPE) builds its vocabulary by starting from individual bytes or characters and repeatedly merging the most frequent adjacent pairs. The result is a fixed set of subword tokens.
Common words like "the" become a single token, while a rarer word like unhappiness splits into pieces such as un, happ, and iness. Because the pieces compose, any input — even unseen words — can be represented, and decode reverses the process exactly.
BPE merges frequent pairs into subword tokens.
Common words are one token; rare words split.
Each token maps to an id indexing the embedding table.
// byte-pair encoding merges frequent pairs
const tokens = tokenizer.encode("unhappiness");
// -> ["un", "happ", "iness"] -> ids
const text = tokenizer.decode(tokens);
const vocabSize = tokenizer.vocab.size; // ~100kConnect each part of the code to the concept before you type.
encode splits a string into subword tokens and returns their integer ids.
const tokens = tokenizer.encode("unhappiness");A rare word like unhappiness breaks into reusable subword pieces, each mapped to an id.
// -> ["un", "happ", "iness"] -> idsdecode is the inverse: it turns ids back into the original string.
const text = tokenizer.decode(tokens);The vocabulary is a fixed set of tokens, often around 100k entries for modern LLMs.
const vocabSize = tokenizer.vocab.size;BPE builds its vocabulary by repeatedly merging the most frequent adjacent symbol pairs.
// byte-pair encoding merges frequent pairsRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.