October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

LLM Primitives in TypeScript: Build a Tokenizer, Vector Search, and Attention

Build inspectable TypeScript examples of BPE tokenization, cosine-similarity vector comparison, and self-attention, while seeing why they are not a complete LLM.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can build small TypeScript versions of three LLM-related operations—a byte-pair encoding (BPE) tokenizer, cosine-similarity search over vectors, and scaled dot-product attention—to see how text becomes token IDs and how token representations interact. These are inspectable learning primitives, not a compatible production tokenizer, trained embedding model, retrieval-augmented generation (RAG) system, or complete LLM.

The practical value is in exposing the steps and intermediate data. The example approach uses a BPE tokenizer, a toy lookup that returns fixed-dimension Float64Array vectors, and attention that temporarily treats those embeddings as its queries, keys, and values. Each choice makes the mechanics easier to inspect, while leaving important parts of a real Transformer out.

As an Amazon Associate I earn from qualifying purchases.

What these three primitives do

A language model works with numerical representations rather than raw words alone. The three operations here illustrate separate stages: breaking text into token IDs, comparing vectors, and combining contextual information with attention. They are useful for learning, but implementing them does not by itself produce a language model.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Tokenization: converts text into token IDs. BPE builds subword units by merging symbol pairs according to a merge vocabulary.
  • Vector similarity: compares numerical vectors. Cosine similarity measures how closely their directions align.
  • Self-attention: scores relationships between tokens, then uses the scores to weight and combine value vectors.

As the tutorial puts it: “These are three inspectable primitives used in LLM-related workflows. They are not an LLM.”

Build the BPE tokenizer

A BPE tokenizer represents text as subword pieces rather than requiring every possible word to have its own vocabulary entry. It repeatedly applies learned pair merges to an initial sequence of symbols, then maps the resulting pieces to token IDs. For a learning implementation, the important parts to inspect are the vocabulary, merge rules, and the sequence of transformations from input string to IDs.

Do not assume that a small educational implementation produces the same IDs as GPT-2 or any other production tokenizer. Compatibility depends on details such as the exact vocabulary, merge ranks, text preprocessing, and special-token rules. This implementation is described as BPE, not as a compatible tokenizer.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Compare vectors with cosine similarity

Cosine similarity compares two vectors by the angle between them. A common expression is dot(a, b) / (norm(a) * norm(b)); values closer to 1 indicate more similarly directed vectors, while values closer to -1 indicate opposing directions. Similarity is undefined if either vector has zero magnitude, so an implementation needs a deliberate policy for that case.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The example’s embedding lookup maps token IDs to fixed-dimension Float64Array values. This is useful for demonstrating vector operations and search, but the values are not established as learned semantic embeddings. A trained embedding model requires learning from data; a lookup table alone does not make its vectors semantically meaningful.

Compute scaled dot-product self-attention

Attention lets each token gather information from other tokens. For each token, a query is compared with keys using dot products; scaling and softmax turn those scores into weights, and the weights combine the corresponding value vectors. The output is a context-dependent representation.

The learning example simplifies this computation by setting queries, keys, and values directly to the embeddings. In a Transformer, learned linear projections produce the query, key, and value representations. The direct-embedding version is easier to inspect, but it is not the full learned attention mechanism.

From basic attention to autoregressive attention

A natural progression is to begin with weight-free self-attention, then add trainable query, key, and value projections and scale the dot products before softmax. For next-token generation, a causal mask blocks each position from attending to later positions, preventing access to future tokens. Multi-head attention runs several attention operations in parallel across different representational subspaces.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

These are meaningful extensions, not details the three primitives automatically provide. In particular, attention without a causal mask is not constrained to the current and preceding positions.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check shapes and understand the limits

TypeScript annotations help document intended inputs, but they do not validate matrix dimensions at runtime. Functions that operate on vectors or matrices should check shape compatibility explicitly—for example, that dot-product operands have equal lengths and that matrix dimensions can be multiplied—then report invalid inputs clearly.

Even with those checks, this sandbox is not a complete Transformer. A fuller architecture also needs positional information, normalization, feed-forward sublayers, residual connections, and assembled Transformer blocks. A training-capable system additionally needs a learning process; the toy vector lookup is not evidence of trained representations. No performance measurements establish this educational implementation as a fast or competitive model.

Continue from the primitives to a Transformer

For a deeper treatment of attention, Manning’s Build a Large Language Model (From Scratch) develops the progression from basic self-attention through learned projections, causal attention, and multi-head attention. The publisher page also links to source code and additional learning resources.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.