Skip to content

How LLMs Learn from the Internet: The Training Process

Understand how the LLM training process transforms raw web text into aligned models through tokenization, pre-training, and reinforcement learning.

Tuan Tran Van
11 min read
Contents (7 sections)
  1. 1. The Pre-Training Corpus: How the Raw Web Is Cleaned and Tokenized
  2. 2. Pre-Training: Learning Language Patterns by Predicting the Next Token
  3. 3. The Transformer Architecture: Why Attention Scales Across the Web
  4. 4. Base Models Aren't Assistants: The Gap Between Document Simulator and Chatbot
  5. 5. Post-Training and Alignment: Instruction Tuning (SFT) and RLHF
  6. 6. The Data Wall: What Happens When AI Runs Out of Human Text?
  7. References

(LLMs) learn to generate text by processing web datasets through an intensive computational pipeline. The LLM training process does not function as a database lookup system where you retrieve stored facts; instead, it converts raw web content into billions of numerical parameters encoding statistical syntax, semantics, and reasoning structures. By optimizing these parameters across multi-stage pre-training and , you transform raw autocomplete engines into functional conversational systems.

When you query an LLM, your model does not search an index or fetch discrete files from a repository. Every response emerges from a parameter matrix—a dense network of statistical weights tuned across distinct training phases. To build an interactive assistant from internet-scale text, you must execute self-supervised pre-training to establish baseline language models, followed by targeted post-training alignment stages that shape your model's outputs to follow human instructions safely and effectively.

This multi-stage architecture moves sequentially from raw web extraction to behavior optimization. You begin by stripping boilerplate and tokenizing petabyte-scale crawls, pass the clean text into distributed clusters for next-token prediction, and finalize model behavior through supervised and reinforcement learning. Navigating this pipeline requires balancing data quality, compute scaling laws, and objective loss formulations at every engineering step.

The complete computational pipeline transforming raw web data into Large Language Models

1. The Pre-Training Corpus: How the Raw Web Is Cleaned and Tokenized

When you construct a dataset capable of training a , you must extract, clean, and filter petabyte-scale web data. You typically source this data from public repositories like CommonCrawl, which releases between 200 and 400 TiB of uncompressed content per crawl every one to two months. For example, the April 2024 snapshot comprised 2.7 billion web pages totaling 386 TiB of raw HTML text. However, you cannot feed raw web crawls directly into an optimization pipeline because uncurated text contains excessive HTML markup, navigation menus, and duplicate page boilerplate.

CommonCrawl provides data in raw Web ARChive (WARC) files containing full HTML headers, and WARC Encapsulated Text (WET) files containing text-only extractions. While WET files lower your initial ingestion costs, their default extraction algorithms retain excessive boilerplate and UI noise. When you extract text from WARC files using precision-focused libraries such as Trafilatura, you achieve significantly higher corpus quality. Comparative processing on the 2019-18 crawl demonstrates this trade-off: Trafilatura extraction from WARC files yields approximately 200 billion of clean text, whereas WET files yield 254 billion tokens polluted with site navigation and boilerplate. Models you train on Trafilatura-extracted WARC text consistently outperform models trained on WET files, despite the 25% drop in raw token volume.

To refine your extracted text, you must apply systematic base filtering and heuristic quality thresholds. Your base filtering relies on URL blocklists to eliminate adult content, paired with fastText language identification models tuned to retain English text with a confidence score ≥ 0.65. You then apply quality rules derived from RefinedWeb, MassiveText, and C4 to eliminate structural anomalies. C4 heuristics drop documents containing "lorem ipsum", code fragments marked by curly brackets ({ or }), or irregular line lengths. Furthermore, you apply punctuation thresholds, dropping documents where the fraction of lines ending in terminal punctuation is ≤ 0.12 to strip out non-prose UI fragments and short keyword lists. For specialized educational datasets like FineWeb-Edu, you deploy synthetic annotation: you use Llama-3-70B-Instruct to rate 500,000 document samples on a 0–5 educational scale, using these labels to train a Snowflake-arctic-embed regression classifier. Filtering your corpus at a classifier threshold of ≥ 3 isolates 1.3 trillion high-value educational tokens from a 15-trillion token base.

The multi-stage web data cleaning and deduplication pipeline

Deduplication prevents your model from memorizing repeated text verbatim. Unfiltered web data contains heavy repetition from domain mirrors and crawler loops. Your standard fuzzy deduplication architecture uses MinHash over 5-gram word sequences. By configuring 112 hash functions across 14 buckets of 8 hashes each, your system flags document pairs sharing at least 75% similarity whenever their hashes match across all 8 positions in any bucket. Crucially, you must avoid global cross-dump deduplication across historical crawls: iteratively deduplicating older dumps against newer ones removes over 90% of older crawl text (e.g., stripping the 2013-48 dump from 490 billion to 31 billion tokens) and degrades benchmark performance by leaving low-quality out-of-distribution remnants. Performing MinHash deduplication independently per dump preserves 20 trillion tokens across historical dumps while matching top benchmark performance.

Finally, you pass the clean text through Byte Pair Encoding (BPE), a lossless algorithm that maps strings into integer token sequences using a fixed vocabulary (50,000–100,000 tokens). Common words map to single tokens, while rare terms split into subwords (e.g., "unhappiness" into "un" + "happiness").

2. Pre-Training: Learning Language Patterns by Predicting the Next Token

Pre-training requires over 98% of your compute budget. You structure pre-training as self-supervised learning where your sole objective is next-token prediction across (e.g., 2,048 tokens long).

The training objective relies on cross-entropy loss over a fixed vocabulary V. For a token sequence x = (x_1, x_2, ..., x_n), your model projects an unnormalized logit vector across V, which a softmax function converts into a predicted probability distribution y_hat = [y_hat_1, y_hat_2, ..., y_hat_V]. The cross-entropy loss CE(phi) for predicting the true target token x_i (where index k = f(x_i)) is formulated as:

text
Loss_CE(phi) = -E_x [ log y_hat_k ]

At initialization, your parameter matrix phi—spanning billions to hundreds of billions of parameters—is populated with small random statistical weights drawn from specific initial distributions.

Consequently, an untrained model outputs random probabilities across the vocabulary. You drive learning through an iterative batch optimization loop.

Specialized hardware clusters (GPUs or TPUs) ingest batches of tokenized text sequences containing millions of tokens per step.

Your model executes a forward pass to project token probabilities, computes the scalar cross-entropy loss against the ground-truth target tokens, and runs backpropagation to compute partial derivatives (gradients) quantifying each parameter's contribution to prediction error. An optimizer then applies gradient descent, updating all parameters simultaneously by minute increments (e.g., 0.00001) to lower aggregate loss.

The pre-training batch optimization loop predicting the next token

Across trillions of token iterations, next-token prediction forces the model to compress grammatical syntax, factual relationships, and reasoning patterns into its internal weight matrices. You do not explicitly encode linguistic rules; high-level language capabilities emerge naturally as the statistical consequence of minimizing prediction loss over diverse web corpora.

3. The Transformer Architecture: Why Attention Scales Across the Web

Prior to the architecture introduced in 2017, sequence modeling relied on Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM) units. These architectures process sequences sequentially from left to right. This sequential dependency created two critical bottlenecks: vanishing gradients over long horizons (causing models to forget early context) and an inability to parallelize training across compute nodes.

The Transformer architecture eliminates recurrence by implementing self-attention mechanisms. Self-attention enables all tokens across an input sequence to compute direct associative weights with one another simultaneously, regardless of positional distance. For example, in the sentence "The animal didn't cross the street because it was too tired", the calculates high attention weights between the pronoun "it" and "animal" rather than "street", resolving coreference ambiguities through full contextual visibility.

Transformer self-attention routing context across deep network layers

Modern decoder-only foundation models stack dozens of these Transformer layers. As token vectors traverse deeper layers:

  • Early layers capture local syntactic structure and short-range token associations.
  • Intermediate layers model semantic relationships and paragraph-level context.
  • Deep layers synthesize abstract reasoning patterns and cross-document concepts.

Because self-attention eliminates step-by-step sequential bottlenecks, you can distribute tensor operations across thousands of GPUs using pipeline and tensor parallelism. This hardware-architecture alignment makes it computationally feasible to train models on multi-trillion-token web corpora within acceptable timeframes.

4. Base Models Aren't Assistants: The Gap Between Document Simulator and Chatbot

Once pre-training completes, you have produced a Base Model (such as raw LLaMA or GPT-4 base). While this model holds extensive linguistic and factual distributions, it does not function as an instruction-following assistant.

A base model is fundamentally an internet document simulator. It continues text based on statistical likelihood rather than user intent. If you prompt a base model with an inquiry like "How do you reverse a string in Python?", the model does not recognize this as a request for help. Instead, it treats your prompt as an incomplete web document—often generating follow-up exam questions such as "Question 2: How do you split a string? Write your answer below" rather than providing the solution.

Comparison between an unaligned internet document simulator and an aligned assistant

Furthermore, unaligned base models replicate the toxic, biased, and hallucinatory patterns prevalent in raw web data. To convert an internet-scale completion engine into an aligned chatbot that follows instructions, you must transition the model into post-training.

5. Post-Training and Alignment: Instruction Tuning (SFT) and RLHF

Post-training begins with Supervised Fine-Tuning (SFT). During SFT, you fine-tune the base model on curated demonstration datasets consisting of conversational prompt-response pairs. These datasets, typically ranging from tens of thousands to hundreds of thousands of examples, teach the model the conversational structure of a helpful assistant.

SFT uses standard autoregressive language modeling loss, but only computes gradients over the assistant's response tokens while masking prompt tokens.

The multi-stage post-training alignment architecture with SFT, Reward Model, and RLHF

To align model behavior beyond demonstration data, you train a secondary neural network called a Reward Model (RM). The Reward Model ingests a prompt x and candidate response y, outputting a scalar reward score r_theta(x, y). To train the RM without absolute scalar scoring, you present annotators with pairwise comparison samples (x, y_w, y_l), where y_w is the winning response and y_l is the losing response. You optimize the RM parameters theta by minimizing negative log-likelihood loss:

text
Loss_RM(theta) = -E_(x, y_w, y_l) [ log sigma( r_theta(x, y_w) - r_theta(x, y_l) ) ]

where sigma is the sigmoid function. This loss penalizes your Reward Model whenever it assigns a lower scalar score to the human-preferred output y_w than to y_l.

In the final alignment stage, you optimize your SFT language model against the Reward Model using Reinforcement Learning, typically via Proximal Policy Optimization (PPO).

You define the RL environment as follows:

  • Policy (pi_RL_phi): Your language model parameterized by phi.
  • Action Space: Model vocabulary tokens (V ~ 50,000 to 100,000 token choices per step).
  • Observation Space: Input prompt token distributions.

To prevent your policy model from generating unreadable token strings that overfit or exploit the Reward Model (reward hacking), you add a Kullback-Leibler (KL) divergence penalty to the reward function. This term measures the probability divergence between your active RL policy pi_RL_phi and the baseline frozen SFT policy pi_SFT:

text
r = r_theta(x, y) - beta * D_KL(pi_RL_phi(y|x) || pi_SFT(y|x))

where beta controls penalty scale. Additionally, you can mix in pre-training gradients to prevent performance regression on standard NLP tasks.

Although RLHF elevates model usability, safety, and human preference scores, it introduces the "alignment tax." Empirical evaluations demonstrate that RLHF can increase factual hallucination rates compared to pure SFT. Hallucinations occur because SFT and RLHF force your model to produce helpful, definitive answers even when its internal parameter weights lack the required pre-trained facts.

6. The Data Wall: What Happens When AI Runs Out of Human Text?

Your pre-training scaling velocity faces a physical constraint: the exhaustion of high-quality human text. Modern foundation models demand exponential token scaling (e.g., 1.4 trillion tokens for LLaMA, 15 trillion tokens for FineWeb), consuming public web text faster than human populations create original content.

The Data Wall constraint and strategies for high-efficiency curation and synthetic data

To bypass this data wall, machine learning infrastructure relies on two primary strategies:

  1. Automated High-Efficiency Filtering: Rather than expanding raw token counts, you deploy automated classifiers to extract high-value text. Classifiers like FineWeb-Edu filter multi-trillion token datasets down to educationally dense subsets (e.g., 1.3 trillion tokens at score threshold ≥ 3), matching or exceeding the downstream performance of unfiltered datasets ten times larger.
  2. Synthetic Data Generation: You increasingly train models on outputs generated by frontier LLMs. The influx of synthetic text in web dumps is tracked via statistical proxy terms common in model generations—such as "delve", "rich tapestry", "intertwined", "certainly!", "dive into", "it's important to note", and "as a large language model". While synthetic text maintains performance in limited runs, its impact on multi-epoch, massive-scale pre-training remains an active engineering investigation alongside securing proprietary non-public archives.

References

Check yourself

Three quick questions to see whether the main ideas stuck.

  1. What core objective governs how a large language model learns during the pre-training phase?
  2. Why does a raw pre-trained Base Model fail to function as an interactive chatbot out of the box?
  3. Why does RLHF include a Kullback-Leibler (KL) divergence penalty between the active RL policy and the initial SFT model?

Share this article

AI glossary