LLM concepts, explained two ways
The ideas behind every chatbot, one per page, from tokens and attention to training, inference, evaluation and real applications. Each concept is explained twice, in plain English for anyone and technically for developers, with a worked example for each and an interactive figure built on real data.
Foundations
- Token (Beginner): A token is the small piece of text, often a word or part of a word, that a language model reads, counts and generates one at a time.
- Tokenizer (Beginner): A tokenizer is the program that splits text into tokens and maps each one to an id, using a vocabulary learned once before the model is trained.
- Embedding (Beginner): An embedding is a list of numbers, a vector, that represents a token or a piece of text so that things with similar meaning end up close together.
- Next-token prediction (Beginner): Next-token prediction is the task a language model performs: given the text so far, it scores every token in its vocabulary and one token is chosen as the next.
- Context window (Beginner): A context window is the maximum number of tokens, prompt and reply together, that a language model can take into account at one time.
Architecture
- Dimension (Intermediate): A dimension is one slot in a vector, and a model's dimension or hidden size is how many numbers it uses to represent every token at each layer.
- Attention (Intermediate): Attention is the step where each token looks back at the earlier tokens, scores how relevant each one is, and blends in information from the most relevant.
- Positional encoding (Intermediate): Positional encoding is the signal that tells a transformer where each token sits in the text, because attention alone cannot tell “dog bites man” from “man bites dog”.
- Feed-forward network (Intermediate): A feed-forward network is the step in each transformer layer that processes every token on its own: it widens the token's vector, applies a nonlinearity and shrinks it back.
- Normalization and residual connections (Intermediate): Residual connections add each layer's output to its input, and normalization rescales the numbers, so dozens of transformer layers can be trained without the signal fading or exploding.
- Transformer (Intermediate): A transformer is a neural network built from a stack of identical blocks, each mixing information between tokens with attention and then refining each token on its own.
- Grouped-query attention (Intermediate): Grouped-query attention lets several query heads share one set of key and value heads, cutting the KV cache by the group size at little cost in quality.
- Sliding-window attention (Intermediate): Sliding-window attention limits each token to reading only the most recent fixed number of tokens, so attention work and cache size stop growing with the length of the text.
- Mixture of experts (Intermediate): A mixture-of-experts model splits its feed-forward layers into many experts and uses a router to send each token to only a few, so most of its parameters stay idle per token.
- Multimodal models (Intermediate): A multimodal model accepts more than text by cutting an image into small patches, turning each into a vector, and feeding those vectors to the transformer as if they were tokens.
Training
- Pre-training and loss (Intermediate): Pre-training is the first and largest stage of building an LLM, in which the model learns to predict the next token on trillions of tokens of text and is scored by cross-entropy loss.
- Scaling laws (Advanced): Scaling laws are empirical formulas showing that a language model's loss falls smoothly as a power of its parameters, training tokens and compute, and they guide how to split a budget.
- Fine-tuning and RLHF (Intermediate): Fine-tuning and RLHF are the stages after pre-training that teach a base model to follow instructions, first from human-written examples and then from human preference rankings.
- Distillation (Intermediate): Distillation is training a small student model to imitate a larger teacher, using the teacher's output probabilities or generated text as targets instead of only the correct answers.
- Reasoning models (Intermediate): A reasoning model is a language model trained to write out a long chain of thought before its final answer, spending extra tokens at answer time to solve harder problems.
Inference
- KV cache (Intermediate): The KV cache is the stored keys and values of every earlier token, kept so a language model can generate each new token without recomputing the whole conversation.
- Sampling and temperature (Beginner): Sampling is how a language model picks the next token from its probabilities, and temperature, top-k and top-p are the dials that make the choice safer or more adventurous.
- Quantization (Intermediate): Quantization stores a model's numbers in fewer bits, for example 4 instead of 16, so it needs a fraction of the memory and often runs faster, at some cost in precision.
- Latency and throughput (Intermediate): Latency is how quickly one request gets its tokens and throughput is how many tokens a server produces in total; for LLM decoding both are limited mainly by how fast weights can be read from memory.
- Speculative decoding (Advanced): Speculative decoding speeds up generation by letting a cheap draft guess several tokens ahead and having the large model verify them all in one pass, without changing the output distribution.
Evaluation
- Hallucination (Beginner): A hallucination is a fluent, confident statement from a language model that is not true: the model produces plausible text and is rewarded for guessing over admitting it does not know.
- Benchmarks (Intermediate): A benchmark is a fixed set of questions or tasks with automatic scoring used to compare language models; its score is only as meaningful as the test and as precise as its question count.
- Elo and Arena ratings (Intermediate): An Arena rating ranks language models from votes in blind pairwise comparisons, turning wins and losses into a score where a rating gap predicts how often one model beats another.
Applications
- RAG and vector search (Intermediate): Retrieval-augmented generation looks up the passages most relevant to a question using embeddings, then gives them to a language model so it answers from those sources.
- Tool use and agents (Intermediate): Tool use lets a language model request that an application run a function, such as search or a calculation, and agents repeat that loop until a task is finished.
- Open-weight models (Beginner): An open-weight model is a language model whose trained weights are published for download, so anyone can run and adapt it, though that is not always the same as being fully open source.