# How LLMs Are Trained and Aligned

> Pretraining, instruction tuning and preference training explain what models know, why they follow instructions and where their knowledge ends. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/ai-engineer/how-llms-are-trained
Last updated: 2026-11-28

Pretraining, instruction tuning and preference training explain what models know, why they follow instructions and where their knowledge ends.

Beginner · about 4 hours · difficulty 2/5

Learn first: [Transformers and Attention, Intuitively](https://www.internhack.in/roadmaps/tracks/ai-engineer/transformers-attention)

Modern assistant models go through several stages, and each one leaves fingerprints you will see in production.

### The stages

- **Pretraining**: the model predicts the next token over a huge corpus of text and code. This is where knowledge and language ability come from. It also sets a **knowledge cutoff**: the model knows nothing after its training data ends.
- **Supervised fine-tuning (instruction tuning)**: training on example conversations teaches the model to act like an assistant and follow instructions.
- **Preference training**: methods such as RLHF, DPO and constitution-based approaches push the model toward answers humans or rubrics prefer: helpful, honest, harmless.
- **Reinforcement learning on verifiable tasks**: newer models are also trained with rewards for correct math, passing code tests and successful tool use, which is where much of the recent progress in reasoning and agentic ability comes from.

### What this means for you

- Do not rely on the model for recent facts; give it current data (that is what RAG and tools are for).
- The model is trained to be agreeable, so it may accept false premises in a question. Ask neutrally.
- Refusals and safety behaviour come from alignment training; different providers make different trade-offs.
- **Base models** (pretrained only) complete text rather than follow instructions; most applications want instruction-tuned models.

### Good enough to move on

You can explain the difference between a base and an instruct model, what a knowledge cutoff is, and why grounding with external data is needed.

## Key concepts

- **Pretraining and knowledge cutoff**
- **Instruction tuning (SFT)**
- **RLHF, DPO and preference training**
- **RL on verifiable rewards**
- **Base vs instruct models**
- **Hallucination: why it happens**

## Mini project

Run the same five prompts against a base model and an instruct variant of the same open model family (via Hugging Face or Ollama) and document the behavioural differences.

## Check yourself

A user asks your assistant about an event from last week and gets a confident but wrong answer. Which training stage explains this, and what is the engineering fix?

## Free resources

- [Deep Dive into LLMs like ChatGPT (Andrej Karpathy)](https://www.youtube.com/watch?v=7xTGNNLPyMI) (YouTube)
- [Illustrating RLHF](https://huggingface.co/blog/rlhf) (Hugging Face)

Previous: [Transformers and Attention, Intuitively](https://www.internhack.in/roadmaps/tracks/ai-engineer/transformers-attention) · Next: [Sampling, Temperature and Determinism](https://www.internhack.in/roadmaps/tracks/ai-engineer/sampling-and-decoding)

## More in How LLMs Work (An Engineer's View)

- [Tokens and Context Windows](https://www.internhack.in/roadmaps/tracks/ai-engineer/tokens-and-context)
- [Transformers and Attention, Intuitively](https://www.internhack.in/roadmaps/tracks/ai-engineer/transformers-attention)
- [How LLMs Are Trained and Aligned](https://www.internhack.in/roadmaps/tracks/ai-engineer/how-llms-are-trained)
- [Sampling, Temperature and Determinism](https://www.internhack.in/roadmaps/tracks/ai-engineer/sampling-and-decoding)
- [Reasoning Models and Thinking Budgets](https://www.internhack.in/roadmaps/tracks/ai-engineer/reasoning-models)
- [The Model Landscape and Choosing a Model](https://www.internhack.in/roadmaps/tracks/ai-engineer/choosing-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [AI Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/ai-engineer)
