How an LLM works, for engineers
Tokens, the context window, sampling and why models make things up — the mental model every other topic builds on.
☰
An LLM does one thing, over and over: it reads everything in its window and guesses the next token. Everything else — chat, tools, 'reasoning' — is built on that loop.
+ The whole system in one paragraph− Hide
A large language model turns your text into tokens, scores every possible next token, picks one, adds it to the text and repeats. It only sees what's in its context window, remembers nothing between calls, and was trained to predict likely text — which is also why it can state wrong facts with confidence.
In this lesson
3 chapters · ~6 min · a quick check in eachEverything is on this page — just scroll down ↓
New to this? Warm up first+ optional
Words you'll meet
Skim these now and come back whenever one shows up. Every other AI-engineering topic uses them.
ⓘ Details & sources− Hide details
The terms, in plain words.[1]
The big picture
From your text to the answer
Start here: the whole machine in five steps. The model never writes a sentence at once — it guesses one token, adds it, and reads everything again for the next one.
ⓘ Details & sources− Hide details
Read everything, guess one token, append, repeat.[1]
5 steps. Follow the flow one step at a time — what happens first, and what comes next.
Try it: the temperature dial
Cold, it says 'Paris' every time; hot, the long shots start winning. The numbers are an example; the reshaping is the real math. Even at 0, real APIs aren't fully repeatable.
ⓘ Details & sources− Hide details
Same scores, different picks: temperature reshapes the odds before one token is chosen.[1]
What the model actually reads
The model never sees letters or words, only token numbers. That's why prices and limits are in tokens, and why the same text counts differently on different models. The split shown is illustrative.
ⓘ Details & sources− Hide details
Tokens, not words: common words stay whole, rare ones split.[1]
What it can see: the context window
Each call, everything must fit in one window: instructions, documents, the chat, the question and the answer it writes. More isn't always better: recall drops as it fills.
ⓘ Details & sources− Hide details
Outside the window, the model knows nothing about your conversation.[2]
Curious? Go deeper+ 2 optional
Your app sends the whole prompt; the tokenizer cuts it into token numbers.
Every call starts from zero
The model keeps no memory between calls. To 'remember' a conversation, your app sends the whole history again every turn — which is why long chats get slower and pricier.
4 steps. Follow the flow one step at a time — what happens first, and what comes next.
Your chatbot's second answer ignores what the user said in their first message. What's the most likely cause?
Pick one — or just keep scrolling, it's not graded.
Keep going — it's free
Create your free account to unlock the rest of this lesson, keep your progress and pick up right where you left off.
- 2How it got this way
- 3Why it makes things up