Writing the underlying pieces as ordinary code exposes their intermediate data and operations. This tutorial builds three of them from scratch in TypeScript: a byte-pair encoding (BPE) tokenizer, cosine similarity vector search, and scaled dot-product attention.

Why Build LLM Primitives in TypeScript?

Products like ChatGPT and Claude can make LLM primitives look like a black box. Writing the underlying pieces as ordinary code exposes their intermediate data and operations. This tutorial builds three of them from scratch in TypeScript: a byte-pair encoding (BPE) tokenizer, cosine similarity vector search, and scaled dot-product attention. Each exposes a different mechanism: BPE converts text into token IDs, vector similarity compares numerical representations, and attention computes a weighted combination of value vectors from query-key scores.