Find a course

Module 3: Generative AI and Language ModelsLesson 7 of 1013 min

How Large Language Models Work

Learn how chatbots predict text one token at a time, how they are trained and fine-tuned, and what context windows and temperature control.

Step 1 of 14Learn

Autocomplete, scaled up enormously

Type "Happy birthday to" on your phone and it suggests "you." That suggestion comes from a tiny model predicting the most likely next word. A large language model (LLM), the engine behind modern AI chatbots, is built on the same basic idea, scaled up in ways that change what it can do.

An LLM is a huge neural network, usually based on the transformer design, trained on a vast collection of text: books, websites, articles, code and more. Its training task is simple to state: given some text, predict the next piece. It reads a passage, guesses the next word, checks the real next word, and adjusts its billions of weights to make a better guess next time. Repeat this across trillions of words.

Something surprising happens at that scale. To predict text well, the model must absorb a great deal about how language works, about facts that commonly appear in text, about the structure of arguments, code and stories. Predicting the next word of a physics explanation is easier if you've captured patterns of physics. Predicting the next line of a program is easier if you've captured how code works.

When you chat with an LLM, it generates its reply one piece at a time. It predicts a likely next piece, adds it to the text, then predicts the next one based on everything so far, and so on until the answer is complete. There is no separate fact database it looks things up in, unless the product connects it to search or documents. The answer is produced from learned patterns, which explains both its fluency and its tendency to make things up.

Step 2 of 14Practice · True or false

When a basic language model answers a question, it looks up the answer in a stored database of verified facts.

Step 3 of 14Practice · Diagram

How a language model writes, one token at a time

Which step is affected by the temperature setting?

Tap the shape that answers the question, then press Check.

How a language model writes, one token at a timeCycle diagram: Read all the text so far → Score every possible next token → Pick one token → Add it to the text.Repeat until theanswer is doneRead all thetext so farScore everypossible nexttokenPick one tokenAdd it to thetext
Read all the text so far

Your prompt plus everything the model has already written.

Score every possible next token

The network outputs a probability for each token in its vocabulary.

Pick one token

Usually a likely one; the temperature setting controls how adventurous the pick is.

Add it to the text

The new token becomes part of the input for the next round.

Step 4 of 14Learn

Tokens and the context window

LLMs don't actually read words; they read tokens. A token is a chunk of text: often a whole common word like "the" or "house," sometimes a piece of a longer or rarer word. "Unbelievable" might be split into "un," "believ" and "able." Spaces and punctuation are part of tokens too.

As a rough rule of thumb for English, one token is about three-quarters of a word, so 100 tokens is about 75 words, and 1,000 words is roughly 1,300 to 1,400 tokens. Other languages and code often use more tokens per word.

Tokens matter for three practical reasons:

  1. Pricing. AI services that charge by usage usually charge per token, counting both the text you send and the text the model generates.
  2. Limits. Every model has a context window: the maximum number of tokens it can consider at once, including your instructions, any documents you paste, the conversation so far and its own reply. Older models handled a few thousand tokens; many current models handle hundreds of thousands. Text beyond the window is simply not seen.
  3. Odd failures. Because the model sees tokens rather than letters, tasks like counting the letters in a word or reversing a string can trip it up, even when it handles complex essays well.

The context window is also the model's only short-term memory. In a long chat, early messages may fall out of the window, or get less attention, and the model may "forget" instructions you gave at the start. Starting a fresh conversation with a clear summary often works better than pushing a very long one.

Step 5 of 14Practice · Work it out

Using the rule of thumb that one token is about three-quarters of an English word, roughly how many tokens is a 1,500-word report?

Step 6 of 14Try the calculator

Words, tokens and the context window

Estimate how much of a model's context window a document fills, using about 0.75 words per token.

Approximate tokens
8,000
Share of the window used
6.3 %
Words that would fit
96,000 words

Step 7 of 14Note

Step 8 of 14Learn

From text predictor to helpful assistant

A model trained only to predict internet text is not yet a good assistant. Ask it a question and it might continue with three more questions, as if it were completing a quiz page. Turning a raw model into a chatbot usually takes several stages:

  1. Pretraining. The model learns to predict the next token over an enormous text collection. This is by far the most expensive stage, needing large data centers for weeks or months. It produces broad knowledge and language skill.
  2. Instruction tuning (fine-tuning). The model is trained further on many example conversations written or curated by people: an instruction followed by a good response. It learns the format of being helpful.
  3. Learning from feedback. People compare pairs of model answers and pick the better one. Those preferences are used, often through a reinforcement learning method, to push the model toward answers people rate as helpful, honest and harmless. Some developers also use written principles and AI-generated feedback in this stage.

Two consequences follow.

First, the model's knowledge has a training cutoff. It knows little or nothing about events after its training data was collected, unless the product gives it search tools.

Second, the "personality" of a chatbot, such as its politeness, its willingness to say "I'm not sure" and the topics it declines, comes largely from the later stages, shaped by the choices of the company that built it. Different assistants built on similar technology can behave quite differently.

Step 9 of 14Practice · Put in order

Put the typical stages of building an AI chat assistant in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Refine it using human preferences between answers
  2. Fine-tune it on example instructions and good responses
  3. Release it and monitor how it behaves
  4. Pretrain the model to predict the next token
  5. Collect and clean a very large text dataset

Step 10 of 14Practice · Diagram

From raw text to assistant: less data, more care at each stage

Which stage uses by far the most data and computing power?

Tap the shape that answers the question, then press Check.

From raw text to assistant: less data, more care at each stageFunnel diagram: Pretraining on huge text, Fine-tuning on instructions, Human preference tuning, Testing and safety checks.Pretraining on huge textFine-tuning on instructionsHuman preference tuningTesting and safetychecks
Simplified; exact recipes differ between developers.
Pretraining on huge text

Trillions of tokens; the model learns language and facts by predicting the next token.

Fine-tuning on instructions

Tens of thousands of example requests with good answers.

Human preference tuning

People compare answers; the model is nudged toward the preferred ones.

Testing and safety checks

Red-teaming and evaluations before and after release.

Step 11 of 14Explore

Temperature: how random should the next token be?

Suppose the model gives its top candidate token a score d points higher than the runner-up. The x-axis is the temperature setting. The curve shows the chance the model picks the top token in a two-way choice. Low temperature makes the choice nearly certain; high temperature spreads the chances out. Change the score gap d to see the effect.

0.50.60.70.80.91 123
Chance of top token at temperature 1
0.818
Chance at temperature 0.3
0.993

Step 12 of 14Note

Step 13 of 14Practice · Fill in the blanks

Fill in the blanks

The maximum amount of text a model can consider at once is its context . Events after the model's training are unknown to it unless it is given a search tool.

Step 14 of 14Practice · Choose one

You paste a 300-page manual into a chatbot and ask about a detail on page 290. The bot answers as if that section does not exist. What is the most likely cause?