Module 1 of 26 · Open Source Track
The LLM
Mental Model

Before you build agents, get the mental model right: an LLM is a next-token predictor, not a fact database. Four concepts — what an LLM is, the model zoo, how inference runs, and the sampling knobs that shape every answer.

Track 1: Foundations ⏱ ~15 min read 1 / 26
Concept 1 of 4

A machine that predicts the next token

A large language model is, at its core, a next-token predictor. Given the text so far, it outputs a probability for every possible next token (a word-piece) and picks one. Repeat, and you get sentences.

Nobody programmed its answers. It was trained on an enormous amount of text and learned the statistical patterns of language — grammar, facts, styles, reasoning shapes. That's why it can answer things no one wrote a rule for, and also why it can confidently make things up: it's predicting plausible text, not looking up truth.

Concept 1 of 4

From hardcoded phone tree to learned patterns

BEFORE: Old chatbots were a giant phone tree — a human wrote every branch: "if the user says X, reply Y." Say anything off-script and it broke.

PAIN: Language is infinite; you can't enumerate every question. Hand-writing rules for each one is impossible, so rule-based bots were rigid and shallow.

MAPPING: An LLM never got the branches. It read billions of examples and learned the patterns of how text continues. Ask something new and it predicts a plausible continuation — flexible and open-ended, but grounded in likelihood, not a lookup table of facts.

Concept 1 of 4

Train once, predict forever

  1. Training (once)The model reads a huge corpus and adjusts billions of weights to get better at predicting the next token.
  2. Frozen weightsAfter training, the weights don't change. Running the model (inference) only reads them — it doesn't learn from your chat.
  3. Prediction (every call)Given your prompt, it computes next-token probabilities and samples one.
  4. Repeat to generateAppend the token, feed it back, predict again — until a stop signal. That loop is the whole show.
Concept 1 of 4

When is an LLM the right tool?

# Reach for an LLM when...

IF the task is language-shaped
   (summarize, classify, rewrite, converse)
   AND a few wrong answers are tolerable:
  USE an LLM

ELIF you need an exact, always-correct
      computation (math, lookups, totals):
  USE code / a database
  # or give the LLM a TOOL that does it

ELSE:
  # combine: LLM to decide, tools for truth

The whole course is about that last line: LLM for judgment, tools for facts.

Concept 1 of 4

Misconceptions

"The model looks up answers in a database."
There's no database inside. It predicts likely text from learned patterns — which is why it can be fluent and wrong at the same time (a "hallucination").
"It learns from my conversation."
Inference doesn't update the weights. Anything it "remembers" within a chat is just text you kept in the prompt — not learning.

An LLM predicts the next token from learned patterns. Fluent, flexible, and probabilistic — not a fact lookup.

That single truth explains both its magic (open-ended language) and its risk (confident hallucination).

Concept 2 of 4

"AI" is a zoo, not one animal

"LLM" isn't one thing. A base model just continues text. An instruct model is tuned to follow commands. A chat model is tuned for back-and-forth turns. And there are specialists: embedding models (text→vectors), vision models (images), code models.

Size matters too: a 7B model like Mistral runs on a laptop; a 70B needs a serious GPU. Picking the right species — and the smallest one that does the job — is half of building efficient agents.

Concept 2 of 4

One word, many species

BEFORE: When people say "AI," they picture one thing: a chatbot that takes a question and gives an answer.

PAIN: That single image hides real differences. Handing a raw base model a chat prompt, or using a giant model where a tiny one would do, wastes money and produces confusing results.

MAPPING: "AI model" is like the word "vehicle" — it covers a bicycle, a truck, and an ambulance. You wouldn't take a bicycle to move furniture. Knowing the species (base vs instruct vs chat, small vs large, text vs vision) lets you pick the right vehicle for the trip.

Concept 2 of 4

The main species

  1. BaseRaw next-token continuation. Great for completion, awkward for instructions — it just keeps writing.
  2. Instruct / ChatFine-tuned to follow commands and hold a conversation. This is what you use for agents (Mistral-Instruct).
  3. EmbeddingTurns text into a vector of numbers for search and similarity — the engine behind RAG (M09).
  4. Vision / CodeSpecialists for images or programming. Same core idea, different training diet.
Concept 2 of 4

Which model for the job?

IF building a conversational agent:
  USE an instruct/chat model  # e.g. Mistral

IF you need search / similarity:
  USE an embedding model      # not a chat model

IF the input is images:
  USE a vision model          # e.g. LLaVA

# Then pick the SMALLEST size that passes
# your evals — bigger = slower + costlier.

Default to the smallest model that clears your quality bar; scale up only when it fails.

Concept 2 of 4

Misconceptions

"Bigger is always better."
Bigger is slower and costlier. A 7B model that passes your evals beats a 70B you can't afford to run on every request. Right-size for the task.
"An embedding model can chat."
No — it only produces vectors. Use a chat model to talk and an embedding model to search. They're different species for different jobs.

"AI model" spans many species: base, instruct/chat, embedding, vision, code. Match the species to the task.

Then pick the smallest size that clears your bar — that's how open-source agents stay fast and free.

Concept 3 of 4

Read all at once, write one at a time

Inference is running the trained model to generate text. A transformer reads your entire prompt in parallel (that's what "attention" enables), building a rich picture of context all at once.

But it writes one token at a time: predict the next token, append it, then re-read the whole thing and predict again. This is why responses stream in word by word, and why longer outputs take longer — each token is a fresh forward pass.

Concept 3 of 4

Skimming the whole page vs reading letter by letter

BEFORE: Older models (RNNs) read text strictly in order, one word after another, carrying a running summary — like sounding out a sentence letter by letter.

PAIN: Reading serially is slow and forgetful: by the end of a long sentence, the beginning has faded, so long-range connections get lost.

MAPPING: A transformer skims the whole page at once and lets every word "look at" every other word (attention). Nothing fades, and it runs in parallel — then it composes the reply one token at a time, always re-reading the full page before the next word.

Concept 3 of 4

One generation step

  1. TokenizeYour prompt is split into tokens and turned into vectors.
  2. Attention over everythingEvery token attends to every other, in parallel, building context-aware representations.
  3. Predict next tokenThe model outputs a probability for every possible next token.
  4. Sample & appendPick one token (see Concept 4), append it, and loop — re-reading the whole sequence each time until a stop token.
Concept 3 of 4

Pseudocode

# autoregressive generation = a loop
tokens = tokenize(prompt)

WHILE TRUE:
  probs = model(tokens)         # attention over ALL tokens
  next  = sample(probs)         # pick one (temperature...)
  IF next == END_OF_TEXT:
    BREAK
  tokens.append(next)           # feed it back in
  stream(next)                  # word appears on screen

# each token = one full forward pass ->
# longer output = proportionally slower
Concept 3 of 4

Misconceptions

"The model plans the whole answer, then types it."
It commits one token at a time, each conditioned on all previous ones. There's no pre-written full answer — the reply emerges token by token.
"Output length doesn't affect speed much."
Every token is a separate forward pass, so a 500-token answer takes roughly 5× as long as a 100-token one. Output length is a real cost lever.

Transformers read the whole context in parallel (attention) and generate one token at a time.

That's why replies stream, why long outputs are slower, and why what's in your context window matters so much (M02).

Concept 4 of 4

Knobs that turn probabilities into a pick

At each step the model has a probability for every next token. Sampling controls decide how to choose. Temperature reshapes the distribution: low (0–0.3) sharpens toward the top choice (focused, repeatable); high (0.8+) flattens it (varied, creative).

Top-k keeps only the k most likely tokens; top-p (nucleus) keeps the smallest set whose probabilities sum to p. Both trim the long tail of unlikely tokens before sampling — guardrails against nonsense.

Concept 4 of 4

The one-dish diner vs the tasting menu

BEFORE: With no sampling controls, the model always picks the single highest-probability token — a diner that only ever serves its #1 dish.

PAIN: Always-the-top is repetitive and can get stuck in loops. Always-random is chaos — it'll serve you something inedible from the bottom of the menu.

MAPPING: Temperature is how adventurously you order; top-k/top-p is trimming the menu to only the good dishes first. Low temperature + tight top-p = reliable favorites; higher = a varied tasting menu, still from the dishes worth eating.

Concept 4 of 4

From probabilities to a token

  1. Model outputs probabilitiesOne score per possible next token.
  2. Temperature reshapes themLow sharpens toward the top; high flattens toward even odds.
  3. Top-k / top-p trims the poolDrop the unlikely tail so nonsense can't be chosen.
  4. Sample oneDraw from what's left. Temperature 0 = always the top token (as deterministic as the backend allows).
Concept 4 of 4

Pick temperature by task

IF extraction, classification, JSON,
   tool-calling, anything needing
   repeatable / exact output:
  temperature = 0 – 0.2      # focused

ELIF brainstorming, drafting,
      creative writing:
  temperature = 0.7 – 1.0    # varied

ELSE:
  temperature = 0.3 – 0.5    # balanced default

Agents mostly want low temperature — you're wiring behavior, not writing poetry.

Concept 4 of 4

Misconceptions

"Temperature is a 'smartness' or 'creativity' dial."
It only changes how the existing probabilities are sampled — not the model's knowledge. High temperature adds variety and risk; it doesn't make the model smarter.
"Temperature 0 guarantees identical output."
It removes sampling randomness, but hardware/backend non-determinism can still cause tiny differences. It's as repeatable as you can get, not a cryptographic guarantee.

Temperature reshapes the distribution; top-k/top-p trim the candidate pool. Low = focused and repeatable, high = varied.

For agents, default low — predictable behavior beats surprise almost every time.

Test the core insight

One question per concept. Tap to reveal the answer.

Open the full module on desktop

The desktop version runs real inference against a local Mistral via Ollama, visualizes attention and sampling live, and walks your first model API call in Python and Node — the hands-on "Hello, model" that turns this mental model into working code.

Open M01 on Desktop → Live inference · sampling demo · first API call · ~50 min

Module 1 of 26 · Open Source Track · Foundations