Lesson 2 of 16 · Level 1: Beginner
How AI Chatbots Work: Tokens, Context Windows and Hallucinations
- ⏱ 13 min read
- ❓ 4 review questions
- ✍️ Exercise with solution
In this lesson
- Understand how a language model predicts text.
- Learn what tokens and the context window are, and why they matter to you.
- Know why AI "hallucinates" and how to reduce it.
- Protect your privacy when you use AI assistants.
- Use specific prompts to rescue long chats, surface uncertainty and get multiple versions.
You don't need math to use ChatGPT, Claude or Gemini, but understanding how they work under the hood (the simple version) changes how you use them. It explains why they sometimes invent facts, why the same question gets different answers, why they "forget" what you said at the start of a long conversation and how to get more out of them.
The core idea: predicting the next word
A large language model (LLM) is, at heart, a system trained to predict what text comes next. If it reads "The capital of France is…", it has seen that phrase completed with "Paris" so many times that "Paris" is the most likely continuation.
The surprising part is that to predict the text of millions of books, articles, conversations and programs well, the model has to learn an enormous amount about grammar, facts about the world, reasoning, writing styles and the logic of code. Abilities that look a lot like understanding emerge from that prediction skill at massive scale.
How it's trained (in three stages)
- Stage 1 · the most expensivePretrainingThe model reads a vast amount of public and licensed text and adjusts billions of internal parameters to get better at predicting the next word.
- Stage 2Instruction tuningIt's trained on examples of questions and good answers so it behaves like a helpful assistant instead of just completing text.
- Stage 3Feedback tuningPeople (and other systems) rate responses, and the model learns to prefer the ones that are most helpful, honest and safe.
To see it in action, imagine the model has to continue the sentence "On Sunday mornings I like to have coffee with…". Internally it assigns a probability to every possible continuation and picks from among the highest:
These percentages are an example to illustrate the idea, not real values from a model. They add up to 100%: the model spreads probability across every possible continuation.
1. At its core, what does a language model do?
It generates text token by token based on what's most likely given everything before it. That's why it writes so fluently, and why it can be fluently wrong.
Tokens: the unit it "thinks" in
Models don't read whole words; they read tokens, which are chunks of text. A common word may be one token, while a long or unusual word gets split into several. For English, a widely used rule of thumb is that 100 tokens equal about 75 words. Other languages, numbers and code usually take more tokens per word.
Tokens matter for two practical reasons:
- Length limits (on what you can send and what you get back) are measured in tokens.
- If you use AI through an API (from a program), you pay per token. You'll see real prices in lesson 10.
The context window: its working memory
The context window is the maximum amount of text the model can take into account at once: your instructions, the documents you attach, the earlier conversation and its own answer. Today's models have very large windows. The Claude models current in November 2026, for example, accept up to 1 million tokens, the equivalent of several books. But windows aren't infinite, and the fact that a model can read a lot doesn't mean it pays equal attention to everything.
Practical consequences:
- In very long conversations, information from the beginning can lose weight or fall out entirely. When you switch topics, start a new chat.
- If something matters, repeat it or summarize it in your current message.
- The model doesn't "remember" your other chats unless the app has a memory feature turned on.
2. In a very long conversation, the AI seems to have forgotten instructions you gave at the start. What should you do?
The context window is its working memory. A fresh chat that starts with a good summary usually works better than shouting louder in a cluttered one.
Why AI "hallucinates"
A hallucination is when the model generates false information with complete confidence: a quote nobody said, a law that doesn't exist, a made-up statistic, a fake citation. It happens because the model generates the most plausible text, and an invented fact can sound very plausible.
Hallucinations are more likely when:
- You ask for very specific facts (numbers, dates, names, exact quotes, URLs).
- The topic is obscure or very recent.
- You pressure it to answer something it doesn't know ("give me 10 studies proving…").
- Give it the information yourself: paste the document or data and ask it to answer only from that.
- Turn on the assistant's web search and ask for sources with links. Then open them.
- Give it permission to say "I don't know": "If you're not sure, say so."
- Always verify numbers, names, regulations and quotes before using them in anything that matters.
3. What is an AI hallucination?
The model produces what sounds likely, not what it has checked. Give it sources and verify the facts that matter.
Why the same question gets different answers
When generating each word, the model doesn't always pick the single most likely option; it adds some variation (controlled by a setting called temperature). That makes answers more natural and creative, but it means asking the same question twice gives you different versions. Use that to your advantage: if an answer doesn't work for you, regenerate it or ask for "three different versions."
Models with reasoning and tools
Today's assistants are no longer just "text predictors." Many combine the model with tools that cover its weak spots:
Extended reasoning
The model "thinks" step by step before answering, which makes it much better at math, logic and complex problems.
The trade-off: it's slower. Use it for decisions and calculations, not for a quick greeting.Web search
It looks up current pages and cites its sources.
Fixes: outdated information. Always open the sources for anything important.Code execution
It writes and runs programs to do exact calculations or analyze files (lesson 7).
Fixes: the math mistakes it makes "from memory."Files and actions
It reads PDFs, spreadsheets, photos and screenshots; some can act in your email, calendar or documents (lesson 9).
Watch out: more access demands more care with permissions.When the right tool is turned on, many of the classic limitations (stale facts, math errors) shrink dramatically. That's why it pays to know which features your assistant has.
Three prompts that work with how the model works
Now that you know how it "thinks," you can ask in ways that play to its strengths. These three prompts solve the most common problems:
1. To rescue a long conversation
Summarize this conversation in a block I can paste into a new
chat: the goal, decisions made, key data (with exact figures),
the format we agreed on and pending tasks. 200 words max.
Expected result: a compact summary. Open a new chat, paste it as your first message and keep going: the model has the important parts "fresh," without the noise of all the back-and-forth.
2. To make it admit what it doesn't know
Answer the question, then label each fact in your answer as
(A) certain, (B) probable or (C) I should verify this. For any
C, tell me where I could check it. Question: [your question]
Expected result: the same answer with the shaky facts flagged. It isn't foolproof (the model can label something false as "certain"), but it tells you where to start checking.
3. To put the variation to work
Give me 3 very different versions of [an email subject line /
a tagline / the opening line of my presentation]: one formal,
one friendly and one with humor. Say which audience each works
best for.
Expected result: three genuinely different options instead of one. Because the model adds variation as it generates, asking for alternatives is faster than regenerating until something clicks.
And before you use any fact an assistant gives you in something that matters, run through this list:
Privacy: what happens to what you type
Whatever you type into an assistant is processed on the servers of the company that provides it. Depending on the product and plan, conversations may be kept for a while and, in some cases, used to improve the models (there's usually a setting to turn that off). Business and enterprise plans generally offer stronger guarantees. Check your tool's privacy settings and your company's policy, and read lesson 12 before pasting anything sensitive.
4. If 100 tokens equal about 75 words of English, roughly how many tokens does a 1,500-word report take up?
1,500 ÷ 0.75 = 2,000. Since many words take more than one token, a text always "weighs" more tokens than words. Multiplying by 0.75 (1,125) would get the direction backwards.
Practice exercise: test for hallucinations
- Ask an assistant with web search turned off: "Give me 3 published books about the history of coffee in the Pacific Northwest, with author and year."
- Search for each book online or in a library catalog. Do they all exist? Do the author and year match?
- Repeat the request, adding: "If you're not sure a book exists, tell me." Or turn on web search and ask for links.
See solution
It's common for at least one result to have wrong details or not exist at all, because a niche topic plus a request for exact titles, authors and years is the perfect recipe for a hallucination. With permission to admit doubt, or with web search and links, the answer is usually more honest and easier to verify. The lesson: for specific facts, demand sources and check them yourself.
Summary
- An LLM generates text by predicting the next token; its abilities emerge from that at scale.
- Tokens are chunks of text: they set length limits and API costs (about 75 English words per 100 tokens).
- The context window is its working memory: start new chats for new topics.
- Hallucinations are plausible false facts: give it sources and verify.
- Reasoning, web search and code execution greatly expand what it does well.
Next lesson: How to Write AI Prompts That Work: A Formula, Techniques and Templates. You'll learn the six-part prompt formula, seven techniques that improve any prompt and templates you can use today.
Create your account in 30 seconds, mark your lessons and download your certificate at the end.