How Language Models Produce Text
Learn how a language model predicts tokens, what the context window and knowledge cutoff mean, and how these explain everyday quirks of AI tools.
Welcome back!
Step 1 of 13Learn
Autocomplete, scaled up enormously
Your phone's keyboard suggests the next word as you type. After "See you" it offers "tomorrow" or "soon" because those words often follow in the texts it has learned from. A large language model does the same basic job, predicting what comes next, but at a vastly larger scale and with far more context.
During training, a model reads a huge collection of text: books, websites, articles, code and more. Over and over, it sees a passage with the next piece hidden and adjusts billions of internal numbers, called parameters, to get better at guessing the hidden piece. Nobody programs in grammar rules or facts. The patterns of language, and a great deal of knowledge about the world, get absorbed because they help with prediction.
After this pretraining, developers fine-tune the model on examples of helpful conversations and use human feedback to reward answers people prefer. That is what turns a raw text predictor into an assistant that follows instructions, answers questions and declines some harmful requests.
When you send a prompt, the model writes its reply one small piece at a time. Each new piece is chosen based on your prompt plus everything it has written so far. There is no separate step where it looks up the truth. That one fact explains most of the surprises in this lesson.
Step 2 of 13Practice · Chart
What might follow "See you ..."?
If the model always took the most likely option, which word would it write next?
Tap the bar that answers the question, then press Check.
Step 3 of 13Learn
Tokens: how models count text
Models don't read letters or whole words. They read tokens, chunks of text that are often a whole common word, part of a longer word, a number or a punctuation mark. "Spreadsheet" might be split into "spread" and "sheet"; a rare name might become four or five tokens.
A handy rule of thumb for English is that one token is about three quarters of a word, or about four characters. So 1,000 tokens is roughly 750 words, and a 3,000-word report is about 4,000 tokens. Other languages, code and tables often use more tokens per word.
Tokens matter for three practical reasons:
- Limits. Every model can handle only so many tokens at once. Very long documents may be cut off or summarized internally.
- Cost and speed. Business plans and developer access are often priced per token, and longer prompts take longer to process.
- Odd blind spots. Because the model sees chunks rather than letters, tasks like counting the letters in a word or reversing a string can trip it up, even though it writes beautiful paragraphs.
You don't need to count tokens day to day, but knowing they exist helps you understand why a tool says your file is "too long" or why it miscounts characters in a headline.
Step 4 of 13Diagram
How a model writes, one token at a time
Tap a shape with to see more.
- Read all tokens so far
Your prompt, any files and the reply written so far.
- Score every possible next token
A probability for each of tens of thousands of tokens.
- Pick one
Usually a likely token, with some randomness.
- Add it to the text
Then the whole cycle runs again for the next token.
Step 5 of 13Try the calculator
Token estimator
Estimate how many tokens a document uses with the rule of thumb of about 0.75 words per token for English prose.
- Document tokens
- 8,000
- Reply tokens
- 667
- Total tokens used
- 8,667
Step 6 of 13Practice · Work it out
Using the rule of thumb of 0.75 words per token, about how many tokens does a 4,500-word report contain?
Step 7 of 13Learn
The context window: the model's working memory
The context window is the total number of tokens a model can consider at one time. It includes everything: hidden instructions from the tool, your messages, any files you upload, and the model's own replies so far. Context windows have grown fast, from a few thousand tokens in early chatbots to hundreds of thousands or more in many current tools, but they are always finite.
What happens when a conversation gets long? Depending on the tool, the oldest messages may be dropped, summarized or truncated. Even within the window, models tend to pay less attention to details buried in the middle of a very long input than to the beginning and end. In practice you may notice the assistant "forgetting" a rule you set an hour ago, or missing a clause on page 37 of a long contract.
Practical habits follow:
- Put the most important instructions near the start of your prompt, and restate key constraints in long conversations.
- Start a new conversation for a new task instead of continuing one endless thread.
- For long documents, ask about specific sections, or ask the model to quote the passage it relied on so you can check it.
Note that the context window is not long-term memory. Some tools offer separate saved "memory" or custom instructions, but by default a new chat starts fresh.
Step 8 of 13Practice · Chart
Where a long chat's context went
Tap the part using the most of the context window.
Tap the slice that answers the question, then press Check.
| Item | Tokens |
|---|---|
| Uploaded contract | 60,000 |
| Earlier messages | 30,000 |
| Tool instructions | 3,000 |
| Latest question | 500 |
| Reply so far | 2,000 |
Step 9 of 13Practice · Match the pairs
Match each term to its meaning.
Tap an answer, then tap the place where it belongs (or drag it there).
- Token
- Context window
- Training data
- Knowledge cutoff
- Parameters
Step 10 of 13Learn
Knowledge cutoff and why answers vary
Training takes months and stops at some point. The knowledge cutoff is the date after which the model has seen no data. Ask about a law passed, a price changed or a product launched after that date and a model without search will either say it doesn't know or, worse, produce a plausible guess. Some tools connect to web search or company documents to fill this gap. When they do, the answer is only as good as the sources retrieved, so check which sources were used.
You may also notice that asking the same question twice gives different answers. That is by design. At each step the model has a ranked list of likely next tokens, and it usually samples among the top candidates rather than always taking the single most likely one. A setting often called temperature controls how adventurous that sampling is. Lower values give more predictable, repetitive output; higher values give more varied and creative output, with a higher chance of odd choices.
Two consequences matter at work. First, variation is useful for brainstorming: ask for more options and you get them. Second, one answer to a factual question proves nothing. If you regenerate and the "fact" changes, that is a strong hint the model was guessing.
Step 11 of 13Note
Step 12 of 13Practice · Select all that apply
Which of these explain why an AI assistant might give a wrong answer about a recent event? Select all that apply.
Select all that apply.
Step 13 of 13Practice · Choose one
Halfway through a long chat, the assistant stops following your rule to "keep every answer under 100 words". What is the most likely explanation and fix?
Lesson complete!