Section 10.1.3 : LLMs and Transformer Architecture
π― Learning Objective
Understand how Large Language Models (LLMs) work and the Transformer architecture that underpins them. You will be able to explain the attention mechanism, the tokenization process, and the training steps of a modern LLM.
What Is a Large Language Model?
A Large Language Model (LLM) is a deep learning model trained on immense text corpora to understand and generate human language. The term "large" refers to both the model size (billions of parameters) and the amount of training data (trillions of tokens).
To better grasp the scope of an LLM, imagine an artificial neural network made up of billions of "tuning knobs" (the parameters). During training, these knobs are progressively adjusted β trillions of times β so the model becomes a language expert. Each parameter encodes a tiny piece of knowledge: a syntactic relationship, a semantic association, a statistical pattern of human language.
The Evolution of Model Sizes
| Model | Year | Parameters | Context Window | Key Innovation |
|---|---|---|---|---|
| GPT-2 | 2019 | 1.5 billion | 1,024 tokens | First "large" public LLM |
| GPT-3 | 2020 | 175 billion | 4,096 tokens | Emergent few-shot learning |
| PaLM | 2022 | 540 billion | 8,192 tokens | Scaling laws validated |
| GPT-4 | 2023 | ~1.8 trillion (estimated, MoE) | 128,000 tokens | Multimodal, reasoning |
| Llama 4 Maverick | 2025 | 400 billion MoE (~17B active) | 1M-10M tokens | Competitive open source, MoE |
| DeepSeek V3.1 | 2025 | 671 billion MoE | 128,000 tokens | MLA + MoE, reduced cost |
| GPT-5 | 2025 | Undisclosed | 256,000 tokens | Hybrid router, multi-model |
| Claude Opus 4.6 | 2026 | Undisclosed | 200,000 tokens | Extended thinking, safety |
The trend is no longer simply "bigger = better." Since 2024, the focus has shifted to efficiency: how to achieve the same performance with fewer active parameters (MoE), less memory (MLA), and lower training costs (GRPO).
The Core Principle: Predicting the Next Token
The fundamental operation of an LLM is surprisingly simple: predict the next token. Given a sequence of tokens, the model computes a probability distribution over all possible tokens and selects the most probable one (or samples from this distribution).
Concretely, if you give the model the sequence "The sun sets over the", it evaluates the probability of each token in its vocabulary: "sea" (12%), "city" (8%), "mountain" (6%), "plain" (3%)... and selects according to a sampling strategy controlled by the temperature and top-p parameters:
- βTemperature = 0: the model always picks the most probable token β deterministic, repetitive responses
- βTemperature = 0.7: good balance between coherence and creativity β standard usage
- βTemperature = 1.5: creative but potentially incoherent responses β brainstorming
Top-p (nucleus sampling) is complementary: rather than considering all tokens, only keep tokens whose cumulative probability reaches p (e.g., top-p = 0.9 β keep tokens representing 90% of the probability mass).
Tokenization: Splitting the Text
Before processing text, an LLM splits it into tokens β basic units that can be words, subwords, or characters. The most common method is BPE (Byte Pair Encoding):
- β"intelligence" β ["intelli", "gence"] (2 tokens)
- β"Chat" β ["Chat"] (1 token)
- β"GPT-4" β ["G", "PT", "-", "4"] (4 tokens)
- βAn emoji π€ β 1 special token
- β"unconstitutionally" β ["un", "constitu", "tion", "ally"] (4 tokens)
- β"Hello world" β ["Hello", " world"] (2 tokens β note the included space)
Why tokens matter to you: APIs charge per token (input + output). An average English word β 1.3 tokens. A 200-word email β 260 tokens. A 10-page document β 3,500 tokens. Optimizing the length of your prompts directly optimizes your costs.
GPT-5 uses a vocabulary of approximately 200,000 tokens. The context window has evolved considerably: 128,000 tokens for GPT-4 Turbo, then 1 million for Gemini 3.1 Pro, and up to 10 million tokens for Llama 4 Scout (Meta, April 2025) β the equivalent of dozens of books analyzed in a single conversation.
Context Window: Quantity vs Quality
Caution: a 10-million-token context window doesn't mean the model perfectly uses all that information. Studies show that LLMs suffer from "lost in the middle" β information in the middle of a long context receives less attention than information at the beginning and end. That's why placing the most important information at the beginning or end of your prompt remains best practice, even with large windows.
The Transformer Architecture in Detail
The Transformer, introduced by Vaswani et al. in the paper "Attention Is All You Need" (2017), is composed of stacked blocks, each containing:
1. Embeddings: each token is converted into a high-dimensional numerical vector (e.g., 12,288 dimensions for GPT-4). These vectors capture the semantic meaning of the token. Semantically similar words ("king" and "queen") will have close vectors in this space.
The embedding space is fascinating: it encodes algebraic relationships. The canonical example: vector(king) - vector(man) + vector(woman) β vector(queen). This means the model has learned conceptual relationships between words, not just their statistical co-occurrence.
2. Positional Encoding: the model adds position information to each token (the Transformer processes all tokens in parallel, unlike RNNs which process them sequentially). Modern techniques use RoPE (Rotary Position Embedding) which encodes relative position between tokens rather than absolute position, allowing better generalization on sequences longer than those seen during training.
3. The Attention Mechanism (Self-Attention): this is the heart of the Transformer. For each token, the mechanism calculates how much that token should "pay attention" to every other token in the sequence.
Technically, each token is transformed into three vectors: Query (Q β "what is this token looking for?"), Key (K β "what does this token contain?"), and Value (V β "what information to transmit?"). The attention score between two tokens = Qβ Β· Kβ (dot product). The higher the score, the more token 1 "pays attention" to token 2.
For example, in the sentence "The cat that belongs to Marie ate its kibble", to resolve "its" β the attention mechanism learns to link "its" to "cat" or "Marie" depending on context, by assigning a high attention score to the relevant token.
4. Multi-Head Attention: instead of a single attention mechanism, the Transformer uses several in parallel (e.g., 96 "heads" for GPT-4, even more for GPT-5). Each head learns to capture a different type of relationship:
- βSyntactic head: subject β verb
- βSemantic head: adjective β qualified noun
- βCoreference head: pronoun β antecedent ("its" β "cat")
- βPositional head: local proximity relationships
- βPattern head: recurring structures (lists, numbering)
5. Multi-Head Latent Attention (MLA): an innovation introduced by DeepSeek V2 (2024), MLA compresses attention keys and values through a latent space, drastically reducing required memory while maintaining quality. Instead of storing complete K and V vectors for each past token (memory-expensive), MLA projects them into a reduced-dimension space. This technique reduced DeepSeek's inference cost by ~5Γ compared to standard attention, with no measurable quality loss.
6. Feed-Forward Network (FFN): after attention, each token passes through an independent neural network (two linear layers with a non-linear activation). This is where the model transforms contextual information collected by attention into new representations. FFNs are responsible for the majority of the model's "factual knowledge" β deactivating specific neurons in the FFN can make the model "forget" facts.
7. Mixture of Experts (MoE): rather than activating all parameters for each token, the MoE architecture only activates a subset of specialized "experts." Llama 4 Maverick has 400 billion parameters but only activates ~17 billion per token, combining power and efficiency. DeepSeek V3 uses the same approach with "shared experts" always active and "routed experts" activated based on context.
A router (small neural network) decides which 2-4 experts (out of 128-256 available) to activate for each token. It's like a hospital where each patient is directed to the relevant specialists rather than consulting every doctor.
The 3 Training Stages of a Modern LLM
Stage 1: Pre-training (the most expensive β ~95% of compute budget)
The model learns to predict the next token on a massive corpus: books, scientific articles, websites (Common Crawl), code (GitHub), Wikipedia, etc. This is where the model "reads" the equivalent of all human knowledge accessible online.
Typical data sources and their roles:
| Source | Volume (approx.) | What the Model Learns |
|---|---|---|
| Common Crawl (web) | ~60% of corpus | Everyday language, factual knowledge |
| Books and publications | ~15% | Formal style, long-form reasoning |
| Source code (GitHub) | ~10% | Logic, structures, formal reasoning |
| Wikipedia and encyclopedias | ~5% | Structured facts, concept relationships |
| Conversations and forums | ~5% | Informal register, Q&A |
| Scientific data | ~5% | Specialized terminology, methodology |
Cost: for a frontier model like GPT-5, the estimated budget is $200 to $500 million, months of training on 10,000+ NVIDIA H100/H200 GPUs. Power consumption equals that of a small city for several months. DeepSeek shook the industry by demonstrating that a competitive model could be trained for ~$5.6 million through architectural innovations (MLA, efficient MoE).
Stage 2: Supervised Fine-Tuning (SFT)
The pre-trained model is fine-tuned on high-quality conversation examples written by humans. This is where the model learns to follow instructions, answer questions, and adopt a conversational format.
The SFT process typically uses 50,000 to 500,000 conversation examples carefully written by qualified human annotators. Each example illustrates desired behavior: being helpful, honest, refusing dangerous requests, structuring responses, citing sources when possible.
Stage 3: Alignment through Reinforcement
Two main approaches coexist in early 2026:
- β
RLHF (Reinforcement Learning from Human Feedback): human evaluators rank multiple model responses by quality. A reward model is trained, then the LLM is fine-tuned to maximize this reward. Used by OpenAI (GPT-5), Anthropic (Claude 4.6). The typical RLHF process: generate 4-8 responses per prompt β human ranking β train the reward model β optimize via PPO (Proximal Policy Optimization).
- β
GRPO (Group Relative Policy Optimization): a DeepSeek innovation, this method eliminates the need for a separate reward model by comparing the model's responses against each other. Major advantage: the reward signal is self-contained β the model learns by comparing its own outputs without human evaluators or an external reward model. More economical and used to train DeepSeek-R1, which demonstrated remarkable emergent reasoning capabilities.
- β
DPO (Direct Preference Optimization): a simplified approach to RLHF that avoids training a separate reward model. The reward function is implicit in the optimization objective. Used by Meta for Llama and by several academic teams.
Pre-training vs Fine-tuning: An Analogy
Pre-training is like a child's general education (reading, writing, general knowledge). Fine-tuning is like professional training (doctor, lawyer, engineer). RLHF is like field experience + mentorship (patient/client feedback + senior guidance). The base model is "intelligent but wild" β fine-tuning and RLHF make it "intelligent AND useful."
Emergent Phenomena in LLMs
As models grow larger, emergent capabilities appear β not explicitly programmed but arising at a certain scale:
Chain-of-thought (step-by-step reasoning): large models can, when asked to "think step by step," break down complex problems into logical sub-steps. Reasoning models (o3, R1, Claude Opus 4.6 extended thinking) push this phenomenon to the extreme with chains of thought spanning thousands of tokens.
In-context learning: LLMs can learn new patterns simply from examples provided in the prompt (few-shot), without modifying their weights. A model that has never seen a specific data format can understand it after 2-3 examples in the prompt.
Instruction following: the ability to follow complex, multi-step instructions appears around 10-100 billion parameters and improves significantly with scale.
Self-correction: the most recent models can detect and correct their errors when invited to reread their own response β this is the principle of "self-reflection" or "critique" used in agentic architectures.
LLM Limitations
Despite their impressive performance, LLMs have fundamental limitations β even though some are being progressively pushed back:
Hallucinations: LLMs sometimes generate false but plausible information. Reasoning models (o3, Claude Opus 4.6, Gemini Deep Think) reduce this problem by "thinking" before responding, but don't eliminate it. Typical hallucination rate: 3-15% depending on the domain and model.
Context window: even though it's expanding considerably (128K for GPT-4, 2M for Gemini 3.1, 10M for Llama 4 Scout), the model cannot consider more information than its window. Techniques like RAG (Retrieval-Augmented Generation) compensate by connecting the model to external knowledge bases.
Reasoning: classical models simulate reasoning via statistical shortcuts. New "thinking" models (o3, DeepSeek-R1, Claude Opus 4.6 in extended thinking mode) significantly improve multi-step reasoning, but still fail on truly novel problems or reasoning requiring deep causal understanding.
Cost and energy: training a frontier model costs hundreds of millions of dollars and consumes the energy equivalent of a small city for months. Large-scale inference also poses environmental challenges. The trend toward more efficient models (MoE, distillation) is partly motivated by these constraints.
Safety and alignment: ensuring models act in accordance with human intentions remains an open challenge, particularly as AI agents gain autonomy. The alignment problem grows with capabilities: the more powerful a model, the more severe the consequences of misalignment.
Creative sterility: LLMs excel at reformulation and combining existing ideas, but rarely create something truly new. Their output is a statistical interpolation of what already exists in their training data β "true" creativity remains a subject of debate.
Practical Exercise: Exploring LLM Parameters
Duration: 15 minutes
- βOpen ChatGPT or Claude and ask the same question 3 times with different temperatures (if available via the API or settings)
- βCompare the responses: which is the most factual? The most creative? The most incoherent?
- βCount the tokens in your prompt using a tool like tokenizer.chat or tiktokenizer.vercel.app
- βTest context sensitivity: place an important instruction at the beginning, middle, then end of a long prompt. Does the model still follow it?
What is the fundamental operating principle of an LLM?
What are the 3 training stages of a modern LLM like GPT-5?
What does the Mixture of Experts (MoE) architecture mean?