How a Language Model Works: Tokens, Next-Word Prediction, Embeddings and Attention

A tiny language model you can poke: see text split into tokens (words, characters or BPE pieces), how a model scores the next word, what temperature, top-k and top-p do, how words become vectors, and how attention mixes words. Paste your own text. Private: nothing is uploaded.

A toy, on purpose. This is a tiny language model that learns from a short text in a fraction of a second, so you can see every step: how text becomes tokens, how a model scores the next word, what temperature does, and how attention mixes words. Real chatbots do the same things with billions of numbers and much more text.
Use your own text

Text becomes tokens

A model never sees letters or words, only numbered pieces called tokens. Type anything and choose how to cut it up.

How a language model works, with a tiny one you can poke

Large language models predict the next token. Everything else (answering, summarising, coding) comes from doing that billions of times with a model that has learned patterns from an enormous amount of text. This page builds the smallest honest version of each ingredient so you can see it work.

Questions

What is a token?

A piece of text with a number. Whole words are simple but cannot handle new words; single characters never fail but make very long sequences. Byte-pair encoding (BPE) starts from characters and repeatedly glues together the most frequent neighbouring pair, so common words become one token and rare words are built from pieces. The "word pieces" view learns its pieces from the practice text.

What do temperature, top-k and top-p do?

The model gives every word a probability. Temperature sharpens (below 1) or flattens (above 1) those probabilities before one is drawn at random: 0 always takes the most likely word, high values get adventurous and eventually nonsense. Top-k only allows the k most likely words, and top-p only the smallest group of words whose probabilities add up to p.

Why does this toy repeat itself and wander?

It only remembers the last one to three words and has read a few hundred. Real models look at thousands of earlier tokens, have read much of the internet, and replace the counting table with a neural network that generalises, but sampling works the same way.

What is an embedding?

A list of numbers that stands for a word, where similar words get similar lists. Here they come from counting which words appear near each other (positive pointwise mutual information, then keeping the 8 strongest directions with SVD). Chatbots learn theirs while training.

What is attention?

The mixing step of a transformer: softmax(Q·Kᵀ/√d)·V. The weights here use random, untrained matrices, so the pattern is not meaningful language: it shows the mechanism. Choose Similarity of word vectors to see attention when the weights simply compare real word vectors. A chatbot hides "look backwards only" so a word cannot peek at the future.

Is it private?

Yes. Everything is computed in your browser and nothing is sent anywhere. The three practice texts were written for this page.

Comments
Advertisement