Skip to content
datarekha

Course · 8 chapters · 73 lessons

Generative AI

What an LLM is and isn't, the autoregressive loop, structured outputs, RAG that doesn't break, evals that mean something, and self-hosting at the edge of the GPU.

The Generative AI journey 0 / 73 completed
  1. Chapter 01

    Foundations

    6 lessons
  2. 01 What an LLM is A demystified, no-magic explanation of large language models — what they actually do, and what that implies for how you build with them. Beginner6 min
  3. 02 The autoregressive loop How an LLM actually generates text, one token at a time — and what that means for streaming, latency, and your UX. Beginner5 min
  4. 03 Tokens, chat templates and budgets Understand the units that determine an LLM request's cost, latency, context fit, and truncation behaviour. Beginner12 min
  5. 04 Sampling: temperature, top-k, top-p How an LLM turns next-token probabilities into an answer, what temperature, top-k, and top-p actually change, and how to choose settings without fo… Intermediate6 min
  6. 05 Long context: windows, costs and failure modes A practical guide to what an LLM context window contains, what long prompts cost, and why more tokens can still produce worse answers. Intermediate12 min
  7. 06 Structured outputs How to make an LLM emit valid JSON every time using Pydantic, JSON Schema, and constrained generation. Intermediate7 min
  8. Chapter 02

    Prompting & Tools

    5 lessons
  9. 07 Prompt patterns that work Anthropic's priority-ordered prompt techniques — clarity, multishot, chain of thought, XML tags, document ordering, prefill, and prompt caching. Pl… Beginner7 min
  10. 08 Few-shot & chain-of-thought The two prompting techniques that still earn their keep — when to use few-shot examples, when to ask for reasoning, and when neither helps. Intermediate6 min
  11. 09 Reasoning models & test-time compute Reasoning models spend extra inference compute on hard problems before answering. Learn how test-time scaling works, how to tune thinking budgets,… Intermediate8 min
  12. 10 Function/tool calling How LLMs invoke functions — the schema contract, the request/response loop, and the production patterns for tool use. Intermediate8 min
  13. 11 Constrained decoding How structured generation guarantees grammar-valid output by masking illegal tokens at each decode step, and what that guarantee does not cover. Advanced8 min
  14. Chapter 03

    RAG & Adaptation

    12 lessons
  15. 12 Embeddings What an embedding actually IS — a point in semantic space. Distance metrics, dimensionality, and clustering short texts by meaning. Beginner6 min
  16. 13 Sentence embeddings: SBERT A cross-encoder can judge two sentences accurately, but it must do so again for every candidate. SBERT turns each sentence into a reusable vector w… Intermediate7 min
  17. 14 Multimodal (vision & audio) LLMs How multimodal models turn images, audio, and video into representations an LLM can reason over, why resolution drives token cost, and the producti… Intermediate8 min
  18. 15 Vector databases Why a vector DB beats a numpy array at scale. HNSW, metadata filters, and the honest answer: most teams should start with pgvector. Intermediate9 min
  19. 16 RAG basics Retrieval-augmented generation: chunk, embed, store, retrieve, generate. The single most important pattern for any LLM app that touches private data. Intermediate8 min
  20. 17 OCR & document AI for RAG Turn scanned and digital documents into text a RAG system can trust: when to skip OCR entirely, classic engines vs layout pipelines vs vision-langu… Intermediate12 min
  21. 18 Chunking for RAG A retriever cannot find context that your chunks destroyed. Learn how chunk size, overlap, document structure, semantic boundaries, and parent-docu… Intermediate7 min
  22. 19 Advanced RAG Contextual retrieval (Anthropic's 49% recall win) plus the supporting techniques — hybrid retrieval with RRF, query rewriting (HyDE, multi-query),… Advanced9 min
  23. 20 Vectorless retrieval (PageIndex) Retrieval without embeddings: PageIndex turns a document into a tree the LLM reasons down. Learn when it beats a vector database, where it fails, a… Intermediate8 min
  24. 21 RAG evaluations How to actually measure RAG quality. Retrieval metrics (recall@k, MRR, hit rate) vs generation metrics (faithfulness, answer relevance, context pre… Advanced7 min
  25. 22 Fine-tune vs RAG: the decision RAG supplies changing facts at query time; fine-tuning changes a model's behavior. A practical framework for choosing between prompts, retrieval, a… Intermediate8 min
  26. 23 Fine-tuning: LoRA & QLoRA Full fine-tuning rewrites billions of weights you can't afford to store. LoRA freezes the model and trains a tiny low-rank 'diff' instead — here's… Advanced9 min
  27. Chapter 04

    How LLMs are built & trained

    7 lessons
  28. 24 Pretraining data: curation & dedup A model is what it eats. The internet is mostly junk, so the gap between a mediocre LLM and a frontier one is as much about the data pipeline — ext… Advanced9 min
  29. 25 Synthetic data and model collapse How generated examples become useful training material, and how recursive reuse can erase the rare cases your model needs. Intermediate13 min
  30. 26 Mixture of Experts How do you get a trillion-parameter model that costs like a 13B one to run? Don't use all the parameters for every token. That's the MoE trick behi… Advanced8 min
  31. 27 Alignment: SFT, RLHF & DPO A freshly pretrained model completes text — it doesn't answer you. Three post-training stages turn that document-predictor into a helpful assistant… Advanced9 min
  32. 28 Direct Preference Optimization (DPO) RLHF aligns models with a reward model and a finicky RL loop. DPO throws both away — deriving, from the same preference data, a single classificati… Advanced10 min
  33. 29 Constitutional AI & RLAIF Training a model to be harmless usually means humans reading piles of toxic text to label it. Constitutional AI replaces that human feedback with t… Advanced9 min
  34. 30 RLVR — reinforcement learning from verifiable rewards Replace the learned reward model with a program that can actually check the answer. It removes the hackable middleman, and it is why reasoning mode… Advanced12 min
  35. Chapter 05

    Evaluation & Safety

    11 lessons
  36. 31 LLM evals & LLM-as-judge Vibes don't ship. How to build eval suites that catch regressions, when to use an LLM as a grader, and how to stop position, verbosity, and self-pr… Intermediate8 min
  37. 32 Hallucination & grounding Understand why LLMs produce confident errors, how grounding differs from truth, and how to measure support claim by claim instead of trusting a ple… Intermediate7 min
  38. 33 Prompt injection & guardrails Why LLMs can't tell instructions from data, the OWASP #1 risk, and the types of guardrails — input, output, instruction-hierarchy, and least-privil… Intermediate8 min
  39. 34 Guardrails & output validation An LLM is a probabilistic text generator wired into deterministic systems that expect valid JSON, no leaked PII, and on-policy answers. A guardrail… Advanced9 min
  40. 35 Llama Guard: safety classification Llama Guard is a dedicated safety model that classifies prompts, responses, and multimodal content against a hazard taxonomy. Learn its output form… Intermediate7 min
  41. 36 Reward hacking & Goodhart's law Alignment introduced reward hacking as the 'proxy risk' of preference training. This is the full picture: optimize any measurable proxy hard enough… Advanced8 min
  42. 37 Red-teaming LLMs Red-teaming is adversarial testing for an LLM system: attack the model, prompts, retrieval, and tools before real users do, then turn every breakth… Advanced8 min
  43. 38 A taxonomy of jailbreaks A jailbreak makes an aligned model do what its safety training forbids — and unlike prompt injection, the adversary is the user, attacking the mode… Advanced8 min
  44. 39 Watermarking AI content A practical guide to AI watermarking: how statistical text watermarks bias token sampling, how detectors use green-token counts and z-scores, how i… Advanced8 min
  45. 40 Differential privacy for LLMs A model can memorize rare training examples and leak names, medical details, or private text. Differential privacy limits how much any one protecte… Advanced8 min
  46. 41 Bias & fairness in LLMs Classical fairness asks whether a model's decisions treat groups fairly. LLMs add a language-shaped problem: their outputs can stereotype, demean,… Advanced8 min
  47. Chapter 06

    Frontier safety

    7 lessons
  48. 42 Mesa-optimization & inner alignment Underdetermination can produce a proxy or shortcut. Mesa-optimization is the narrower case in which a learned system contains an internal optimizat… Advanced7 min
  49. 43 Evidence for deceptive alignment Deceptive alignment is the possibility that a model behaves well during training and evaluation while preserving a different objective for deployme… Advanced7 min
  50. 44 AI control When a capable model may be scheming and behavioral tests cannot prove otherwise, AI control designs a protocol that limits harm anyway. Learn how… Advanced7 min
  51. 45 Scalable oversight A model can become better at solving a task than the humans judging it. Scalable oversight uses debate, decomposition, weak-to-strong training, and… Advanced7 min
  52. 46 Frontier safety frameworks How do labs decide what safeguards a dangerous model needs? Frontier safety frameworks turn that judgment into a precommitted process: define dange… Advanced7 min
  53. 47 Recursive self-improvement Recursive self-improvement is a system improving its own ability to improve. See why that can compound into an intelligence explosion, how takeoff… Advanced7 min
  54. 48 Model welfare Could an AI system itself have experiences that matter morally? Model welfare is the careful study of that possibility: what evidence could bear on… Advanced6 min
  55. Chapter 07

    Operations

    16 lessons
  56. 49 KV cache & continuous batching The KV cache is why LLM serving is memory-bound, not compute-bound. How caching keys and values makes generation fast, and how PagedAttention plus… Advanced8 min
  57. 50 KV cache offloading & memory tiers When the KV cache outgrows GPU VRAM, move idle cache blocks through CPU RAM and storage without confusing extra capacity for free speed. Advanced8 min
  58. 51 Cost & latency engineering Input vs output token economics, time-to-first-token, prompt caching, the router pattern, and semantic caching — the levers that move your bill by… Advanced7 min
  59. 52 Inference metrics: TTFT, ITL & goodput 'Fast' and 'high throughput' are different, often opposing numbers. To run an LLM service you need the operator's metrics — time-to-first-token, in… Advanced8 min
  60. 53 Model routing & cascades Most queries are easy. Routing only the hard ones to an expensive model — and cascading cheap-first — cuts LLM cost 45–85% at near-equal quality. T… Intermediate7 min
  61. 54 Self-hosting with vLLM When to run your own model and when to stay on an API. vLLM, quantization (GPTQ, AWQ, GGUF, bitsandbytes), VRAM math, and the honest break-even wit… Advanced8 min
  62. 55 Disaggregated serving (prefill/decode) Prefill is compute-bound, decode is memory-bound — yet a normal server runs both on the same GPU, where they fight. Disaggregated serving splits th… Advanced8 min
  63. 56 Quantization A 70B model needs 140 GB in full precision and zero consumer GPUs can hold it. Quantize it to 4-bit and it fits in 40 GB — for a few points of accu… Advanced8 min
  64. 57 GGUF & running LLMs locally Run LLMs on your own machine: understand GGUF, llama.cpp, quantization tiers, memory budgets, chat templates, and the failure modes that make local… Intermediate9 min
  65. 58 Choosing open-weight models: licences, lineage & residency How to pick an open-weight model for a company: what 'open weights' does and does not mean, how licences differ, why a model's base-model lineage m… Intermediate11 min
  66. 59 Distillation Quantization shrinks a model's weights. Distillation trains a brand-new, smaller model to copy a big one's behaviour — fewer layers, genuinely fast… Advanced9 min
  67. 60 On-policy distillation Classic distillation trains the student on text the teacher wrote. On-policy distillation trains it on text the student wrote, graded by the teache… Advanced11 min
  68. 61 Speculative Decoding LLMs generate one token at a time, which is slow. Speculative decoding lets a small model sprint ahead and a big model check its work in one shot —… Advanced8 min
  69. 62 Test-time compute — buying accuracy at inference You can spend more compute at inference instead of more parameters at training. Sampling many answers, revising one answer, and searching with a ve… Advanced11 min
  70. 63 Small language models and on-device inference How to choose, quantize, distil, evaluate, and run a small language model where a large one is wasteful. Intermediate12 min
  71. 64 Continuous batching and the inference scheduler How an in-flight scheduler keeps variable-length generations moving without turning KV memory and tail latency into surprises. Intermediate12 min
  72. Chapter 08

    Systems Design at Scale

    9 lessons
  73. 65 Async vs sync — handling concurrency Why an LLM API serves 10,000 waiting requests on one thread — and when async helps, when it doesn't. Advanced9 min
  74. 66 System design boundaries Choose correctly between stateless and stateful services, Lambda and ECS, databases and caches, queues and streams, retrieval and reranking, and mo… Intermediate13 min
  75. 67 Load balancing LLM inference One GPU replica can't serve 10,000 users. How to spread LLM traffic across replicas — and why round-robin is the wrong default. Advanced9 min
  76. 68 Rate limiting & denial-of-wallet How to stop one client from running up a five-figure token bill on your inference API — the attack that targets your wallet, not your uptime. Advanced9 min
  77. 69 Caching: exact, semantic & prompt Your embedding API keeps answering the same question. Three kinds of cache that cut LLM cost and latency — and the one that can silently return wro… Advanced9 min
  78. 70 AI gateways When several applications call several model providers, an AI gateway gives them one controlled front door for routing, fallback, retries, budgets,… Advanced8 min
  79. 71 Queues & batch pipelines 50,000 documents won't go through your summarization pipeline in one request. How queues, workers, retries, and backpressure get the job done. Advanced9 min
  80. 72 Circuit breakers & resilience Your database (or model provider) goes down mid-query. How timeouts, retries, circuit breakers, and fallbacks stop one failure from taking down eve… Advanced9 min
  81. 73 LLM serving ops An LLM endpoint is still a production service — but GB-sized weights, minutes-long cold starts, GPU scarcity, and token-based capacity break the us… Advanced9 min
  82. End of section 0 / 73 complete

    Make it stick — pass every quiz.

    Each lesson has a short quiz at the bottom. Passing the quiz is what marks the lesson complete and counts toward your certificate.

FAQCommon questions

Generative AI — frequently asked questions

Straight answers to the questions people ask most about generative ai.

What actually is a large language model?

An LLM is a neural network (a transformer) trained to predict the next token in text. From that single objective, at scale, it learns grammar, facts, reasoning patterns, and style, then generates by predicting one token at a time. It doesn't look things up — it produces statistically likely continuations.

What is RAG and why use it?

RAG (Retrieval-Augmented Generation) retrieves relevant documents and feeds them into the model's context so it answers from your data instead of only its training. It reduces hallucination, lets you use private or up-to-date information, and avoids the cost of retraining the model.

Read the lesson
What do temperature, top-k, and top-p control?

They control randomness in generation. Temperature scales how sharply the model favors high-probability tokens — low is focused and near-deterministic, high is creative and riskier. Top-k and top-p (nucleus) limit sampling to the most likely tokens. Use low temperature for factual tasks, higher for brainstorming.

Read the lesson
Why does an LLM hallucinate, and how do I reduce it?

Because it generates plausible continuations rather than retrieving facts, it can state confident falsehoods, especially outside its training data. Reduce it by grounding answers with RAG, asking for citations, lowering temperature, and constraining the task — but you can't eliminate it, so verify critical outputs.

What's the difference between fine-tuning and RAG?

Fine-tuning adjusts the model's weights to change its style or specialise its behavior; RAG leaves the model unchanged and supplies knowledge at query time. Use RAG for facts that change or are private, and fine-tuning for consistent format, tone, or task behavior — they're often combined.