Lesson 1: What Is an LLM?
Understand what large language models are, why they predict the next token, and the mental model that unlocks everything else.
What Is an LLM?
A Large Language Model (LLM) is a deep neural network — usually a transformer — trained on enormous amounts of text to do one deceptively simple thing: predict the next token in a sequence.
That single task, repeated billions of times, produces models like ChatGPT, Claude, and Gemini that can write code, summarize documents, answer questions, and act as agents.
The One-Sentence Mental Model
An LLM is an extremely advanced autocomplete. It reads everything you give it (your prompt), and then generates text one token at a time, choosing each next token based on patterns it learned during training.
How Generation Actually Works
| Stage | What Happens |
|---|---|
| 1. Tokenize | Your prompt is split into tokens (words, parts of words, punctuation) |
| 2. Encode | Each token becomes a numeric vector (embedding) |
| 3. Predict | The model computes a probability for every possible next token |
| 4. Sample | A token is picked (greedy, random, temperature-based…) |
| 5. Repeat | The new token is appended and the model predicts again |
This loop is called autoregressive generation — the output feeds back into the input, one token at a time.
What an LLM Is vs. Isn't
| An LLM is… | An LLM is NOT… |
|---|---|
| A next-token predictor | A database of facts |
| A pattern-matching engine | A search engine |
| A reasoning tool (with limits) | A calculator (do math in code) |
| A tool that follows patterns | A tool that "knows" truth |
Why This Matters for AI Tools
Almost every AI tool you will use — chatbots, code assistants, agent frameworks, RAG pipelines — is a wrapper around this next-token prediction loop. When you understand the loop, you understand why:
- Prompts matter: the input shapes the probability distribution of the output.
- Context matters: the model only "sees" what is inside its context window.
- Hallucinations happen: the model picks plausible tokens, not true tokens.
Key Takeaways
- An LLM predicts the next token, one step at a time (autoregressive generation).
- Generation = tokenize → encode → predict → sample → repeat.
- It is a pattern engine, not a fact database — always verify important outputs.
- Every AI tool you use is built on top of this loop.
Next up: The transformer architecture and self-attention — how the model decides which words matter.
# Simulating how an LLM picks the next token
import math
import random
# Imagine the model's scores (logits) for the next token
candidates = ["Paris", "London", "Berlin", "Madrid"]
logits = [5.2, 2.1, 1.4, 0.3]
# Convert logits to probabilities (softmax)
exps = [math.exp(x) for x in logits]
total = sum(exps)
probs = [e / total for e in exps]
print("Next-token probability distribution:\n")
for token, p in zip(candidates, probs):
print(f" {token:8s} -> {p:.4f} ({p*100:.1f}%)")
# Greedy decoding always picks the most likely token
best = candidates[probs.index(max(probs))]
print(f"\nGreedy choice: {best}")
# Sampling may pick a different token (like real LLMs do)
random.seed(7)
sample = random.choices(candidates, weights=probs, k=1)[0]
print(f"Sampled choice (seed=7): {sample}")Lesson Code (Python)
# Simulating how an LLM picks the next token
import math
import random
# Imagine the model's scores (logits) for the next token
candidates = ["Paris", "London", "Berlin", "Madrid"]
logits = [5.2, 2.1, 1.4, 0.3]
# Convert logits to probabilities (softmax)
exps = [math.exp(x) for x in logits]
total = sum(exps)
probs = [e / total for e in exps]
print("Next-token probability distribution:\n")
for token, p in zip(candidates, probs):
print(f" {token:8s} -> {p:.4f} ({p*100:.1f}%)")
# Greedy decoding always picks the most likely token
best = candidates[probs.index(max(probs))]
print(f"\nGreedy choice: {best}")
# Sampling may pick a different token (like real LLMs do)
random.seed(7)
sample = random.choices(candidates, weights=probs, k=1)[0]
print(f"Sampled choice (seed=7): {sample}")Console Output
Next-token probability distribution:
Paris -> 0.9303 (93.0%)
London -> 0.0419 (4.2%)
Berlin -> 0.0208 (2.1%)
Madrid -> 0.0069 (0.7%)
Greedy choice: Paris
Sampled choice (seed=7): ParisCode Visualization Tips
- Draw the token loop as a circle: Prompt → Tokenize → Predict → Sample → Repeat — label each stage with a real example.
- Print the softmax distribution as a horizontal bar chart and color the winning token differently.
- Trace a single sentence through the 5 stages on paper, writing the token-by-token decisions.
Professional Tips & Tricks
- When an LLM gives a surprising answer, ask yourself: 'which pattern in my prompt made that token likely?'
- Keep prompts self-contained — the model has no memory outside the current context window.
- For factual tasks, treat LLM output like a draft from a very confident intern: verify before trusting.
Python Code Judge & Practice Arena
LeetCode StyleRun real Python 3.12 WebAssembly code directly in your browser against automated test suites.
Trace the Generation Loop
Test Your Knowledge
Instant feedbackQuick Check: What Is an LLM?
Up next · Continue learning
The Transformer & Self-Attention
Peek inside the model: embeddings, the transformer stack, and the self-attention mechanism that decides which words matter.