Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Build a ChatGPT-like LLM from scratch using PyTorch step by step
.claude/skills/brycewang-stanford-llm-from-scratch-guide/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 8% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 244% | 0% |
| case-13 | ✗→✓ | ▲ Improved | -8% | 0% |
| case-17 | ✗→✓ | ▲ Improved | -9% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 78% | 0% |
LLMs-from-scratch is a comprehensive educational repository with over 87,000 stars on GitHub that teaches you how to build a ChatGPT-like large language model from the ground up using PyTorch. Created by Sebastian Raschka, a machine learning researcher and author, the project provides a complete pipeline covering data preparation, tokenization, attention mechanisms, pretraining, and instruction finetuning.
Unlike tutorials that treat LLMs as black boxes, this project demystifies every component by walking through the full implementation. Each chapter corresponds to a Jupyter notebook with clear explanations, diagrams, and runnable code. The repository accompanies the book "Build a Large Language Model (From Scratch)" and serves as a standalone learning resource for researchers and engineers who want deep understanding of transformer-based language models.
The project is particularly valuable for academic researchers who need to understand the internals of LLMs for their own research, whether that involves modifying architectures, running ablation studies, or developing domain-specific language models for scientific applications.
Clone the repository and set up a Python environment with the required dependencies:
bashgit clone https://github.com/rasbt/LLMs-from-scratch.git cd LLMs-from-scratch # Create a virtual environment python -m venv llm-env source llm-env/bin/activate # Install dependencies pip install -r requirements.txt
The project requires Python 3.10+ and PyTorch 2.0+. For GPU-accelerated training, ensure you have CUDA installed. The notebooks can also run on CPU for smaller model configurations, though training times will be significantly longer.
Key dependencies include:
The project is organized into sequential chapters that build on each other:
Covers the conceptual foundations of LLMs, including the transformer architecture, the difference between encoder and decoder models, and how pretraining and finetuning work at a high level.
Implements text tokenization from scratch, including byte-pair encoding (BPE). You build a custom tokenizer and learn how text is converted to numerical representations:
python# Tokenization example from the project import tiktoken tokenizer = tiktoken.get_encoding("gpt2") text = "Large language models are fascinating." token_ids = tokenizer.encode(text) decoded = tokenizer.decode(token_ids)
Implements self-attention, multi-head attention, and causal (masked) attention from scratch. This is the core computational primitive of transformers:
python# Simplified multi-head attention class MultiHeadAttention(nn.Module): def __init__(self, d_in, d_out, context_length, num_heads, dropout=0.0): super().__init__() self.W_query = nn.Linear(d_in, d_out, bias=False) self.W_key = nn.Linear(d_in, d_out, bias=False) self.W_value = nn.Linear(d_in, d_out, bias=False) self.out_proj = nn.Linear(d_out, d_out) self.num_heads = num_heads self.head_dim = d_out // num_heads
Assembles the full GPT architecture using the attention mechanism, layer normalization, feed-forward networks, and positional embeddings.
Trains the GPT model on a text corpus using next-token prediction. Covers the training loop, loss computation, learning rate scheduling, and gradient clipping.
Adapts the pretrained model for downstream classification tasks, demonstrating how to add a classification head and finetune on labeled data.
Converts the pretrained model into an instruction-following assistant using supervised finetuning on instruction-response pairs, similar to how ChatGPT is trained.
This resource is invaluable for several research scenarios:
For researchers working with limited compute, the project includes configurations for small models (124M parameters) that can be trained on a single GPU in reasonable time, making it practical for experimentation and prototyping.
Combine this project with other tools in your research stack:
The bonus materials in the repository cover additional topics like DPO (Direct Preference Optimization), loading pretrained weights from Hugging Face, and converting models between different formats.
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 13,537 | 6,318 | -53% | 1 | 1 | 0% | 2,251 | 2,437 | +8% | 0 | 0 | — |
case-02 | pass→pass | 13,190 | 16,165 | +23% | 1 | 1 | 0% | 2,332 | 4,153 | +78% | 0 | 0 | — |
case-03 | pass→pass | 3,539 | 2,253 | -36% | 1 | 1 | 0% | 578 | 1,661 | +187% | 0 | 0 | — |
case-04 | pass→pass | 8,316 | 3,104 | -63% | 1 | 1 | 0% | 1,285 | 1,759 | +37% | 0 | 0 | — |
case-05 | fail→fail | 8,317 | 5,460 | -34% | 1 | 1 | 0% | 1,396 | 2,172 | +56% | 0 | 0 | — |
case-06 | fail→pass | 4,317 | 4,795 | +11% | 1 | 1 | 0% | 625 | 2,151 | +244% | 0 | 0 | — |
case-07 | pass→pass | 11,595 | 14,370 | +24% | 1 | 1 | 0% | 2,078 | 4,228 | +103% | 0 | 0 | — |
case-08 | pass→pass | 6,090 | 7,332 | +20% | 1 | 1 | 0% | 1,087 | 2,723 | +151% | 0 | 0 | — |
case-09 | pass→pass | 7,013 | 9,462 | +35% | 1 | 1 | 0% | 1,069 | 2,892 | +171% | 0 | 0 | — |
case-10 | pass→pass | 7,895 | 9,139 | +16% | 1 | 1 | 0% | 1,277 | 2,801 | +119% | 0 | 0 | — |
case-11 | pass→pass | 8,261 | 14,637 | +77% | 1 | 1 | 0% | 1,567 | 2,625 | +68% | 0 | 0 | — |
case-12 | pass→pass | 7,324 | 3,300 | -55% | 1 | 1 | 0% | 1,329 | 2,003 | +51% | 0 | 0 | — |
case-13 | fail→pass | 10,792 | 2,436 | -77% | 1 | 1 | 0% | 1,812 | 1,658 | -8% | 0 | 0 | — |
case-14 | pass→pass | 16,235 | 16,265 | +0% | 1 | 1 | 0% | 2,504 | 3,675 | +47% | 0 | 0 | — |
case-15 | pass→pass | 8,043 | 2,273 | -72% | 1 | 1 | 0% | 1,263 | 1,669 | +32% | 0 | 0 | — |
case-16 | pass→pass | 5,622 | 2,290 | -59% | 1 | 1 | 0% | 888 | 1,650 | +86% | 0 | 0 | — |
case-17 | fail→pass | 11,576 | 2,536 | -78% | 1 | 1 | 0% | 1,881 | 1,720 | -9% | 0 | 0 | — |
case-18 | pass→pass | 8,082 | 1,999 | -75% | 1 | 1 | 0% | 1,233 | 1,601 | +30% | 0 | 0 | — |
case-19 | pass→pass | 4,802 | 2,204 | -54% | 1 | 1 | 0% | 728 | 1,643 | +126% | 0 | 0 | — |
case-20 | fail→fail | 17,694 | 21,897 | +24% | 1 | 1 | 0% | 3,175 | 4,963 | +56% | 0 | 0 | — |
case-21 | pass→pass | 11,640 | 12,591 | +8% | 1 | 1 | 0% | 2,240 | 3,915 | +75% | 0 | 0 | — |
case-22 | pass→pass | 12,799 | 17,272 | +35% | 1 | 1 | 0% | 2,432 | 4,110 | +69% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +18 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.