Build a GPT from Scratch Cover

BY MICHAEL BRENNDOERFER

Build a GPT from Scratch

Build a small GPT in PyTorch, one step at a time, from transformer math to training and text generation

Read in order, or browse the contents to find the topic you need.

Chapters in progress
Browse contents

About Build a GPT from Scratch

Build a GPT from Scratch takes one small language model from text input to generated output. We work through each tensor operation, connect it to the underlying math, and implement the model in readable PyTorch.

Explore the chapters

Browse the parts and chapters of Build a GPT from Scratch. Read from the beginning, or choose the topic you need.

Chapters are in progress. They will appear here as they are published.

No chapters in this volume have been published yet.

Contents · 0 chapters

More about the book

The book is inspired by Andrej Karpathy's educational work, especially nanoGPT and microGPT. The aim is not to copy those projects line by line. The aim is to explain the tensor shapes and mathematical decisions underneath them so that the implementation becomes readable.

We start with the mechanics that make transformer code less mysterious: shape notation, matrix multiplication, embedding lookups, dot products, and softmax. From there, self-attention becomes a sequence of understandable operations rather than a single block of code to memorize.

By the end, you should be able to trace a tiny GPT from text input to generated output: tokens become vectors, attention mixes context, cross-entropy measures next-token mistakes, backpropagation computes gradients, AdamW updates parameters, and sampling turns logits back into text.

On Michael’s writing

Feedback on Michael’s writing

Genuine feedback, with names abbreviated for privacy.

These comments concern Michael’s online writing and other handbooks, not this new PDF edition.

Meet the community
“I am enjoying reading your writings. I appreciate the effort you put into it. I want to drop a note of thanks. Thank you!”
Krishnan S.Researcher
“I would like to thank you for your work. Specifically, the chapter: 'LIME Explainability: Complete Guide to Local Interpretable Model-Agnostic Explanations' was extremely helpful for me in understanding LIME and communicating to colleagues. Your work is inspiring and valuable. Thank you for doing it.”
Will G.Data Scientist
“I just came across your website where your books are available for free to read. Thank you so much for making this available to the public. This will help a lot of people.”
Vikas C.AI/ML Engineer
“I found your book Language AI Handbook's explanations of Transformer architectures and production deployment strategies to be exceptionally clear and insightful”
San Z.Researcher
“Your writing feels like a treasure trove of great material. So, thank you for compiling everything on your website!”
Griffin P.Quantitative Researcher
“Really appreciate the way you have explained the concepts. Simple to understand, yet builds up knowledge exponentially, all while reinforcing it with examples repeatedly. I've read the last two chapters `74.Self-attention & 75.Q,K,V` and I've to admit, my concepts have never been clearer. You really are doing an amazing job breaking down maths and complex concepts like a story!”
Muhammad H.AI/ML Engineer
“Thank you Michael. It's top!”
David X.Data Scientist
“I read a few of your blogs on NLP and oh WOW, they really are something, now I exactly know how all of the terms Entropy, Cross-entropy and how it is connected to Perplexity, will definitely read more of your blogs.”
Kanishk K.Reader
“I came across your collection of online handbooks covering quantitative finance, AI, and machine learning. I wanted to reach out and thank you for making such high-quality material openly available. They are incredibly helpful for my CFA Level II review and for brushing up ahead of ML interviews.”
Karim Z.Reader
“Thanks so much for establishing such an amazing community. I appreciate the opportunity to be a learner and a contributor to the community.”
Cyrille L.AI/ML Engineer
“Thanks for putting cool stuff out into the world.”
Charlie B.Applied AI
“I came across one of your books, Language AI Handbook, and man, I'm loving it. I've read many books but have never found such a comprehensive book on large models that covers everything from start to finish with such depth.”
Uon L.Reader
Read all 12 reader reviews
“I would like to thank you for your work. Specifically, the chapter: 'LIME Explainability: Complete Guide to Local Interpretable Model-Agnostic Explanations' was extremely helpful for me in understanding LIME and communicating to colleagues. Your work is inspiring and valuable. Thank you for doing it.”
Will G.Data Scientist
“Really appreciate the way you have explained the concepts. Simple to understand, yet builds up knowledge exponentially, all while reinforcing it with examples repeatedly. I've read the last two chapters `74.Self-attention & 75.Q,K,V` and I've to admit, my concepts have never been clearer. You really are doing an amazing job breaking down maths and complex concepts like a story!”
Muhammad H.AI/ML Engineer
“Thanks so much for establishing such an amazing community. I appreciate the opportunity to be a learner and a contributor to the community.”
Cyrille L.AI/ML Engineer
“I just came across your website where your books are available for free to read. Thank you so much for making this available to the public. This will help a lot of people.”
Vikas C.AI/ML Engineer
“I am enjoying reading your writings. I appreciate the effort you put into it. I want to drop a note of thanks. Thank you!”
Krishnan S.Researcher
“Thank you Michael. It's top!”
David X.Data Scientist
“Thanks for putting cool stuff out into the world.”
Charlie B.Applied AI
“I found your book Language AI Handbook's explanations of Transformer architectures and production deployment strategies to be exceptionally clear and insightful”
San Z.Researcher
“Your writing feels like a treasure trove of great material. So, thank you for compiling everything on your website!”
Griffin P.Quantitative Researcher
“I came across your collection of online handbooks covering quantitative finance, AI, and machine learning. I wanted to reach out and thank you for making such high-quality material openly available. They are incredibly helpful for my CFA Level II review and for brushing up ahead of ML interviews.”
Karim Z.Reader
“I read a few of your blogs on NLP and oh WOW, they really are something, now I exactly know how all of the terms Entropy, Cross-entropy and how it is connected to Perplexity, will definitely read more of your blogs.”
Kanishk K.Reader
“I came across one of your books, Language AI Handbook, and man, I'm loving it. I've read many books but have never found such a comprehensive book on large models that covers everything from start to finish with such depth.”
Uon L.Reader

Author and edition details

About the author and this edition

Michael Brenndoerfer, author of Build a GPT from Scratch

Michael Brenndoerfer

Michael has spent more than a decade working across software engineering, data, AI, and business. He writes to understand difficult ideas more deeply and to share what he learns in a clear, practical way.

Edition
First edition
Published
Last reviewed
Cite this book

Reference

Citation details

Cite or share this article.

BIBTEXAcademic
@book{brenndoerfer2026gptfromscratch, author = {Michael Brenndoerfer}, title = {Build a GPT from Scratch}, year = {2026}, url = {https://mbrenndoerfer.com/books/gpt-from-scratch}, publisher = {mbrenndoerfer.com}, note = {Accessed: 2026-11-25} }
APAAcademic
Michael Brenndoerfer (2026). Build a GPT from Scratch. Retrieved from https://mbrenndoerfer.com/books/gpt-from-scratch
MLAAcademic
Michael Brenndoerfer. "Build a GPT from Scratch." 2026. Web. November 25, 2026. <https://mbrenndoerfer.com/books/gpt-from-scratch>.
CHICAGOAcademic
Michael Brenndoerfer. "Build a GPT from Scratch." Accessed November 25, 2026. https://mbrenndoerfer.com/books/gpt-from-scratch.
HARVARDAcademic
Michael Brenndoerfer (2026) 'Build a GPT from Scratch'. Available at: https://mbrenndoerfer.com/books/gpt-from-scratch (Accessed: November 25, 2026).
SimpleBasic
Michael Brenndoerfer (2026). Build a GPT from Scratch. https://mbrenndoerfer.com/books/gpt-from-scratch