A model that predicts the next word, three layers of training, and a handful of clever runtime tricks - that's the entire LLM story.
ChatGPT launched in late 2022 on top of GPT-3.5. Four years later, in 2026, frontier LLMs carry over a trillion parameters, context windows spanning millions of tokens, and can "think before answering." The core is still something very simple: predicting the next token. This piece peels back the layers visually, enough for a mid-level software engineer to grasp the whole stack - from the tokenizer, the transformer, pretraining, RLHF, Claude's Constitutional AI, all the way to KV cache, speculative decoding, MoE, and reasoning.
Who is this for? Mid-level software engineers who've heard terms like "transformer," "RLHF," "inference," "MoE" but haven't yet pieced them together into one picture. No deep math background needed - just basic matrix and probability knowledge, and some coding experience.
What this isn't. Not a textbook. No derivatives, no gradient descent from scratch. The goal: build a mental model accurate enough to read papers, write prompts, choose models, and debug LLM apps without getting the technical details wrong.
1 · The whole LLM story in one sentence
An LLM is a function that takes in a sequence of tokens and returns a probability distribution over its entire vocabulary for the next token. That's the whole API at its core. Everything flashy - writing code, solving math, reasoning, writing prose - is just the surface of calling that function over and over, each time grabbing one new token and appending it to the end of the sequence.
mat wins. Append it to the end, run again. Repeat until a stop token appears or the budget runs out."ChatGPT" might become ["Chat", "G", "PT"]; "playing" becomes ["play", "ing"]. This lets a finite vocabulary cover proper nouns, rare words, and unfamiliar languages - and explains why character counts in an LLM never match your estimate.
An annoying consequence: an LLM doesn't know individual characters. It sees "strawberry" as roughly 2–3 merged tokens. Asking "how many r's?" is asking it to count something it has never actually seen. Most of an LLM's "dumb" mistakes live in this tokenizer layer, not in the reasoning layer.
2 · The Transformer - the heart of every LLM
Every modern LLM - GPT, Claude, LLaMA, Gemini, Mistral, DeepSeek, Qwen - uses the Transformer architecture (Vaswani et al., Google, 2017, "Attention Is All You Need"). The version used for text-generating LLMs is called a decoder-only transformer. The structure is remarkably repetitive: the same block stacked 30–120 times.
Self-attention in 60 seconds
For every token, three vectors are produced: Q (query - "what am I looking for?"), K (key - "what am I?"), V (value - "what information do I carry?"). The current token compares its query against the keys of every preceding token, producing match scores, then blends the values weighted by those scores. The result: every token gets "rewritten" with its surrounding context.
cat), Head 2 (blue) catches the verb (saw), Head 3 (green) catches nearby words. Many heads run in parallel and get concatenated - that's the "multi-head" part.3 · Timeline - from GPT-3 to the 2026 generation
Before ChatGPT, LLMs were an academic toy. After ChatGPT, they became a product. Below are the milestones that shaped the industry, compressed into less than four and a half years.
4 · How training works - three stages
A model like Claude or GPT-4 isn't trained just once. It passes through three stages that differ enormously in data, objective, and cost. Understanding these three stages is the key to understanding why LLMs "behave" the way they do.
Pretraining - where all the "knowledge" comes from
Pretraining is the data-ingestion stage. Everything an LLM "knows" about the world - Python syntax, Roman history, a pho recipe - enters here. The dataset is a blend: Common Crawl (heavily filtered), Wikipedia, books, GitHub code, arXiv, Stack Exchange, blog posts. Quality filtering is the most closely guarded part - bad data makes a bad model.
After pretraining, you have a base model. It knows how to write Python code, knows Vietnamese grammar, knows who Trần Hưng Đạo was - but if you ask it "Hi, what are you up to?" it will answer something like: "I'm writing a post. What about you? - I'm just relaxing. - Nice, me too..." It's just continuing text, it doesn't know what "answering" means. That's the job of the next two stages.
SFT - teaching the model to "answer"
SFT = Supervised Fine-Tuning. Fundamentally the same as pretraining (next-token prediction), but the data is tens to hundreds of thousands of user question → assistant answer pairs, hand-written or curated. The model learns the chat format, an assistant's demeanor, and where to stop.
SFT data shapes the model's initial "personality." If every answer is long, polite, and bulleted - the model learns to match it. This is why every chatbot sounds a bit alike: the same pool of contractor raters, the same style guide.
RLHF - teaching the model to "answer better"
SFT teaches "one correct answer." RLHF (Reinforcement Learning from Human Feedback) teaches degree of quality. The classic recipe was popularized by OpenAI in 2022 (InstructGPT, the foundation of ChatGPT):
- Generate: the model answers the same prompt 2-4 times.
- Rate: human annotators rank the answers.
- Reward model: train a small model to predict the rater's score.
- RL: use the reward model as a "teacher"; PPO optimizes the policy (the LLM) to raise expected reward, with a KL penalty to keep it from drifting too far from the base model.
RLHF is what turned GPT-3 into ChatGPT. With the same base model, before RLHF it was useful roughly 30% of the time; after RLHF, 80%+. Alternative techniques have since emerged:
5 · Constitutional AI - Claude's path
Anthropic was founded in 2021 by a group of ex-OpenAI researchers, including Dario & Daniela Amodei and key authors of the GPT-3 paper, with the thesis: RLHF's bottleneck is humans - it doesn't scale, isn't consistent, and isn't transparent about values. The proposed solution: Constitutional AI (CAI), a 2022 paper.
Claude's constitution is partially public - drawing from the UN Universal Declaration of Human Rights, Apple's terms of service, and AI research principles. This doesn't mean Claude "reads the constitution" every time it answers - the principles are baked into the weights through the RL stage.
Character training - another layer of Claude
Anthropic has publicly disclosed an additional step called character training (starting around Claude 3, 2024): using CAI-like techniques to shape personality (as opposed to just "safety"). Claude is taught traits like curiosity, candor when warranted, humility about its own limits, and not feigning agreement. This is why Claude is often noted to have a distinct "voice" compared to GPT or Gemini.
6 · Inference - runtime optimization
Train once, run billions of times. Inference is where your cloud bill comes from, and where ML engineers have poured the most effort in 2023-2026. The techniques below stack together - a single Claude API request typically benefits from nearly all of them.
KV cache - the simplest trick, the biggest impact
When generating the 100th token, attention needs the K and V of the preceding 99 tokens. Recomputing from scratch for every token costs O(n²). The KV cache simply stores the K, V once computed. From the second token onward, only the new K, V gets computed. O(n²) → O(n).
Speculative decoding - one draft + one verify
Large models run slowly (each token is a full pass through all the weights). The idea: use a small, fast draft model to guess k tokens ahead. Then the large model checks all k tokens in one pass. Tokens the draft guessed correctly are accepted. The first wrong token gets corrected. Result: if the draft guesses right 70% of the time, speed increases ~2-3x with no quality loss (mathematically, the output distribution is identical).
Four tokens produced in one forward pass of the target model instead of four separate passes.
MoE - splitting into experts
Mixture of Experts: instead of activating all 70B parameters for every token, say 400B parameters are split into N "experts" (typically N=8, 32, or 256). For each token, a small router selects the top-k experts (usually k=1-2). Only k/N of the weights get computed. Result: large capacity (more parameters = more knowledge), small compute (only a fraction is active).
Quantization - fewer bits, kept quality
Weights are usually trained in FP16/BF16 (16-bit). After training, they can be compressed to INT8, INT4, even FP4 with very little loss. LLaMA 3 70B needs 140GB in FP16; only 40GB in INT4 - runnable on a single consumer GPU. This is why you can run LLaMA on a MacBook M-series.
Test-time compute - answering slower to answer correctly
A second scaling axis appeared in late 2024 with OpenAI o1: instead of making the model bigger, let it generate more tokens before the final answer. Reasoning tokens (thinking/reasoning tokens) get rewarded when they lead to the correct answer (via RLVR on math/code). More compute at runtime → better answers. AI economics flipped: now you can pay more per answer to get a smarter one.
Claude has had "extended thinking" since Claude 3.7 (2025): you flip a flag, the model generates up to tens of thousands of hidden reasoning tokens, then returns a concise final answer. For hard bugs in code, the gap between "thinking off" vs "thinking on" is sometimes several dozen percentage points of accuracy.
7 · Open source - the new playing field
By 2026, open weights are no longer a toy. DeepSeek R1, LLaMA 4, Qwen 3, Mistral Large - all compete neck-and-neck with the closed frontier on many benchmarks. "Open weights" doesn't mean true open source (code + data + pipeline). Most only publish weights; the data and training process stay closed. But it's enough for you to: run locally, fine-tune, and audit behavior at the input-output level.
| Model | Params (total / active) | Context | Type | Notes |
|---|---|---|---|---|
| Claude Opus 4.7 Anthropic · 2026 | - / - | 1M | Closed | Frontier reasoning + character. Architecture undisclosed. |
| GPT-5 OpenAI · 2025 | - / - | 400k+ | Closed Reasoning | Unifies GPT + o-series. Router auto-selects the thinking level. |
| Gemini 3 Pro Google · 2025 | - / - | 2M | Closed MoE | Longest context. Native multimodal. |
| LLaMA 4 Behemoth Meta · 2025 | ~2T / ~288B | 10M (subset) | Open MoE | The largest open model. Teacher for the smaller LLaMA 4 variants. |
| DeepSeek V3 / R1 DeepSeek · 2024-25 | 671B / 37B | 128k | Open MoE Reasoning | Low reported training cost (~$6M). The first open-source reasoning model competitive with o1. |
| Qwen 3 / QwQ Alibaba · 2025 | 235B / 22B | 256k | Open MoE Reasoning | Strong in Chinese, multilingual across Asian languages. Many sizes from 0.5B to 235B. |
| Mistral Large 2 Mistral · 2024 | 123B / 123B | 128k | Open-ish | Dense (not MoE). Restricted commercial license. |
| Gemma 3 Google · 2025 | 27B / 27B | 128k | Open | Runs well on a single consumer GPU. Multimodal. |
Are "Open Source" LLMs really open source?
Most of what the media calls "open-source LLM" is actually open weights - weights published for download, running, and fine-tuning. But the data, training code, hyperparameters, logs - the things you'd need to rebuild the model from scratch - are almost always kept closed. In 2024, the OSI (Open Source Initiative) published an official Open Source AI definition requiring all four: weights, code, data (or details sufficient to reproduce it), and a license permitting modification + redistribution.
By that standard, most "open" LLMs today fail to qualify. Below is a spectrum of four levels, from fully closed to OSI-compliant open source:
- ✗ Weights
- ~ Architecture (hints)
- ✗ Training data
- ✗ Training code
- ~ Model card / safety report
- ✗ Open license
- ✓ Weights (downloadable)
- ✓ Architecture + paper
- ✗ Training data
- ✗ Training code
- ~ High-level recipe
- ~ License: blocks >700M MAU, blocks training other models on the output, acceptable-use policy
- ✓ Weights
- ✓ Architecture + paper
- ✗ Training data
- ~ Inference code (ref impl)
- ~ Partial training recipe
- ✓ License: Apache 2.0 / MIT - free commercial use
- ✓ Weights + checkpoints
- ✓ Architecture + paper
- ✓ Public training data (Dolma, Pile)
- ✓ Training code + configs
- ✓ Log, loss curve, seeds
- ✓ License Apache 2.0
For engineers: don't let "open source" marketing fool you when assessing legal standing or reproducibility. Ask directly: are there weights? what license? where's the training data? can it be retrained? Those four questions separate an LLM you can truly own from one you're merely "borrowing."
8 · A mental model for engineers
If you had to keep only five things to build an LLM app correctly, here they are:
temperature=0 is not deterministic (it depends on batching, hardware). For tight reproducibility, use a seed + be careful with prompt caching.
03 Discussion
Leave a note
A considered space for questions, counterpoints, and useful additions. Civil, on-topic, signed.
Reader notes
...Loading notes...