🤖 How AI Actually Reads and Thinks: BPE Tokens, Temperature, Top-K, Top-P & Mixture of Experts — Simply Explained
You’ve probably used ChatGPT, Claude, or Gemini. You type something, and the AI replies in seconds. But have you ever wondered what’s actually happening under the hood?
1. BPE Tokenizer — How AI Reads Your Words
The problem AI had to solve
Computers don’t read words the way you do. Early AI models had a simple idea: give every word its own number. “cat” = 42, “dog” = 43, and so on.
But English alone has over 170,000 words — and that’s before you count slang, typos, scientific terms, or words in other languages. This approach exploded in size and still couldn’t handle new or rare words.
So engineers invented something smarter: Byte Pair Encoding (BPE).
How BPE works — the “merge game”
BPE starts from individual characters and learns to merge the most frequently appearing pairs together, over and over.
Imagine the word “unhappiness”. BPE doesn’t store it as one chunk. Instead, it breaks it into learned pieces:
"unhappiness" → ["un", "happ", "iness"]Each piece is a token. The model reads tokens, not words.
Real-world examples
Word Tokens Count “the” [“the”] 1 “unhappiness” [“un”, “happ”, “iness”] 3 “ChatGPT” [“Chat”, “G”, “PT”] 3 “2024” [“20”, “24”] 2 “cat” [“cat”] 1
Common words get 1 token. Rare or long words get split into subwords. This is why AI APIs charge by the token — shorter, simpler text costs less!
The brilliant insight
BPE handles languages it’s never seen words from by breaking them into familiar subword chunks. It never gets completely stuck on an unknown word, because it can always fall back to individual characters.
Simple analogy: BPE is like reading with syllables. You might not know the word “antidisestablishmentarianism,” but you can sound it out piece by piece: “an-ti-dis-es-tab-lish-ment-ar-i-an-ism.”
2. Temperature — The AI’s Creativity Dial
What happens when AI picks the next word
Every time an AI model generates a word, it doesn’t just pick one randomly. It looks at all possible next words and assigns each a probability:
Next word after "The sky is..."
→ "blue" : 45%
→ "clear" : 22%
→ "dark" : 16%
→ "falling" : 9%
→ "purple" : 8%Now here’s where temperature comes in.
Temperature reshapes those probabilities
Temperature is a number, usually between 0 and 2. It controls how wild those probability distributions become.
Low temperature (e.g. 0.1): The most likely word gets even more dominant. The AI almost always picks “blue.”
→ "blue" : 95%
→ "clear" : 4%
→ others : <1%High temperature (e.g. 1.5): The distribution flattens. Everything becomes more equally likely. Expect surprises.
→ "blue" : 24%
→ "clear" : 21%
→ "dark" : 20%
→ "falling" : 18%
→ "purple" : 17%When to use which?
Use case Recommended temperature Code generation 0.1–0.3 Factual Q&A 0.3–0.5 Normal conversation 0.7–1.0 Creative writing 1.0–1.3 Brainstorming / poetry 1.3–1.8
Simple analogy: Temperature is like a coffee-to-sleep ratio. At 0.1, the AI is dead tired and just says the obvious thing. At 1.8, it’s had five espressos and is making unexpected, imaginative (sometimes bizarre) connections.
3. Top-K Sampling — The Shortlist
The problem with pure temperature
Even with temperature, the AI technically considers every word in its vocabulary — all 50,000+ tokens. Many of them are comically wrong. Should “The sky is… QWERTY” ever be an option? No.
Top-K solves this by simply throwing out the bottom candidates before sampling.
How Top-K works
You set a number K. The model looks at all possible next tokens, sorts them by probability, keeps only the top K, and samples from those.
Example with K = 5:
Before Top-K (all 50,000 tokens):
"blue": 45%, "clear": 22%, "dark": 16%, "falling": 9%, "purple": 8%,
"banana": 0.0001%, "QWERTY": 0.000001%, ... (49,995 more)After Top-K (only top 5 remain):
"blue": 47%, "clear": 23%, "dark": 17%, "falling": 8%, "purple": 5%
(renormalized to sum to 100%)The AI now only picks from these 5 sensible options.
The trade-off
- Small K (e.g. 3): Very focused, less varied. Can feel repetitive.
- Large K (e.g. 50): More variety, but might pick some odd words occasionally.
- K = 1: Always picks the single most likely word. Completely deterministic — same input always gives same output.
Simple analogy: Imagine you’re at a restaurant. Top-K is like the waiter only showing you the 5 most popular dishes instead of the full 200-item menu. You still choose, but from a curated, sensible list.
4. Top-P (Nucleus Sampling) — The Smarter Shortlist
The problem with Top-K
Top-K has a flaw: K is fixed regardless of context.
Get Sriram Subramanian’s stories in your inbox
Join Medium for free to get updates from this writer.
Sometimes the model is very confident — maybe 80% sure the next word is “blue”. In that case, allowing 50 alternatives is wasteful and risky.
Other times the model is very uncertain — maybe the top word only has 12% probability. In that case, K=5 might be too restrictive.
Top-P (also called Nucleus Sampling) fixes this by adapting dynamically.
How Top-P works
Instead of fixing the number of tokens, you fix a cumulative probability threshold — P.
The model picks the smallest set of tokens whose probabilities add up to at least P.
Example with P = 0.90 (90%):
Scenario A — Model is confident:
"blue": 85%, "clear": 6% → cumulative = 91% ✅ → only 2 tokens in nucleusScenario B — Model is uncertain:
"blue": 25%, "clear": 20%, "dark": 18%, "falling": 15%, "purple": 14% → cumulative = 92% ✅ → 5 tokens in nucleusThe nucleus shrinks when the model is confident. It expands when uncertain. Beautiful!
Top-K vs Top-P at a glance
Feature Top-K Top-P How it works Fixed number of candidates Fixed probability mass Adapts to confidence? ❌ No ✅ Yes Easy to understand? ✅ Very simple Slightly more complex Used in practice? Yes, often combined Yes, the modern default
Simple analogy: Top-K is like always inviting exactly 5 friends to a party. Top-P is like inviting friends until you’re 90% sure the party will be fun — sometimes that’s 2 people, sometimes it’s 8.
5. Mixture of Experts (MoE) — The AI Hospital
Why we need MoE
Modern AI models are massive. GPT-4 is rumoured to have over a trillion parameters. Running all of them for every single word you generate is astronomically expensive.
Mixture of Experts is the architecture that lets models be huge in capacity but cheap to run.
The core idea
Instead of one giant neural network, you have many smaller “expert” networks — each specialising in different things (grammar, math, code, logic, facts, etc.).
For each token (word) the model generates, a gating network looks at the token and routes it to the best 2 experts (out of, say, 8).
Token: "calculate"
Gating network decides → Route to: [Math expert] + [Logic expert]
Skip: Grammar, Style, Facts, Code, Language, Reasoning expertsToken: "beautiful"
Gating network decides → Route to: [Style expert] + [Language expert]
Skip: Math, Logic, Grammar, Facts, Code, Reasoning expertsThe Mixtral 8x7B example
Mistral’s famous Mixtral 8x7B model has:
- 8 expert networks, each with ~7B parameters = 56B total parameters
- But only 2 experts activate per token = ~13B parameters actually used
So you get the knowledge capacity of a ~56B model running at the cost of a ~13B model. That’s the MoE magic.
Total parameters: ~56 Billion
Active parameters per token: ~13 Billion (only 2 of 8 experts)
Efficiency gain: ~4×Why k=2 is the sweet spot
You might ask: why not activate 4 or 6 experts for even better quality?
Here’s the diminishing returns curve:
Active experts (k) Quality gain vs k=1 Compute cost k = 1 Baseline 1× k = 2 +80% 2× k = 3 +87% 3× k = 4 +90% 4× k = 8 +96% 8× (= full dense model)
The jump from k=1 → k=2 is massive. From k=2 → k=3? Much smaller. After k=4, you’re basically paying full price for tiny gains. This is why Mistral chose k=2.
Simple analogy: Imagine a hospital with 8 specialist doctors. When you arrive with a complaint, a triage nurse (the gating network) sends you to exactly the 2 most relevant specialists — not all 8. You get expert-level care at a fraction of the cost.
Putting It All Together
Here’s how these concepts work together in a single AI response:
You type: "Write a short poem about autumn."
↓
1. BPE tokenizer splits your text into tokens
["Write", " a", " short", " poem", " about", " autumn", "."]
↓
2. Model processes tokens, generates next token probabilities
↓
3. MoE routes each token through 2 specialist experts
↓
4. Temperature (0.8) reshapes the distribution slightly
↓
5. Top-P (0.9) selects the nucleus - top tokens summing to 90%
↓
6. Final token is sampled → "Leaves"
↓
Repeat until the poem is complete...Every single word you read in an AI response went through this pipeline — hundreds or thousands of times per second.
Final Thoughts
These five concepts form the invisible machinery behind every AI conversation you’ve ever had. They’re elegant solutions to real problems:
- BPE solves the vocabulary explosion problem
- Temperature solves the boring vs chaotic balance problem
- Top-K/P solve the ridiculous word choices problem
- MoE solves the can’t afford to run a trillion parameters problem
Understanding them won’t just make you a better AI user — it’ll help you prompt smarter, understand why AI sometimes gives weird answers, and appreciate the engineering that makes it all work.





