Knowledge Base · Learning paths
How to Build Your Own LLM from Scratch: The 5-Stage Process Behind GPT and Claude
Building a large language model is less about architecture and more about data, evaluation, and systems engineering. This complete 5-stage roadmap covers pre-training (next-token prediction), high-quality data filtering and mixing, scaling laws for compute optimization, post-training (SFT + RLHF/DPO) to turn predictors into helpful assistants, and finally evaluation with system integration.
Translated from Chinese by AI. Spotted an issue? Use the feedback button.
Original Post: https://x.com/0xCodez/status/2058911661973454915
I’ve broken down exactly how large language models are built — the complete process behind ChatGPT, Claude, and Gemini — and compressed it into a single roadmap.
Save this post.
Once you finish reading, you’ll understand the precise five-stage journey from raw internet text to a model that can respond like a helpful assistant.
Follow my Substack for more high-signal AI content: movez.substack.com
This is not an exaggeration. Most people think the key to building an LLM lies in its architecture. The most important lesson here is: architecture is actually the least important part.
Common Misconceptions About LLMs
Ask most people how to build a model like Claude, and they’ll say: “Use a Transformer.” As if the secret lies in the neural network design.
It doesn’t. The Transformer architecture has largely become standardized and is publicly available. All major labs use roughly the same building blocks. If architecture were the moat, everyone would already have GPT-4.
The sentence that will truly reshape your understanding is this: In practice, what determines a model’s success is data, evaluation, and systems — not architectural tweaks. The best models aren’t just trained. They are engineered.
That’s why this guide focuses on what actually matters: five stages. Architecture is merely a footnote in Stage 1. The remaining four stages are where real model superiority is decided.
01. Pre-training: Teaching the Model Language Itself
Everything begins with a seemingly simple but deeply profound objective: predict the next token. This is autoregressive language modeling. Given a sequence of tokens, the model learns the probability distribution of what comes next.
When you do this at sufficient scale across vast amounts of text, the model absorbs grammar, facts, and reasoning patterns — not because anyone explicitly taught them, but because predicting the next token well requires mastering these things.
Tokenization Is the First Step
Before the model sees any text, the text is first split into tokens. The standard method is Byte-Pair Encoding (BPE), and its design choices influence the entire pipeline that follows.
Architecture: The Least Important Part
The model is a Transformer. For this section, that’s basically all you need to know — and that’s exactly the point. You don’t win by inventing a smarter Transformer. The real competition happens in the other four stages.
Scaling laws taught in courses prove this: Transformers simply have better constant factors and slopes than LSTMs. Pick the standard tool and move on.
02. Data: The Real Deciding Factor for Models
If architecture is the least important, then data is the most important. This stage is what separates good models from mediocre ones, and it’s the part most people underestimate the most.
The process starts with Common Crawl — a massive scrape of the public web, measured in petabytes: 250 billion web pages, over 1 million GB. But raw web data is extremely noisy.
Turning it into usable training material is a brutal, multi-step filtering process.
The pipeline generally looks like this:
- Extraction: Pull text from HTML while properly handling math formulas, templated content, and other special cases.
- Filtering: Remove unwanted content such as NSFW material, harmful content, and personal data.
- Deduplication: Remove duplicates at the URL, document, and line level. Web pages endlessly repeat the same headers, footers, and menus.
- Heuristic Filtering: Remove low-quality documents based on word count, anomalous tokens, dirty tokens, etc.
- Model-based Filtering: Predict whether a page is likely to be cited by Wikipedia.
- Data Mixing: Categorize content into code, books, entertainment, etc., and reweight different source domains using scaling laws.
The sentence worth engraving in your mind is: Data quality beats data quantity. Curating high-quality data is the critical part of practical LLM work — and also the most closely guarded secret in the field.
Curating high-quality data is the key to building production-grade LLMs, and one of the most tightly kept secrets in the industry.
Closed datasets are vastly larger than open ones: LLaMA 3 was trained on 15 trillion tokens; GPT-4 is estimated to have used around 13 trillion tokens.
03. Scaling Laws — Using Compute Resources Optimally
You have 10,000 GPUs available for one month. What model should you train? A larger model, or the same model on more data? Guessing could waste millions of dollars. Scaling laws provide predictable answers.
Empirical findings show that more data and larger models reliably lead to better performance. Moreover, you can predict a model’s final performance before training it, based solely on model size and dataset size.
The modern workflow is to tune hyperparameters on small models first, then extrapolate along the scaling curve to guide that final, massive training run.
The famous Chinchilla conclusion states that roughly 20 training tokens per parameter is compute-optimal when considering only training cost. But this changes dramatically once you factor in inference cost—the cost of actually running the model.
When inference is included, the optimal ratio rises sharply, often exceeding 150 tokens per parameter. You end up training a smaller model on significantly more data, because that model will be run millions or billions of times after training.
There is also a meta-lesson, known as the “bitter lesson”: don’t overcomplicate things. Do simple things and scale them up. In the long run, the only thing that truly matters is how effectively you can leverage computation.
04. Post-Training: Turning a Predictor into a Helpful Assistant
After pre-training, what you get is a powerful model that is not yet suitable for chatting. It completes text, but doesn’t understand that it’s supposed to answer you.
If you ask it a question, it might continue by generating three more questions. From a pure “predict the next word” perspective, that could be a perfectly reasonable continuation.
Supervised Fine-Tuning (SFT)
You show the model thousands of examples: a prompt followed by a high-quality response. The model learns to imitate this pattern. This is behavior cloning and was the critical step from GPT-3 to ChatGPT.
Surprisingly, you don’t need much data. A few thousand examples are often enough, because SFT is mainly teaching the model the format of good answers. The actual knowledge already exists inside the pre-trained model.
The Alpaca project even used another LLM to generate its own training data: 52,000 instruction–response pairs were used to fine-tune a 7B LLaMA into a capable assistant.
RLHF: Aligning with Human Preferences
SFT has three major limitations: it is capped by human demonstration quality; it teaches the model to hallucinate by imitating “correct” answers the model doesn’t actually know (i.e., teaching it to make things up); and collecting ideal answers is expensive. RLHF solves these by optimizing for preferences rather than imitation.
The model generates two answers. A human picks the better one. These preferences are used to train a reward model, which is then used to optimize the LLM to maximize that reward. The classic method uses PPO.
A simpler modern alternative is DPO. It achieves comparable quality using only standard supervised learning and has become the default approach in the open-source community.
05. Evaluation & Systems: Prove It Works and Make It Actually Feasible
Two things wrap around the entire pipeline. Skip either one, and you don’t have a real model.
Evaluation: Measuring Open-Ended Outputs
During pre-training, the key metric is perplexity: how much the model “hesitates” between tokens. From 2017 to 2023, the best models improved from hesitating across roughly 70 tokens down to fewer than 10. But once alignment begins, perplexity breaks down, so evaluation shifts to benchmarks and comparisons:
-
MMLU and HELM: Task suites covering many domains with ground-truth answers. MMLU remains one of the most trusted pre-training benchmarks.
-
Chatbot Arena: Blind human preference votes between two models; over 300,000 votes power an Elo leaderboard.
-
AlpacaEval: Uses an LLM to judge other LLMs. It correlates 98% with Chatbot Arena, takes under 3 minutes, and costs less than $10. It has its own biases, however—such as favoring longer responses.
The honest conclusion is that evaluating an aligned model is genuinely hard. No single number fully captures its quality. The same model can score 0.637 or 0.488 on MMLU depending only on prompt format.
Systems: Making Training Physically Possible
Everyone eventually hits the compute wall. GPUs are expensive, scarce, and constrained by the physics of communication bandwidth. Naively training a 7B model already requires roughly 112GB of memory. Systems work is therefore not optional—it is what makes the whole endeavor feasible:
-
Low Precision: Use 16-bit (bf16) instead of 32-bit to halve memory usage and increase speed.
-
Operator Fusion & Blocking: Reduce slow trips to global memory. FlashAttention alone can deliver ~1.7× end-to-end speedup.
-
Data Parallelism: Split the dataset across multiple GPUs and use ZeRO to shard optimizer states and save memory.
-
Model Parallelism: Split the model across GPUs by layer (pipeline parallelism) or by matrix (tensor parallelism).
-
Sparsity: Mixture of Experts (MoE)—activate only a subset of parameters per token, allowing far more total parameters at the same FLOPs.
What the Whole Pipeline Really Teaches Us
Looking back at these five stages, the central argument becomes crystal clear. Architecture—the part everyone obsesses over—actually receives the least attention. Data, scaling, alignment, evaluation, and systems are where all the real decisions happen.
This is why two labs using the exact same architecture can still produce dramatically different models. Architecture is shared. The things that actually matter are not.
Mistakes That Will Sink an LLM Project
-
Obsessing over architecture. It is the most easily copied and least differentiating part of the entire stack.
-
Treating data as a commodity. Dirty data caps your ceiling no matter how much compute you throw at it. Quality beats quantity.
-
Ignoring Chinchilla math. A model that is too large for the available data will be under-trained and waste compute.
-
Stopping at SFT. Fine-tuned models only imitate. Without RLHF or DPO, they never truly learn what humans actually prefer.
-
Still trusting perplexity after alignment. Post-training changes the distribution; perplexity loses its meaning.
Conclusion
A great model is not trained. It is engineered.
Most people will continue to believe that the key to building LLMs lies in architecture. They will keep reading Transformer explainers and keep missing where the real work happens.
Those who understand the pipeline see it clearly: language modeling first, then clean data, then optimal scaling, then alignment, and finally honest evaluation on efficient systems. Five stages. Architecture is just a paragraph inside one of them.
Pick one stage you’ve been neglecting—whether it’s data or evaluation—and go deep. That’s where the difference is hiding.