Module 21 · How Chatbots Work

How does a chatbot write a whole reply?

Lesson 21.3 · Guess the Next Word

Rosa · Owen

One token at a time. It never sees the end of its own sentence coming.

Rosa types “Ashley posted a new” and the chatbot finishes with “video.”

Then “and it,” then “blew up.” It never planned the sentence. It guessed one token, appended it, and guessed again.

Owen tries to beat it at the game and loses to his own corpus.

The short answer

One token at a time, each chosen from a probability list given everything before it, hundreds of times in a row. It never planned the sentence, and it never checked the sentence against the world. That second part is the next lesson, and Felix’s fake book.

Try it yourself

Pick a starting word, then guess what comes next. The bars show what a tiny model trained on the crew’s sentences would predict.

See it in 3DHow a chatbot writes, one word at a time

What you just did has a name

Next-token prediction

A language model is a classifier with tens of thousands of buckets, one per token.

Given everything so far, it outputs a probability for each possible next token, picks one (usually a likely one, with a little randomness), adds it to the text, and does it again.

A paragraph is a few hundred rounds of that.

A few more things

The tiny version is a table

The app’s model just counts: after “Ashley,” how often does each word come next in the crew’s sentences? Real models do something far richer with attention and embeddings, but the shape of the answer is the same: a list of options with odds.

The randomness is a knob

Always pick the most likely token and the reply is dull and repetitive. Add randomness and it gets creative, and occasionally unhinged. Chatbot settings called “temperature” are exactly this knob.

It doesn’t know where it’s going

There’s no plan and no draft. Each token is chosen given the ones before it. That the result reads like planned writing is the astonishing part, and it’s also why a reply can start confidently and end somewhere false.

Go deeper

The longer version, if you want it.

Attention, in a paragraph

The tiny model only looks at the last word. Real models look at every token so far and, for each new token, learn which earlier ones matter most: “it” in “Ashley posted a new video and it blew up” should attend to “video,” not “Ashley.” That learned looking-back is called attention, and stacking many attention layers is the transformer architecture behind nearly every modern language model.

Attention is what lets the model keep a name from three paragraphs ago, follow a pronoun, or match a closing bracket. It is still next-token prediction. It’s just prediction with a very good memory of the context.

Quick check

You ask the same chatbot the same question in two separate chats and get two differently worded answers. What explains that?

Let’s take it slower

Here’s a rule of thumb: the model is playing the world’s biggest fill-in-the-blank, and the blank is always the next chunk. Everything you typed plus everything it has said so far is the setup. It scores every possible next token, from “the” to “Civic” to a comma, picks one of the high scorers, and then the blank moves one step to the right. It never looks ahead. The sentence you read is a trail of hundreds of small guesses, each made with no idea what the next one would be.

More examples

1

The one-word-at-a-time story

In the campfire game where each person adds one word to a story, nobody knows how it ends. Each word is picked to fit what came before, and the story goes somewhere no one planned. That’s a language model with people doing the predicting, and it goes off the rails the same way.

2

The code that finishes itself

Programmers use assistants that suggest the rest of a line as they type. Same loop: given the code so far, predict the next token, then the next. It’s right so often it’s spooky, and when it’s wrong it’s wrong with total fluency, which is why they still run the tests.

Back to the question

So how does it write a whole reply?

Show the answer

One token at a time, each chosen from a probability list given everything before it, hundreds of times in a row. It never planned the sentence, and it never checked the sentence against the world. That second part is the next lesson, and Felix’s fake book.

More on this: How does ChatGPT work?

One last question

What is a chatbot doing when it writes?

Talk Card

Talk Card · 21.3

A chatbot writes by predicting the next token, adding it, and predicting again, hundreds of times. It never planned the sentence and never checked it against the world.

Data to AI · pettigrewlab.com/learn

Next lesson