Agentic AI Chapter 2 / 16 · How LLMs work

02 How LLMs actually work

It's autocomplete. Just… enormous.

An LLM doesn't "know" facts or "understand" your question the way you do. Underneath, it does one thing over and over: predict the next token. Everything an agent can do is built on this single, humble trick. Let's watch it happen — and meet the two dials, temperature and the context window, that shape every answer.

Tokens Next-token prediction Sampling Temperature Context window

Step zero

Models don't read letters. They read tokens.

Before anything else, your text is chopped into tokens — chunks that are often whole words, sometimes word-pieces. Each token maps to a number (an ID), because a neural network only does math on numbers. "Attention" might be one token or two; a space usually rides along with the word after it. Watch a sentence become tokens.

7 tokens, 7 IDs. The model only ever sees the bottom row of numbers.

The whole engine

Predict one token. Append. Repeat.

Given everything so far, the model outputs a probability for every possible next token — often 100,000+ of them. It picks one, sticks it on the end, and feeds the whole thing back in to predict the next. That loop, called autoregression, is all a language model does. A paragraph is just this step run a few hundred times. Step through it:

The cat sat on the
Top candidates for the next token, by probability. The highlighted one gets chosen and appended.
Why this matters for agents

Because it only predicts likely text, an LLM can sound completely confident and still be wrong — a hallucination. That single fact is why agents need tools, memory, and guardrails, which is the rest of this course.

Dial #1

Temperature: the creativity knob.

The model gives a distribution — but how boldly should it pick? Temperature reshapes those probabilities before sampling. Turn it down and the top choice dominates: safe, repetitive, near-deterministic. Turn it up and the odds flatten: surprising, creative, riskier. Same model, same prompt — drag the slider and watch the distribution breathe.

“My favorite thing to do on a weekend is ___
temperature 0.70
0.1 · focused1.6 · wild
Low temperature ≈ a calculator (same answer). High temperature ≈ a brainstormer (different every time).

Dial #2

The context window: the model's short-term memory.

A model can only "see" a fixed number of tokens at once — its context window. Everything in the window (your prompt, the chat history, retrieved documents) shapes the answer. But the window is finite: as a conversation grows past it, the oldest tokens slide out and are simply gone. The model isn't remembering your earlier messages — it's re-reading whatever still fits. Add tokens and watch the front of the conversation fall off the edge.

0 tokens · window holds 12
Real windows hold thousands to millions of tokens — but the principle is identical: full means the oldest is forgotten.

How big are real windows?

Roughly, as of 2025–26 — a token is about ¾ of a word:

GPT / Claude ~200K tokens
Gemini 2.5 Pro ~1M tokens

1M tokens ≈ feeding it a small library at once.

But bigger isn't always better

More tokens means more cost, slower responses, and models can lose focus on things said earlier in a long conversation ("lost in the middle"). Window size is a real engineering trade-off — not just a bragging right.

🔌 Where this is going

No persistent memory + a finite window is a real limitation. The fix is giving the model an external memory and a search tool — retrieval-augmented generation (RAG), vector databases, and the agent loop. That's Chapters 6–9.

Chapter 2 in one breath

Tokens in, one token out — on a loop.

An LLM tokenizes your text, predicts the next token from a probability distribution, and repeats. Temperature decides how boldly it picks; the context window decides how much it can see. It has no memory of its own — and closing that gap is what turns a language model into an agent.

🖥 Present this chapter

A matching slide deck with speaker notes — press S for notes, F for fullscreen.

Open the slides →