How LLMs Are Built: Pretraining, Instruction Tuning, Alignment and Inference Explained Step by Step

LLM Development 45 min min read Updated: Nov 21, 2026 Beginner
How LLMs Are Built: Pretraining, Instruction Tuning, Alignment and Inference Explained Step by Step
Beginner Topic 2 of 3

The Big Picture

Building an assistant like the ones you chat with happens in stages. Each stage has a different goal.

StageWhat happensResult
PretrainingThe model reads huge amounts of text and learns to predict the next tokenA base model that continues text
Instruction tuningIt is trained on examples of questions and good answersA model that follows instructions
Preference alignmentHumans or rules rank answers and the model is nudged toward better onesMore helpful and safer replies
InferenceYou send a prompt and get a replyWhat your app uses

Stage 1: Pretraining

The model sees text with the next token hidden. It guesses, checks how wrong it was, and adjusts its numbers (parameters or weights) a tiny bit. This repeats an enormous number of times.

The wrongness is called loss. A common loss is based on the probability the model gave to the correct token. The lower the loss, the better the guess.

python import math # Probability the model gave to the correct next token for p in [0.9, 0.5, 0.1, 0.01]: print("p(correct) =", p, " loss = -ln(p) =", round(-math.log(p), 3))
Result

p(correct) = 0.9 loss = -ln(p) = 0.105 | p(correct) = 0.5 loss = -ln(p) = 0.693 | p(correct) = 0.1 loss = -ln(p) = 2.303 | p(correct) = 0.01 loss = -ln(p) = 4.605

If the model gave the right token a probability of 0.9, the loss is small. If it gave only 0.01, the loss is large. Training pushes the loss down.

Stage 2 and 3: Making It Helpful

  • Instruction tuning teaches the format of a conversation, such as answering a question directly
  • Alignment uses ranked examples or rules so replies are more helpful, honest and safe
  • Methods have names such as RLHF and DPO. You do not need the math to build apps, but you should know they exist

Stage 4: Inference

Inference is when the trained model answers you. It reads your prompt and produces tokens one by one. Training is done by the model provider. As an app developer you mostly work at inference time, or you fine-tune a smaller model later.

Parameters, Data and Compute

TermMeaning
ParametersThe learned numbers inside the model. Bigger models have more
Training dataThe text the model learned from. Its quality matters a lot
ComputeThe computer power needed to train and to run the model
Knowledge cutoffThe date after which the model saw little or no data
Why this matters to you Because the model learned from data up to a cutoff, it can be out of date. This is why we later add search and RAG to give it fresh information.

Practice

  1. Run the loss demo with p = 0.7 and p = 0.2
  2. Explain in your own words why a lower loss means a better model
  3. Write which stage you would use to teach a model your company's tone

Conclusion

Pretraining gives a model language skill, instruction tuning and alignment make it useful, and inference is where your apps use it.

Quick Summary: Four stages; loss; parameters; cutoff; why apps add fresh data.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators