LLMs From Scratch: Build Your Own ChatGPT-Style Model in PyTorch
Most of us use large language models every day without knowing what happens inside them. LLMs-from-scratch fixes that by having you build one yourself. It's the official code repository for Sebastian Raschka's book Build a Large Language Model (From Scratch), published by Manning, and it has close to 109,000 stars and about 16,800 forks on GitHub.
What it is
The repo walks you through developing, pretraining, and finetuning a GPT-like LLM in PyTorch without using any external LLM libraries. Each chapter has a main Jupyter notebook, a short summary script, and exercise solutions. You go from working with text data and coding attention mechanisms to implementing a full GPT model, pretraining it on unlabeled data, and finetuning it for text classification and for following instructions. The code also shows how to load the weights of larger pretrained models.
Key features
- Step-by-step chapters. Tokenization, attention, the GPT architecture, pretraining, and two kinds of finetuning, each in its own folder.
- Runs on a laptop. The main chapters are designed for conventional laptops and use a GPU automatically when one is available.
- Deep bonus material. BPE tokenizers from scratch, KV caching, grouped-query and multi-head latent attention, mixture-of-experts, LoRA, and DPO alignment.
- Modern architectures. Standalone from-scratch versions of Llama 3.2, Qwen3, Gemma 3, Olmo 3, Qwen3.5, and Gemma 4.
- Extras. A PyTorch primer appendix, Docker dev container setup, and simple user interfaces for chatting with your trained models.
Why it matters
Reading about transformers only gets you so far. Writing every layer yourself is how the ideas finally click. This repo is a great fit for Python developers, students, and AI hobbyists who want to understand LLMs from the inside out, not just call an API. The code is open under an Apache 2.0-based license that excludes the book text and images, so you can learn from it freely and buy the book if you want the full explanations.
Getting started
Clone the repo with git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git, install the requirements with pip install -r requirements.txt, and open the Chapter 2 notebook. A strong foundation in Python is the main prerequisite, and Appendix A covers the PyTorch basics.
View the repo on GitHub: github.com/rasbt/LLMs-from-scratch
Book at Manning | Author's site | Sequel: Reasoning From Scratch

