Skip to main content

Reasoning Models From Scratch

Project description

Build A Reasoning Model (From Scratch)

This repository contains the code for developing an LLM reasoning model and is the official code repository for the book Build a Reasoning Model (From Scratch).



(Printed in color.)


In Build a Reasoning Model (From Scratch), you will learn and understand how a reasoning large language model (LLM) works.

Reasoning is one of the most exciting and important recent advances in improving LLMs, but it’s also one of the easiest to misunderstand if you only hear the term reasoning and read about it in theory. This is why this book takes a hands-on approach. We will start with a pre-trained base LLM and then add reasoning capabilities ourselves, step by step in code, so you can see exactly how it works.

The methods described in this book walk you through the process of developing your own small-but-functional reasoning model for educational purposes. It mirrors the approaches used in creating large-scale reasoning models such as DeepSeek R1, GPT-5 Thinking, and others. In addition, this book includes code for loading the weights of existing, pretrained models.



To download a copy of this repository, click on the Download ZIP button or execute the following command in your terminal:

git clone --depth 1 https://github.com/rasbt/reasoning-from-scratch.git

Tip: Chapter 2 provides additional tips on installing Python, managing Python packages, and setting up your coding environment.



Table of Contents (In Progress)

Code tests Linux Code tests macOS Code tests Windows

Chapter Title Main Code
Ch 1: Understanding reasoning Models No code
Ch 2: Generating Text with a Pre-trained LLM - ch02_main.ipynb
- ch02_exercise-solutions.ipynb
Ch 3: Evaluating Reasoning Models - ch03_main.ipynb
Ch 4: Improving Reasoning with Inference-Time Scaling TBA
Ch 5: Training Reasoning Models with Reinforcement Learning TBA
Ch 6: Distilling Reasoning Models for Efficient Reasoning TBA
Ch 7: Improving the Reasoning Pipeline and Future Directions TBA
Appendix A: References and Further Reading No code
Appendix B: Exercise Solutions Code and solutions are in each chapter's subfolder
Appendix C: Qwen3 LLM Source Code - chC_main.ipynb
Appendix D TBA
Appendix E TBA
Appendix F: Common Approaches to LLM Evaluation - chF_main.ipynb

 

The mental model below summarizes the main techniques covered in this book.


 

Companion Book

Please note that Build A Reasoning Model (From Scratch) is a standalone book focused on methods to improve LLM reasoning.

In this book, we work with a pre-trained open-source base LLM (Qwen3) on top of which we code apply reasoning methods from scratch. This includes inference-time scaling, reinforcement learning, and distillation.

However, if you are interested in understanding how a conventional base LLM is implemented, you may like my previous book, Build a Large Language Model (From Scratch).


 

Hardware Requirements

The code in the main chapters of this book is designed to mostly run on consumer hardware within a reasonable timeframe and does not require specialized server hardware. This approach ensures that a wide audience can engage with the material. Additionally, the code automatically utilizes GPUs if they are available. That being said, chapters 2-4 will work well on CPUs and GPUs. For chapters 5 and 6, it is recommended to use a GPU if you want to replicate the results in the chapter.

(Please see the setup_tips doc for additional recommendations.)

 

Exercises

Each chapter of the book includes several exercises. The solutions are summarized in Appendix B, and the corresponding code notebooks are available in the main chapter folders of this repository (for example, ch02/01_main-chapter-code/ch02_exercise-solutions.ipynb).

 

Bonus Material

Several folders contain optional materials as a bonus for interested readers:

 

Questions, Feedback, and Contributing to This Repository

I welcome all sorts of feedback, best shared via the Manning Discussion Forum or GitHub Discussions. Likewise, if you have any questions or just want to bounce ideas off others, please don't hesitate to post these in the forum as well.

Please note that since this repository contains the code corresponding to a print book, I currently cannot accept contributions that would extend the contents of the main chapter code, as it would introduce deviations from the physical book. Keeping it consistent helps ensure a smooth experience for everyone.

 

Citation

If you find this book or code useful for your research, please consider citing it.

Chicago-style citation:

Raschka, Sebastian. Build A Reasoning Model (From Scratch). Manning, 2025. ISBN: 9781633434677.

BibTeX entry:

@book{build-llms-from-scratch-book,
  author       = {Sebastian Raschka},
  title        = {Build A Reasoning Model (From Scratch)},
  publisher    = {Manning},
  year         = {2025},
  isbn         = {9781633434677},
  url          = {https://mng.bz/lZ5B},
  github       = {https://github.com/rasbt/reasoning-from-scratch}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reasoning_from_scratch-0.1.8.tar.gz (47.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reasoning_from_scratch-0.1.8-py3-none-any.whl (41.8 kB view details)

Uploaded Python 3

File details

Details for the file reasoning_from_scratch-0.1.8.tar.gz.

File metadata

  • Download URL: reasoning_from_scratch-0.1.8.tar.gz
  • Upload date:
  • Size: 47.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for reasoning_from_scratch-0.1.8.tar.gz
Algorithm Hash digest
SHA256 2e90b88ba893afe918983d53a84e5e94cd8e3a430e4ea4c77bc228a99ea4a8a1
MD5 3a5466ece5f3b6e0079767740bb136fc
BLAKE2b-256 c546743109000b26af643c5dbe4d17e294ed5576d8f8f3a43de9d4f5cc5a3b48

See more details on using hashes here.

File details

Details for the file reasoning_from_scratch-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for reasoning_from_scratch-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 ced58e0c5ca063786effaf8734a9b14632dc40f513b0d20c73192e10e454a6bc
MD5 811178a737693029a46797569b0220a3
BLAKE2b-256 79cfe396d288d6107debfa496b6062e37b04a80100199c6c120304fa2ac92919

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page