Leanpub Header

Skip to main content

Foundations of Large Language Models

What the model is actually doing — and why every certification asks about it differently

This book is 100% completeLast updated on 2026-08-13

Five certifications ask about the same twenty topics, and each one stops at a different

depth. This book teaches the topic once, properly, then shows you exactly where each exam

stops — with 61 original practice questions where every wrong option is explained.

Every number in it was measured. Every line of code was run. The code is included.

Minimum price

$16.00

$16.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

There are five serious certifications in LLM engineering, and between them they ask about

the same twenty or so topics. But each one cuts every topic off at a different depth. One

exam wants you to know that a tokeniser splits text into pieces. Another wants you to know

why that splitting makes the model bad at counting letters. A third wants to know what it

costs you per request. So the candidate ends up assembling each topic from five different

sources that use five different names for the same thing — and never quite knows whether

they have the whole picture or a fragment of it.

This book is organised the other way round. The chapter is the topic, not the exam. Each

chapter teaches one subject properly, once, from the ground up — then shows you exactly

where each certification stops, what it calls things, and what it will ask you. Chapter 10

maps every certification back to the chapters it needs, in the order to read them.

It goes about one and a half steps deeper than the exams ask, and stops one and a half

steps short of research. Take the attention mechanism: the exam wants to know what the

query, key and value vectors are for. This book also shows you why the scores are divided

by the square root of the head dimension, and what visibly happens to the output when you

remove that division — because knowing the mechanism is what lets you answer a question

phrased in a way you have never seen, which is what these exams are largely made of.

And everything in it was measured rather than asserted. A working language model is built

from counts in forty lines and then broken, so you can see the exact failure that

transformers were invented to fix. Byte-pair encoding is trained from scratch. Attention,

a full transformer block, and a small decoder are implemented in NumPy and trained — on a

single CPU core, so you can reproduce every number rather than take it on trust. The

measurements that did not cooperate were kept: attention that averaged instead of routing,

an ablation where removing a component changed nothing, a bigger model that generalised

worse. Each of those is more instructive than a clean result, and together they teach the

habit that matters most on these exams and in production: a measurement describes a regime.

What you get:

- 10 chapters covering tokenisation, embeddings, attention, the transformer block, model

families, context windows and the KV cache, decoding and sampling, and the training

lifecycle.

- 61 original practice questions, tagged by certification and by difficulty, where every

option is explained — not just why the right answer is right, but why each wrong answer

is wrong, because on these exams the distractors are where the teaching is.

- A reverse map from each certification to the chapters that serve it, in reading order.

- All the code and experiment scripts, so every table and figure in the book can be

regenerated.

Written to the published objectives of NVIDIA NCA-GENL, NVIDIA NCP-GENL, NVIDIA NCP

Agentic AI, AWS Certified Generative AI Developer – Professional, and Databricks Certified

Generative AI Engineer. Objectives were checked in August 2026; confirm the current

blueprint with the certifying body before you sit.

You need to be able to read Python. That is genuinely all — neural networks, transformers

and the necessary linear algebra are built up from scratch where they are needed.

Every question in this book is original, written from published exam objectives. Nothing is

reproduced from, or based on recollection of, any live examination.

This book was created through a process that combines careful human planning, content direction, and advanced AI technology, followed by thorough refinement and review to ensure a high-quality final work.

Author

About the Author

Hatem M.

Hatem M. is a programmer and technical author whose work focuses on modern C++, large language models, and AI systems.

His books combine first-principles explanations with complete implementations and reproducible experiments. They include C++ Algorithmic Mastery, an eight-volume series on algorithms and problem solving; Build an LLM Inference Engine in C++, which constructs a GPT-style inference engine from scratch; LLM Quantization: From the Bits Up, which develops the theory and practice of neural network quantization from the bit level upward; and C++ Autopsy, a forensic investigation of ten subtle C++ bugs that compiled successfully, ran correctly, and still produced the wrong answers.

Contents

Table of Contents

Contents
  • 1. What a Language Model Actually Does
    • Prediction, not retrieval
    • Generation is this, in a loop
    • Measuring a language model: perplexity
    • More context should be better. Measure whether it is.
    • The one knob that survives: temperature
  • 2. Tokenisation: Deciding What a Token Is
    • Why not words
    • Byte-pair encoding, built from scratch
    • What the vocabulary size buys
    • What tokenisation does to the model's world
    • The cost of being out of distribution
  • 3. Embeddings: Giving Tokens a Geometry
    • The representation we are replacing
    • Where the positions come from
    • Measuring similarity: three metrics, three answers
    • Does this actually solve Chapter 1's problem?
    • What embeddings still cannot do
  • 4. Attention: Letting Context Choose
    • The mechanism
    • Why divide by {sqrt{dk
    • The causal mask
    • Multiple heads
    • What attention actually bought, measured
    • Why the small model gains so little
  • 5. The Transformer Block
    • The shape of a block
    • The residual path
    • Normalisation
    • The feed-forward network
    • Removing one piece at a time
    • What the block did to attention
  • 6. Architectures and Model Families
    • The experiment that separates the families
    • Three families, three visibility rules
    • What scale buys, and what it does not
    • Mixture of experts
    • Reading a model card
  • 7. Context Windows and the KV Cache
    • The quadratic term, measured
    • Why generation would be unaffordable without a cache
    • The cache is not free
    • What ``context length'' actually promises
  • 8. Decoding and Sampling
    • The experiment
    • Why maximising probability fails
    • Sampling, and what temperature really does
    • Truncation: top-{k
    • Choosing settings in practice
  • 9. The Training Lifecycle
    • What training actually looks like
    • Stage one: pre-training
    • Stage two: fine-tuning, and what it costs you
    • Stage three: alignment
    • Reading the lifecycle on a model card
  • 10. Capabilities, Limits, and Where Each Exam Goes
    • The limits, and what causes each
    • What these systems are reliably good at
    • The reverse map: your certification to these chapters
    • How to use the questions
    • What this volume did, and what it did not

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub