How LLMs Work: From Next-Token Prediction to Intelligence
This comprehensive guide explains large language models from first principles, covering token prediction, information theory, neural networks, Transformer architecture, training via gradient descent, and the path from base models to chat assistants like ChatGPT, including limitations such as hallucinations.
Introduction
The article uses a single narrative thread to explain how large language models (LLMs) like ChatGPT work: what they do, why they can do it, how they are built, and what they cannot do. No machine learning background is required; mathematics is limited to basic arithmetic and logarithms, explained via a "guess the number" game and the sentence "He was thirsty, so he drank a cup of ____".
Language Model Essence
Predicting the Next Token
A language model outputs a probability distribution over the next token given preceding context. Tokens are the smallest units (characters, words, or subwords). Generation proceeds autoregressively: sample a token, append it, and repeat.
A language model is a program that, given preceding text, outputs a probability distribution over the next token.
Why Token Prediction Leads to Intelligence
Turing's imitation game (1950) proposes that if a machine's text is indistinguishable from a human's, it possesses human-like intelligence. Predicting the next token perfectly means matching the human writing distribution, thus passing the Turing test. Accurate prediction requires knowledge ("Paris"), reasoning ("shortest"), and common sense ("throw away"), forcing the model to internalize these capabilities.
Probability and Information
Information measures uncertainty reduction. In a guessing game with 8 equally likely numbers, each yes/no question halves the candidates; 3 questions (3 bits) suffice. The information of an outcome with probability p is I(x) = -log₂ p. Lower probability → higher information (surprise).
Entropy: Average Uncertainty
Entropy H(X) = -Σ p(x) log₂ p(x) is the expected information. For a biased coin (90% heads), entropy ≈ 0.469 bits vs. 1 bit for fair. Entropy is the theoretical lower bound for lossless compression.
Cross-Entropy and Perplexity
Cross-entropy H(p, q) = -Σ p(x) log₂ q(x) measures average bits needed when encoding with model distribution q instead of true p. It is always ≥ entropy, equality iff q = p. Training minimizes cross-entropy on real text, where true probabilities are implicit in the data. Perplexity = 2^{cross-entropy} indicates the effective branching factor.
Shannon's Experiment
Shannon (1951) estimated English entropy by having humans guess characters. With ~100 characters of context, entropy dropped to 0.6–1.3 bits/character. A model's cross-entropy upper-bounds the true language entropy; better models tighten this bound.
Neural Networks
Neurons and Layers
A neuron computes y = ReLU(w·x + b). Multiple inputs use weighted sum. Layers stack neurons; parameters (weights, biases) define the function. Non-linearities (ReLU) enable complex functions; without them, deep networks collapse to linear maps.
Softmax: Scores to Probabilities
The final layer outputs logits (scores) per token. Softmax exponentiates and normalizes to a probability distribution.
Universal Approximation Theorem
A single hidden layer with enough neurons can approximate any continuous function. However, it doesn't specify neuron count, how to find parameters, or generalization.
Depth Advantage
Deep networks exponentially increase expressivity per parameter (Telgarsky 2016). They also mirror hierarchical language processing: characters → words → phrases → meaning. Training deep nets requires normalization (LayerNorm/RMSNorm) and residual connections (y = x + F(x)) to mitigate vanishing/exploding gradients.
Gradient Descent and Backpropagation
Training minimizes cross-entropy via gradient descent: θ ← θ - η∇L. For Softmax + cross-entropy, the gradient for each logit is simply (predicted probability - target). Backpropagation computes all parameter gradients in one forward + one backward pass using the chain rule, enabling efficient training of billions of parameters.
Overfitting vs. Generalization
Models can memorize training data (overfit) or learn reusable patterns (generalize). Validation loss detects overfitting. Massive datasets and limited epochs make classic overfitting rare, but verbatim memorization of repeated data remains a concern.
Transformer Architecture
Embeddings
Tokens map to learned vectors (embeddings). Similar tokens acquire similar vectors (e.g., "water" and "tea"), enabling generalization to unseen contexts via vector proximity.
Attention Mechanism
Each position computes Query, Key, Value vectors via learned matrices. Attention scores = Q·Kᵀ/√d, softmaxed to weights, then weighted sum of Values. Causal masking prevents attending to future tokens. Multi-head attention runs several independent attention heads in parallel, capturing diverse relations (e.g., subject-verb, coreference).
Attention(Q, K, V) = softmax(QKᵀ/√d) VPositional Encoding
Attention is permutation-invariant; Rotary Positional Embeddings (RoPE) rotate Q and K by position-dependent angles, making scores depend on relative distance.
Feed-Forward Networks
Each position independently passes through a two-layer MLP (expand → non-linearity → project). FFNs contribute ~2/3 of parameters and act as key-value memories, recognizing patterns and boosting relevant token probabilities.
Transformer Block
Each block: x = x + Attention(LayerNorm(x)); x = x + FFN(LayerNorm(x)). Pre-LN placement stabilizes training. Stacking dozens of layers (GPT-3: 96) yields the final model.
From Model to ChatGPT
Autoregressive Generation
Tokens are sampled sequentially; once emitted, they cannot be retracted, causing error propagation.
Sampling Strategies
Greedy (argmax), random, top-k, top-p (nucleus). Top-p adapts candidate set size. Temperature scales logits before softmax: low T sharpens, high T flattens.
Base Model → Assistant
Pre-training yields a base model that only continues text. Supervised Fine-Tuning (SFT) on instruction-response pairs teaches the assistant format. RLHF/DPO aligns with human preferences via reward modeling or direct preference optimization. Reasoning RL (e.g., DeepSeek-R1) uses verifiable rewards (code execution, math correctness) to induce chain-of-thought.
Tools and Multimodality
Models emit special tool-call tokens; external programs execute and return results as new tokens. Images are patchified into token sequences.
Scaling Laws
Cross-entropy follows power laws in parameters (N), data (D), and compute (C ≈ 6ND). Kaplan et al. (2020) and Hoffmann et al. (2022) show optimal N/D ratios. Chinchilla found irreducible loss ≈ 1.69 nat/token (≈2.4 bits), corresponding to language's intrinsic entropy.
Limitations
Hallucination : Training objective is plausibility, not truth; rare facts lack regularity; evaluation rewards guessing; autoregressive commitment amplifies errors.
Knowledge cutoff : Only knows pre-training data.
Context window limits : Quadratic attention cost; middle information often lost.
Character/arithmetic errors : Tokenization obscures characters; use code tools.
Sycophancy : RLHF learns to agree with users.
Associative, not causal : Learns directional correlations, not bidirectional logic.
Summary
The article answers a chain of questions: LM predicts next token; quality measured by cross-entropy; limit is language entropy; neural nets with depth, normalization, residuals approximate the function; gradient descent + backprop finds parameters; generalization = compression = finding reusable patterns; Transformer uses attention + FFN; autoregressive generation with sampling; post-training (SFT, RLHF, reasoning RL) creates assistants; tools/multimodality via tokenization; scaling laws drive size; limitations stem from next-token prediction paradigm.
Ten thousand human words spun into silk, layer upon layer the network weaves new phrases. Context reflects a thousand intents, probability weighs a single verse. Gradient back-propagation corrects old mistakes, human preferences also teach. Though every utterance becomes brocade, where lies true knowledge, where doubt?
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tencent Technical Engineering
Official account of Tencent Technology. A platform for publishing and analyzing Tencent's technological innovations and cutting-edge developments.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
