Chapter 1: Words in, words out

One word at a time

A reply is built by predicting the next piece of text, over and over, with a roll of weighted dice.

Lesson 1 of 9 · 3 min read · By the Learnfold team · Updated

When you send a message to ChatGPT or Claude, the reply looks like a finished thought being typed out for show. It isn't. The model builds the reply one small piece at a time, and every piece is a fresh decision.

Here is the whole loop. The model reads everything so far, your message plus whatever it has already written. It gives a score to every possible next piece of text, tens of thousands of candidates. One gets picked and stuck on the end. Then the loop runs again with that piece included. A 300-word answer is a few hundred trips around the loop.

Scores become odds

The raw scores aren't probabilities yet. A step called softmax turns them into odds that add up to 100 percent. After "The cat sat on the", a model might give "mat" the best odds, "floor" and "sofa" a decent chance, and "moon" almost none.

Then the model's job is done. It doesn't choose. A few lines of ordinary code called the sampler roll dice weighted by those odds, and whatever comes up is the next word.

Temperature

Before the dice roll, the scores get divided by a setting called temperature. Below 1, the gaps between scores grow, so the favorite pulls further ahead. Above 1, the gaps shrink and long shots get a real chance. At 0, the sampler skips the dice and always takes the top choice.

Try it. The odds are made up, but the math is the real thing.

Interactive. Try it here or in the app.

Slide the temperature to 0 and sample a few times. You get "mat" every time. Push it to 2 and "keyboard" starts turning up. Neither end is correct. Low temperatures suit jobs with one right answer, like pulling a date out of an email. Higher ones give variety, which helps a brainstorm and hurts a calculation. Most chat apps pick a setting for you and don't show it.

The dice explain two things you've probably noticed. Ask the same question twice and you can get two different answers. And a model can talk itself into a corner. It never goes back to change a word, so once an unlikely word is picked, everything after it has to make sense of it.

Small task, big consequences

"Predicting the next word" sounds trivial. It isn't. To predict the next line of a chemistry textbook well, it helps to have absorbed some chemistry. To continue a detective story, it helps to keep track of who was in the room. Whether that adds up to understanding is a real debate, and you'll meet it at the end of this course. The mechanism isn't up for debate. One piece at a time, picked by dice the model loaded.

One correction before moving on. I've been saying "word". Models don't actually work in words, and the next lesson shows what they use instead.

Rabbit holes

Questions worth chasing. In Learnfold, tap one and your tutor answers it as a new lesson in your course.

  • Why does lowering the temperature make answers more repetitive?
  • If a model can never go back and change a word, how does it ever correct itself mid-answer?
  • What other sampling tricks exist besides temperature, and why would an app use them?
Follow these in Learnfold

Keep going in Learnfold

In the app you can ask about any sentence, follow a question into a new lesson, and pick up where you left off. This lesson opens right where you are.

Personalize has Learnfold’s tutor ask what you already know and what you want from it, then build a version of this course just for you. It uses AI, which needs a Learnfold subscription.

No app yet? Get Learnfold free

Get Learnfold

Learnfold is free on the App Store for iPhone, and this course is free to read in it.

  1. Get the app. Tapping the App Store button also copies this course’s link to your clipboard.
  2. Open Learnfold and tap Paste on the first screen to start with this course.
Download on the App StoreDownload on the App Store

Scan with your iPhone camera to open this course there. If Learnfold is installed it opens straight in the app.

Or send yourself the link:

Or see Learnfold on the App Store