Chapter 1
LLM Fundamentals
NLP → Transformer → training → applications
Full path: 9-chapter hub ·
? RAG
Tap right · Space · F fullscreen
Scope
Chapter 1 of 9
- ? LLM — this deck
- ? RAG — look up first, then think
- ?—? Agent Core + Retrieval
- ?—? LangGraph, MCP/A2A, OpenClaw
- ?—? Multi-agent/KG, Multimodal
present-hub.html
Chapter 1
Why did LLMs emerge?
From NLP to LLM: why traditional AI was not enough
Ch 1
Two questions to start
Why did Siri feel dumb?
Short replies, lost context, brittle rules
Why did ChatGPT feel smart?
Long context, fluent reasoning, general tasks
Classical NLP: cannot model long-range context
Ch 1
NLP = everything is prediction
A Language Model predicts the next token given all previous tokens.
ChatGPT "understands" — but under the hood: next-token prediction, one token at a time.
| Task | What model predicts |
| Chat / QA | P(answer token | question + history) |
| Translation | P(target token | source + target so far) |
| ASR | P(text token | audio frames) |
Ch 1
Prediction loop
Ch 1
Four failures of traditional AI
Rules Infinite variation — rules break
ML Hand-crafted features; not end-to-end
RNN Serial, slow; distant tokens fade
CNN Local window; many layers for global view
Transformer (2017): any token ↔ any token — parallel & global
Ch 1
Long-range dependency
I grew up in Germany ... (50 words) ... I speak fluent ______
Ch 1 · Math
Language Model definition
$$\mathcal{P}(s_1 \ldots s_n) = \prod_{j=1}^{n} \mathcal{P}(s_j \mid s_1, \ldots, s_{j-1})$$
Modeling language = modeling next-token probability, repeatedly.
GPT approximates $\mathcal{P}(s_{t+1} \mid s_{1:t})$ with a Transformer.
Ch 1 · Example
Numeric example
Vocabulary {I, love, Paris}. Sentence "I love Paris":
$$\mathcal{P}(\text{I love Paris}) = \mathcal{P}(\text{I}) \cdot \mathcal{P}(\text{love}\mid\text{I}) \cdot \mathcal{P}(\text{Paris}\mid\text{I love})$$
$0.02 \times 0.12 \times 0.09 = \mathbf{0.000216}$
Generation reverses this: sample next token, append, repeat.
Chapter 2
Why Transformer changed the world
Before formulas — understand the revolution
Ch 2
RNN vs CNN vs Attention
| Arch | Pattern | Problem |
| RNN | T1 → T2 → ... → T50 | Serial; forgets distant tokens |
| CNN | Local windows | Cannot see global context in one layer |
| Attention | T1 ↔ T2 ↔ ... ↔ T50 | Direct links; parallel; global |
Token1 ↔ Token50 in one step, not 49 hops.
Ch 2
Attention: "Who matters most?"
Ch 2 · Math
Scaled dot-product attention
$$\text{Attention}(Q,K,V)=\text{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V$$
- Q Query — what am I looking for?
- K Key — how matchable is each token?
- V Value — what information do I take?
Ch 2 · Math
Self-attention with projections
$$\text{SelfAttn}(X)=\text{softmax}\!\left(\frac{XW_Q(XW_K)^\top}{\sqrt{d_k}}\right)XW_V$$
Mini example (cat, sat, mat) for "sat": scores → softmax [0.26, 0.58, 0.16]
Multi-Head = several $(W_Q,W_K,W_V)$ in parallel → syntax, coreference, semantics.
Ch 2 · Architecture
Three Transformer families
| Model | Stack | Examples |
| Encoder-Decoder | Both; cross-attention | Translation, Whisper |
| Decoder-only | Causal masked self-attn | ChatGPT, Llama |
| Encoder-only | Bidirectional | BERT, embeddings |
ChatGPT path: GPT = decoder-only, autoregressive next-token loop.
Ch 2 · GPT
GPT = Decoder-only
Ch 2 · Inference
How an LLM answers
Q: "What is the capital of France?"
- Tokenize → embed + positional encoding
- Forward through L Transformer layers
- LM Head → logits over vocabulary
- Softmax → P(next token | context)
- Pick token → "Paris"
- Append → repeat until <EOS>
Autoregressive: one token at a time.
Ch 2 · Example
Decode: predicting "Paris"
| Token | Paris | London | the | <EOS> |
| logit | 3.2 | 1.1 | 0.3 | -1.0 |
| softmax | 77.9% | 9.5% | 4.3% | 1.2% |
Context grows:
"Paris"
"."
<EOS>
$$\mathcal{P}_i=\frac{e^{z_i/T}}{\sum_j e^{z_j/T}} \quad (T=\text{temperature})$$
Ch 2 · Sampling
Inference parameters
| Param | Effect | Use when |
| temperature | Low = peaked; high = random | 0 facts/code; 0.7 chat; >1 creative |
| top-p | Sample from nucleus set | ~0.9 default |
| top-k | Keep k highest tokens | k=1 greedy; 40-50 chat |
| Token | T=0.1 | T=1.0 | T=2.0 |
| Paris | 99.2% | 77.9% | 52.1% |
Chapter 3
Why intelligence emerges
Emergent abilities & Scaling Laws
Ch 3
Emergence & scaling
< 1B params
Advanced autocomplete. Weak reasoning.
10B - 100B+
CoT, coding, planning, translation jump.
$$L(N,D) \approx \left(\frac{N_c}{N}\right)^{\alpha_N} + \left(\frac{D_c}{D}\right)^{\alpha_D}$$
Loss decreases predictably with params N, data D, compute C.
Ch 3
What is an LLM?
Definition: Transformer LM with 10B+ parameters, trained on massive text.
Solves tasks via text generation (next-token prediction at scale).
| Technique | Example |
| Scaling | GPT-1 117M → GPT-3 175B |
| Ability eliciting | CoT: "Think step by step" |
| Alignment | RLHF, DPO |
| Tool manipulation | API calls (see RAG course) |
Chapter 4
Modern LLM Training
Pretrain → SFT → RLHF
Ch 4 · Pretrain
Pretraining
$$\mathcal{L}_{\text{PT}} = -\frac{1}{N}\sum_{t=1}^{N} \log \mathcal{P}_\theta(x_t \mid x_{1:t-1})$$
Data: web crawl, books, Wikipedia, code. Distributed GPU training.
After pretrain: model continues text — cannot follow instructions yet.
Ch 4 · SFT
Instruction tuning (SFT)
After pretrain
Q: 2+2=?
May ramble about math...
After SFT
Q: 2+2=?
Answer: 4.
Train only on assistant reply tokens. Methods: Full FT, LoRA, QLoRA.
Ch 4 · RLHF
RLHF alignment
- SFT — imitation on human-written answers
- Reward model — humans rank responses; train RM
- PPO — maximize reward + KL penalty vs SFT
$$\max_\theta \; \mathbb{E}[r(x,y)] - \beta\, D_{\text{KL}}(\pi_\theta \,\|\, \pi_{\text{SFT}})$$
ChatGPT = Pretrain + SFT + RLHF. Eval: MMLU, win rate, TruthfulQA.
Chapter 1 complete
LLM Fundamentals done
Scroll: course-full-llm.html (appendix sections for agent preview)
Next: ? RAG
Full path: 9-chapter hub