Download the free Kindle app and start reading Kindle books instantly on your smartphone, tablet, or computer - no Kindle device required.
Read instantly on your browser with Kindle for Web.
Using your mobile phone camera - scan the code below and download the Kindle app.
Follow the author
OK
Build a Large Language Model (From Scratch)
Purchase options and add-ons
In Build a Large Language Model (from Scratch) bestselling author Sebastian Raschka guides you step by step through creating your own LLM. Each stage is explained with clear text, diagrams, and examples. You’ll go from the initial design and creation, to pretraining on a general corpus, and on to fine-tuning for specific tasks.
Build a Large Language Model (from Scratch) teaches you how to:
- Plan and code all the parts of an LLM
- Prepare a dataset suitable for LLM training
- Fine-tune LLMs for text classification and with your own data
- Use human feedback to ensure your LLM follows instructions
- Load pretrained weights into an LLM
Build a Large Language Model (from Scratch) takes you inside the AI black box to tinker with the internal systems that power generative AI. As you work through each key stage of LLM creation, you’ll develop an in-depth understanding of how LLMs work, their limitations, and their customization methods. Your LLM can be developed on an ordinary laptop, and used as your own personal assistant.
About the technology
Physicist Richard P. Feynman reportedly said, “I don’t understand anything I can’t build.” Based on this same powerful principle, bestselling author Sebastian Raschka guides you step by step as you build a GPT-style LLM that you can run on your laptop. This is an engaging book that covers each stage of the process, from planning and coding to training and fine-tuning.
About the book
Build a Large Language Model (From Scratch) is a practical and eminently-satisfying hands-on journey into the foundations of generative AI. Without relying on any existing LLM libraries, you’ll code a base model, evolve it into a text classifier, and ultimately create a chatbot that can follow your conversational instructions. And you’ll really understand it because you built it yourself!
What's inside
- Plan and code an LLM comparable to GPT-2
- Load pretrained weights
- Construct a complete training pipeline
- Fine-tune your LLM for text classification
- Develop LLMs that follow human instructions
About the reader
Readers need intermediate Python skills and some knowledge of machine learning. The LLM you create will run on any modern laptop and can optionally utilize GPUs.
About the author
Sebastian Raschka is a Staff Research Engineer at Lightning AI, where he works on LLM research and develops open-source software.
The technical editor on this book was David Caswell.
Table of Contents
1 Understanding large language models
2 Working with text data
3 Coding attention mechanisms
4 Implementing a GPT model from scratch to generate text
5 Pretraining on unlabeled data
6 Fine-tuning for classification
7 Fine-tuning to follow instructions
A Introduction to PyTorch
B References and further reading
C Exercise solutions
D Adding bells and whistles to the training loop
E Parameter-efficient fine-tuning with LoRA
- ISBN-101633437167
- ISBN-13978-1633437166
- PublisherManning
- Publication dateOctober 29, 2024
- LanguageEnglish
- Dimensions7.38 x 0.7 x 9.25 inches
- Print length368 pages
Frequently bought together

More items to explore
- Hands-On Large Language Models: Language Understanding and GenerationPaperbackFREE Shipping by AmazonGet it as soon as Thursday, Sep 10
- Build a Reasoning Model (From Scratch)PaperbackFREE Shipping by AmazonGet it as soon as Thursday, Sep 10
- DAMA-DMBOK: Data Management Body of Knowledge: 2nd EditionPaperbackFREE Shipping by AmazonGet it as soon as Thursday, Sep 10
- Agentic Coding with Claude Code: The everyday developer's guide to agentic coding with Claude CodePaperbackFREE Shipping by AmazonGet it as soon as Thursday, Sep 10
- Designing Machine Learning Systems: An Iterative Process for Production-Ready ApplicationsPaperbackFREE Shipping by AmazonGet it as soon as Thursday, Sep 10
Customers also bought or read
- Build a Reasoning Model (From Scratch)#1 Best SellerProgramming Algorithms
Paperback$47.82$47.82FREE delivery Thu, Sep 10 - Hands-On Large Language Models: Language Understanding and Generation
Paperback$37.68$37.68FREE delivery Thu, Sep 10 - AI Engineering: Building Applications with Foundation Models#1 Best SellerEnterprise Applications
Paperback$52.40$52.40FREE delivery Thu, Sep 10 - LLM Engineer's Handbook: Master the art of engineering large language models from concept to production
Paperback$39.99$39.99FREE delivery Thu, Sep 10 - Domain-Specific Small Language Models: Efficient AI for local deployment
Paperback$41.51$41.51FREE delivery Thu, Sep 10 - Reinforcement Learning from Human Feedback: LLM alignment and post-training
Just releasedPaperback$58.44$58.44FREE delivery Thu, Sep 10 - Knowledge Graphs and LLMs in Action: Build AI systems using connected data
Paperback$55.99$55.99FREE delivery Thu, Sep 10 - Designing Machine Learning Systems: An Iterative Process for Production-Ready Applications
Paperback$40.00$40.00FREE delivery Thu, Sep 10 - The Hundred-Page Language Models Book: hands-on with PyTorch (The Hundred-Page Books)
Paperback$46.95$46.95FREE delivery Thu, Sep 10 - LLMs in Production: From language models to successful products
Paperback$48.13$48.13FREE delivery Thu, Sep 10 - Natural Language Processing with Transformers, Revised Edition
Paperback$41.60$41.60FREE delivery Thu, Sep 10 - Building LLMs for Production: Enhancing LLM Abilities and Reliability with Prompting, Fine-Tuning, and RAG
Paperback$43.48$43.48FREE delivery Thu, Sep 10 - AI Agents in Action: Build, orchestrate, and deploy autonomous multi-agent systems
Paperback$55.85$55.85$3.99 delivery Wed, Sep 23 - Deep Learning with PyTorch, Second Edition: Training and applying deep learning and generative AI models
Paperback$51.68$51.68FREE delivery Thu, Sep 10 - Machine Learning with PyTorch and Scikit-Learn: Develop machine learning and deep learning models with Python#1 Best SellerSpeech & Audio Processing
Paperback$36.05$36.05FREE delivery Thu, Sep 10 - Building AI Agents with LLMs, RAG, and Knowledge Graphs: A practical guide to autonomous and modern AI agents
Paperback$41.99$41.99FREE delivery Thu, Sep 10 - Mathematics of Machine Learning: Master linear algebra, calculus, and probability for machine learning
Paperback$44.30$44.30FREE delivery Thu, Sep 10 - Deep Learning (Adaptive Computation and Machine Learning series)
Hardcover$56.00$56.00FREE delivery Sep 11 - 14 - AI Agents and Applications: With LangChain, LangGraph, and MCP
Paperback$51.69$51.69FREE delivery Thu, Sep 10 - AI Systems Performance Engineering: Optimizing Model Training and Inference Workloads with GPUs, CUDA, and PyTorch
Paperback$71.25$71.25$3.99 delivery Sep 21 - 24 - Hands-On Machine Learning with Scikit-Learn and PyTorch: Concepts, Tools, and Techniques to Build Intelligent Systems
Paperback$75.43$75.43FREE delivery Thu, Sep 10 - Designing Multi-Agent Systems: Principles, Patterns, and Implementation for AI Agents
Paperback$46.39$46.39FREE delivery Thu, Sep 10 - Prompt Engineering for LLMs: The Art and Science of Building Large Language Model-Based Applications
Paperback$54.51$54.51FREE delivery Thu, Sep 10 - Deep Reinforcement Learning Hands-On: A practical and easy-to-follow guide to RL from Q-learning and DQNs to PPO and RLHF
Paperback$31.10$31.10Delivery Sep 13 - 14 - AI Agents in Action, Second Edition: Intelligent workflows with LLMs, MCP, A2A, and more#1 New ReleaseNatural Language Processing
Paperback$47.48$47.48FREE delivery Thu, Sep 10 - Building Applications with AI Agents: Designing and Implementing Multiagent Systems
Paperback$56.44$56.44FREE delivery Thu, Sep 10
From the Publisher
“The most understandable and comprehensive explanation of language models yet! Its unique and practical teaching style achieves a level of understanding you can’t get any other way.”
Cameron Wolfe, Senior Scientist, Netflix
“Sebastian combines deep knowledge with practical engineering skills and a knack for making complex ideas simple. This is the guide you need!”
Chip Huyen, author of Designing Machine Learning Systems and AI Engineering
“Definitive, up-to-date coverage. Highly recommended!”
Dr. Vahid Mirjalili, Senior Data Scientist, FM Global
why this book?
Build a Large Language Model (From Scratch) offers a practical, hands-on approach to understanding and constructing large language models (LLMs) from the ground up.
By guiding you through each stage—from data preparation and coding attention mechanisms to pretraining and fine-tuning—this book demystifies the inner workings of LLMs using Python and PyTorch.
Ideal for developers and machine learning enthusiasts, it empowers you to build a functional GPT-style model on a standard laptop, fostering a deeper comprehension of generative AI technologies.
about Manning
Manning helps developers and tech professionals stay ahead in a fast-moving industry with expert-led books, videos, and projects. Learning never stops, but it’s hard to keep up, so we focus on content that’s practical, clear, and trusted. As an independent publisher, we adapt quickly, from pioneering early-access books to offering DRM-free eBooks. Our series, like "In Action" and "In a Month of Lunches", reflect a commitment to making complex topics accessible.
LLMs in Production: From language models to successful products
|
AI Agents in Action
|
Natural Language Processing in Action, Second Edition
|
Effective Conversational AI: Chatbots that work
|
Data Analysis with LLMs: Text, tables, images and sound (In Action)
|
Causal AI
|
|
|---|---|---|---|---|---|---|
| Customer Reviews |
4.5 out of 5 stars 36
|
4.0 out of 5 stars 51
|
4.8 out of 5 stars 8
|
4.8 out of 5 stars 7
|
4.8 out of 5 stars 6
|
4.4 out of 5 stars 14
|
| Price | $48.13$48.13 | $55.85$55.85 | $58.97$58.97 | $51.42$51.42 | $33.99$33.99 | $51.86$51.86 |
| Level of proficiency | Intermediate | Intermediate | Intermediate | Intermediate | Intermediate | Advanced |
| About the reader | For data scientists and ML engineers. | For intermediate Python programmers. | For intermediate Python programmers. | For developers, engineers, and product managers. | For data scientists and data analysts. | For data scientists and machine learning engineers. |
| Special features | Includes liveBook with out built-in AI assistant. | Includes liveBook with out built-in AI assistant. | Includes liveBook with out built-in AI assistant. | Includes liveBook with out built-in AI assistant. | Includes liveBook with out built-in AI assistant. | Includes liveBook with out built-in AI assistant. |
| Pages | 456 | 344 | 688 | 328 | 232 | 520 |
Editorial Reviews
Review
A clear, hands-on guide that empowers readers to build their own models and explore the cutting edge of AI. -- Guillermo Alcántara, Project manager, PepsiCo Global
Must-have resource for quickly getting up to speed on LLMs. Whether you're new to the field or looking to deepen your knowledge, it’s the perfect guide. -- Walter Reade, Staff Developer Relations Engineer, Kaggle/Google
A fantastic resource for diving into LLMs—a must-read for anyone eager to get hands-on! -- Dr. Vahid Mirjalili, Senior Data Scientist, FM Global
From the Back Cover
From the back cover:
Build a Large Language Model (From Scratch) is a practical and eminently-satisfying hands-on journey into the foundations of generative AI. Without relying on any existing LLM libraries, you'll code a base model, evolve it into a text classifier, and ultimately create a chatbot that can follow your conversational instructions. And you'll really understand it because you built it yourself!
About the reader:
Readers need intermediate Python skills and some knowledge of machine learning. The LLM you create will run on any modern laptop and can optionally utilize GPUs.About the Author
Product details
- Publisher : Manning
- Publication date : October 29, 2024
- Language : English
- Print length : 368 pages
- ISBN-10 : 1633437167
- ISBN-13 : 978-1633437166
- Item Weight : 1.35 pounds
- Dimensions : 7.38 x 0.7 x 9.25 inches
- Best Sellers Rank: #6,991 in Books (See Top 100 in Books)
- #1 in Computer Neural Networks
- #2 in Artificial Intelligence Expert Systems
- #2 in Python Programming
- Customer Reviews:
About the author

Sebastian Raschka, PhD is an LLM Research Engineer with over a decade of experience in artificial intelligence. His work bridges academia and industry, including roles as senior engineering staff at an AI company and a statistics professor.
As an independent researcher and industry expert, Sebastian collaborates with companies on AI solutions and serves on the Open Source Advisory Board at University of Wisconsin–Madison.
Sebastian specializes in LLMs and the development of high-performance AI systems, with a deep focus on practical, code-driven implementations.
Customer reviews
Customer Reviews, including Product Star Ratings help customers to learn more about the product and decide whether it is the right product for them.
To calculate the overall star rating and percentage breakdown by star, we don’t use a simple average. Instead, our system considers things like how recent a review is and if the reviewer bought the item on Amazon. It also analyzed reviews to verify trustworthiness.
Learn more how customers reviews work on AmazonReviews with images
Built the LLM in Day
Top reviews from the United States
- 5 out of 5 stars
A great introduction to LLM architecture
Reviewed in the United States on August 3, 2026My goal for this book was to learn more about how LLMs produce text, and in particular, how they know when to stop. And this book covers it all, with source code you can actually follow. I expecially appreciate the appendices that cover Pytorch and the basics of neural networks. I'm going to buy the next book in this series, on reasoning models, as soon as I'm done with this review.
One person found this helpfulSending feedback...Sending feedback...HelpfulThank you for your feedback.Sorry, we failed to record your vote. Please try againThanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport - 5 out of 5 stars
Amazing book. Exceeded my expectations!
Reviewed in the United States on October 12, 2025The book is amazing. Much better than I expected. I was minimally familiar with neural networking techniques (finished 6-months course on Coursera, and by now have forgotten most of it). So, I had a vague idea about forward and backward propagation, remembered such terms as dropout, normalization etc. without actually remembering their meaning. From the Andrew Ng course I remembered the term "transformer" (since he had a few good introductory explanations of it), but by now I completely forgot how it works. My knowledge of Python was very limited (and mostly forgotten). I knew nothing about PyTorch. When I saw the references to the book on Facebook, I decided that it might be helpful for me to recall these concepts, and especially interesting was to learn the concept of transformers and self-attention which I knew belong to the foundation of modern LLMs.
The book exceeded my expectations. It is written in an excellent methodical style. Introduces concepts one by one, helps experimenting with them in the real code. It provided an excellent introduction to PyTorch (in Appendix A, which the author recommended to consume before reading the rest of the book). The introduction is short, not overwhelming the reader with millions potential concepts of the huge ecosystem of Python and PyTorch, and still sufficient for productive consuming the entire book that uses both. All the concepts are defined in easy-to-consume steps, leading eventually to a complete overall understanding of GPT model. I am not naive to think that I can develop LLMs by myself now, but I definitely got more than expected. And enjoyed the material a lot.
I did not use the code from GitHub (by the book's reference). Instead, I meticulously re-entered all the examples from the book's text into several Jupyter Notebooks in VSCode. This way I moved a bit slower but understood material better. Even found a few minor (typo-level) issues in the code.
I am working on an ordinary Surface Book (no GPU), and all examples work instantaneously so far (obviously, it will change when I come to training). I am now in the position after chapter 4: Built the untrained GPT model and cannot wait when I will start training and using it.
Highly recommend the book to everyone who wants to make their hands "dirty" with the AI.
27 people found this helpfulSending feedback...Sending feedback...HelpfulThank you for your feedback.Sorry, we failed to record your vote. Please try againThanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport - 5 out of 5 stars
Outstanding book !!
Reviewed in the United States on August 5, 2026Well written book! Clear explanation on how models like ChatGPT work. Provides an excellent foundation !
Sending feedback...Sending feedback...HelpfulThank you for your feedback.Sorry, we failed to record your vote. Please try againThanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport - 5 out of 5 stars
The Book That Makes LLMs Feel Buildable
Reviewed in the United States on May 5, 2026Clear Explanations Without Hiding the Hard Parts
What I like most is that it doesn’t treat LLMs like magic. It breaks the process down step by step—from tokenization and embeddings to attention, training, and generation—so I could see how all the pieces connect.
Hands-On Learning That Actually Sticks
The examples make a huge difference. Instead of only reading theory, I was able to follow along and understand how each part works in code. That made concepts like transformers and self-attention feel much less intimidating.
Great for Going Beyond Surface-Level AI
This isn’t just a “what is AI?” book. It helped me understand the mechanics behind modern language models and gave me a much stronger foundation for experimenting on my own.
Challenging, But Worth It
Some sections take focus, but that’s part of why I liked it. It pushes you to really understand the material.
3 people found this helpfulSending feedback...Sending feedback...HelpfulThank you for your feedback.Sorry, we failed to record your vote. Please try againThanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport - 4 out of 5 stars
A Valuable LLM Book, but Beginners May Need More Foundation
Reviewed in the United States on August 11, 2026I enjoyed the portion of this book that I read. It offers a practical and focused approach to understanding how large language models are built. I don't like the cover of the book, though.
However, absolute beginners may benefit from complementing it with a comprehensive deep learning resource. Before building an LLM, it helps to understand artificial neural networks, which provide the foundation upon which LLMs are built.
For that purpose, I highly recommend the Deep Learning from Curiosity to Mastery series as a companion. Volume 1 covers the foundations of neural networks, while Volume 2 explains embeddings, attention mechanisms, and Transformers in detail. Those are the foundations of LLM.
Raschka’s book is an excellent specialized resource for building an LLM. For beginners, pairing it with the broader foundation provided by Deep Learning from Curiosity to Mastery can make the concepts easier to understand and connect.
Sending feedback...Sending feedback...HelpfulThank you for your feedback.Sorry, we failed to record your vote. Please try againThanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport - 5 out of 5 stars
Built the LLM in Day
Reviewed in the United States on July 22, 2026I read the book on Monday, and on Tuesday, I had a fully functional Large Language Model (from Scratch).
I'm a retired carpenter, so anyone can build a Large Language Model from this book.
I used pretraining_simple.py, instruction_finetune.py, and downloaded the OpenAI weights.
I trained the LLM on my own Roof Framing 10 MB dataset. It can correctly answer all the questions in my instruction dataset and in Roof Framing in general. Any questions not related to roof framing are answered in coherent sentences. I wouldn't call them hallucinations, just coherent, unrelated answers.

I read the book on Monday, and on Tuesday, I had a fully functional Large Language Model (from Scratch).
I'm a retired carpenter, so anyone can build a Large Language Model from this book.
I used pretraining_simple.py, instruction_finetune.py, and downloaded the OpenAI weights.
I trained the LLM on my own Roof Framing 10 MB dataset. It can correctly answer all the questions in my instruction dataset and in Roof Framing in general. Any questions not related to roof framing are answered in coherent sentences. I wouldn't call them hallucinations, just coherent, unrelated answers.
One person found this helpfulSending feedback...Sending feedback...HelpfulThank you for your feedback.Sorry, we failed to record your vote. Please try againThanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport - 3 out of 5 stars
Code Okay explanations are not
Reviewed in the United States on September 5, 2025This book is so-so. I wouldn't buy it again. I wanted to learn how the llm works and how the embedding algorithms are designed. Alternatively, he could have discussed the training algorithm of the llm and how the weighting matrices are determined. Alternatively, he could have discussed how the math by setting the vector spaces relate meanings to words so that an llm can convert that into something intelligible as a response. None of this was done. He presents code for llm and uses python libraries. However, it is a black box. All the discussion varies from 2 extremes of high level generalities and then specific lingo and code for particular abstractions. However, virtually nothing is made concrete. Of course some will disagree, but if I knew how llm's worked, I wouldn't write this book. If I don't understand the details of llm functions and code design with llm, this book wouldn't help much. That being said, if you want some code snippets, you will find some useful ones here.
5 people found this helpfulSending feedback...Sending feedback...HelpfulThank you for your feedback.Sorry, we failed to record your vote. Please try againThanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport - 5 out of 5 stars
100% Recommend (With prerequisite)
Reviewed in the United States on August 31, 2026This is a really amazing book to understand the foundational concepts behind LLMs and NLP. It is very well thought out and organized, while maintaining technical depth. A prior understanding of Python makes it more beneficial. If you want to learn what’s in this book but don’t now Python I’d 100% recommend first completing the free CS50P class from Harvard on EdX.
Sending feedback...Sending feedback...HelpfulThank you for your feedback.Sorry, we failed to record your vote. Please try againThanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport
Top reviews from other countries
Jérémie5 out of 5 starsTop
Reviewed in France on July 29, 2025A great book to start and understand AI.
Sending feedback...Thanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport
MvG5 out of 5 starsPerfect Explanation of LLMs
Reviewed in Germany on June 9, 2026One of the best books on the subject of AI and LLMs that I’ve come across so far. The author explains the components and inner workings of an LLM perfectly. By the end of the book, you’ll have a clear understanding of how an LLM and a chatbot work internally. The only prerequisites for this book are a basic understanding of how neural networks are structured and some knowledge of vectors and matrices; with these, all the steps and code examples are easy to follow.
I’m already looking forward to the author’s next book on reasoning models.
Sending feedback...Thanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport
JP Blankert2 out of 5 starsUseful stuff - sloppy and badly structured content
Reviewed in the Netherlands on August 15, 2026Contains useful stuff, but the author is very sloppy with numbers, which is very annoying comparing one figure with the next. He often rounds off numbers in a wrong way, especially when they are small ('1') or ('2'). The author prefers printing Python that a LLM can also give me. The structure of the book is not logical. Introduces a loss function without mathematical representation. You will need chatgpt to make shots and let you explain. Even chapter titles are misleading, as chapter 7 'pretraining' - no, it is the first introduction of a specific loss function. Book needs a lot of re-work
There is not even one flow diagram representing the total flow through a LLM
Sending feedback...Thanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport
Kirill1 out of 5 starsBad printing quality
Reviewed in the United Arab Emirates on December 5, 2025Poor printing quality: paper is so thin so one can see letters from back side while reading front side. Also for some reason main cover is not alligned with the rest of the book.
Overall impression like it was printed at home
Sending feedback...Thanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport
Beyza5 out of 5 starsİçerim güzel
Reviewed in Turkey on August 25, 2025İçerik çok güzel ama ben basım kağıdını beğenmedim
Sending feedback...Thanks, we'll investigate in the next few days.Sorry, We failed to report this review. Please try againWe'll check if this review meets our community guidelinesOpens in a new tab. If it doesn't, we'll remove it.
CancelReport





















