Phase 2 — LLM and Generative AI Fundamentals
This phase explains how large language models actually work, from the mathematics up to the APIs you will call. It is the conceptual core of the whole book: RAG, agents, evaluation, and security all become much easier once you understand what a model is doing when it produces a token.
You do not need a machine-learning background. Every idea is built from the ground up, and the goal is interview readiness, not research. By the end you should be able to explain attention, tokenization, sampling, fine-tuning, and tool calling clearly and correctly.
What you will be able to do
By the end of this phase you should be able to:
- Explain how a neural network learns, and what a transformer is doing layer by layer.
- Describe tokenization, embeddings, the context window, and the KV cache.
- Explain how a model turns logits into text through softmax and sampling, and how temperature, top-k, and top-p change the output.
- Distinguish pretraining, fine-tuning, instruction tuning, RLHF, and parameter-efficient methods like LoRA.
- Explain precision formats (FP32/FP16/BF16/INT8/INT4) and quantization trade-offs.
- Use structured outputs, JSON schema, and function calling correctly and safely.
- Compare models and providers, and know when to use a hosted model versus an open-source one.
How the topics fit together
flowchart TD
A["How ML and neural nets work"] --> B["Tensors and PyTorch"]
B --> C["Forward pass and backpropagation"]
C --> D["Attention and self-attention"]
D --> E["The transformer architecture"]
E --> F["Tokenization and tokens"]
F --> G["Embeddings"]
G --> H["Context windows and the KV cache"]
H --> I["Logits, softmax, next-token prediction"]
I --> J["Sampling: temperature, top-k, top-p"]
J --> K["Training vs inference and batching"]
K --> L["Pretraining, fine-tuning, instruction tuning"]
L --> M["RLHF and preference alignment"]
M --> N["LoRA, QLoRA, PEFT"]
N --> O["Numeric precision and quantization"]
O --> P["Structured outputs and JSON schema"]
P --> Q["Function and tool calling"]
Q --> R["Streaming responses"]
R --> S["Prompt design and system prompts"]
S --> T["Context engineering"]
T --> U["Hallucinations"]
U --> V["Prompt injection and context poisoning"]
V --> W["Model comparison and selection"]
W --> X["Provider APIs: OpenAI, Anthropic, Gemini"]
X --> Y["Open-source models and Hugging Face"]
Topic order
Work through these in order. Each topic is one concept, and merged roadmap bullets are covered inside the relevant page.
- How machine learning and neural networks work — fitting a function from data.
- Tensors and PyTorch — the data structure everything is built on.
- Forward pass and backpropagation — how a network learns.
- Attention and self-attention — how tokens look at each other.
- The transformer architecture — multi-head attention, positional encoding, and layers.
- Tokenization and tokens — turning text into numbers.
- Embeddings — turning meaning into geometry.
- Context windows and the KV cache — the model’s working memory.
- Logits, softmax, and next-token prediction — how a model picks the next token.
- Sampling: temperature, top-k, top-p — controlling randomness.
- Training vs inference and batching — the two modes and how they differ.
- Pretraining, fine-tuning, and instruction tuning — the three-stage story.
- RLHF and preference alignment — teaching models to be helpful.
- Parameter-efficient fine-tuning: LoRA, QLoRA, PEFT — adapting big models cheaply.
- Numeric precision and quantization — FP32, FP16, BF16, INT8, INT4.
- Structured outputs and JSON schema — reliable machine-readable answers.
- Function and tool calling — letting the model act.
- Streaming responses — better latency for users.
- Prompt design and system prompts — structuring the input.
- Context engineering — deciding what the model sees.
- Hallucinations — why models confidently invent things.
- Prompt injection and context poisoning — the core LLM threat.
- Model comparison and selection — choosing the right model.
- Provider APIs: OpenAI, Anthropic, Gemini — the practical interfaces.
- Open-source models and Hugging Face — running and using open models.
Tip:
How to study this phase. These ideas build on each other. If attention feels unclear, reread topics 1–3 rather than pushing forward; attention is just a weighted lookup built on the same forward/backward machinery as an ordinary network.
Checkpoint project
At the end of the phase, extend Project 2 — Grounded knowledge assistant with a small model component: call two providers through one interface, produce a structured (validated) answer, stream it, and compare model behaviour on a fixed set of prompts. The exact scope lives in the projects part of the book.