AI Mastery 2026: Gemini, Claude Code, Codex, Agents & AI Video/How large language models actually work: tokens, context windows, hallucinations1 / 60

How large language models actually work: tokens, context windows, hallucinations

How large language models actually work: tokens, context windows, hallucinations

Every AI tool you will use in this course, from the Gemini app to Claude Code to Codex, sits on top of a large language model (LLM). If you understand the handful of mechanics underneath, the odd behaviors stop being mysterious. You will know why a model confidently states a wrong date, why a long chat "forgets" what you said an hour ago, and why the same prompt gives a slightly different answer twice.

By the end of this lesson you will be able to explain tokens, context windows and hallucinations in plain English, and you will have run two small experiments that make each idea concrete. That understanding is the foundation for every prompting, agent and safety technique in the rest of the course.

🟢 Beginner takeaway: A language model is a very large autocomplete. It has read an enormous amount of text and learned which word fragments tend to follow which. When you type a question, it predicts a likely next fragment, adds it, and repeats until it has an answer. It does not look things up in a database and it does not "know" facts the way a person does. That is why it can sound sure and still be wrong.

What a model actually does: predict the next token

A model does not read letters or whole words. It reads tokens, which are chunks of text. In English a token is roughly three quarters of a word, so 1,000 tokens is about 750 words. Common words like "the" are one token. Rare words, code, and non-English text split into more pieces. A Hebrew or Japanese sentence usually costs more tokens than the same sentence in English.

Training is the process of showing the model trillions of tokens and adjusting billions of internal numbers (weights) so that its next-token guesses improve. After training, the weights are frozen. When you chat, the model runs a loop: look at everything in the conversation so far, produce a probability for every possible next token, pick one, append it, repeat. That is the whole trick.

Two consequences follow directly:

  • Sampling adds randomness. The model usually does not pick the single most likely token every time. A setting called temperature controls how adventurous the choice is. This is why you get different wording on a second run.
  • Everything is text in, text out. Instructions, examples, documents and your question are all just tokens in the same stream. The model has no separate "rules" channel. This matters later for prompt injection.

Modern chat models add a second stage after pre-training. They are tuned with human feedback so that the "most likely continuation" of a question is a helpful, safe answer rather than a random forum reply. Reasoning modes (ChatGPT thinking modes, Gemini Pro, Claude extended thinking) let the model generate many hidden tokens of working-out before the visible answer. That improves accuracy on hard problems but costs more tokens and time.

The context window: the model's working memory

The context window is the maximum number of tokens the model can look at in one go. It includes the system instructions, every message in the chat, any files you attached, and the answer being written. Current frontier models offer windows from a few hundred thousand tokens up to a million or more. Check the model's documentation for the exact figure, because it changes by tier.

Three practical rules come from this:

  1. The model has no memory between chats unless the product adds one. ChatGPT memory, Claude memory and Gemini's personalization are features layered on top. They inject saved notes into the context; the raw model still starts from zero.
  2. Long chats degrade. Even inside the window, models pay less reliable attention to material buried in the middle of a huge context. Important instructions should sit near the start or be repeated near the end.
  3. Once you overflow, something gets dropped. Products either refuse, silently truncate, or summarize older turns. Claude Code's /compact command exists precisely to summarize a long session so you can keep working.
ContentApproximate tokens
One typical email150 to 400
One page of a report500 to 700
A 30-page contract15,000 to 20,000
A 300-page book100,000 to 130,000
A medium code repositoryOften far beyond any window

These are estimates. Every provider has a free tokenizer page or API call you can use to count exactly.

Why models hallucinate

A hallucination is fluent, confident output that is false: a fake citation, a product feature that does not exist, a legal rule from the wrong country. It is not a bug that will be patched away. It is the natural result of a system whose job is to produce plausible text. When the true answer is not well represented in training data, the most plausible-sounding continuation is still produced.

Hallucinations are most likely when you ask about:

  • Specific numbers, dates, prices, statistics and version numbers.
  • Citations, URLs, case names, ISBNs and quotes.
  • Recent events after the model's training cutoff.
  • Niche topics, small companies, private individuals.
  • Anything where you have pushed the model into agreeing with a premise that is wrong.

The fixes are practical, not magical. Give the model the source material so it can quote rather than recall (this is what NotebookLM, Claude Projects and ChatGPT file uploads do). Turn on web search or Deep Research for current facts. Ask the model to say "I don't know" when unsure. And verify anything that goes to a customer, a court or a bank.

A useful mental model for choosing settings

You rarely set temperature in a chat app, but the idea still helps. For brainstorming, marketing copy and names, you want variety: run the prompt several times and pick. For extraction, classification, code and anything factual, you want consistency: use a reasoning mode, give sources, and ask for a fixed output format. In the API (covered in Module 10) you set temperature directly; for factual tasks keep it low.

🟡 Intermediate: Treat the context window as a budget you manage on purpose. Before pasting a 40-page document, ask yourself what the model actually needs. Often the answer is a table of contents plus the two relevant sections. When you must work with a large corpus, use a retrieval tool (NotebookLM, a Claude Project, or a RAG pipeline) that pulls only the relevant chunks into context per question. Also learn to reset: in Claude Code, /clear starts a fresh context and /compact summarizes; in ChatGPT and Gemini, start a new chat and paste a short "state of play" summary. A clean 5,000-token context usually beats a cluttered 150,000-token one.
🔴 Advanced / Pro tip: Hallucination rate is not fixed per model; it depends heavily on how you ask. Two techniques measurably help. First, separate retrieval from reasoning: ask the model to first list the exact sentences from the provided source that support each claim, then answer only from those sentences. Second, ask for calibrated uncertainty: require a confidence label (high / medium / low) on each factual statement and a reason. Low-confidence items become your verification checklist. In API work, combine these with structured output (JSON schema) so a downstream script can route low-confidence claims to a human. And remember that reasoning modes trade tokens for accuracy: measure whether the improvement on your actual task justifies the cost and latency before making it the default.

Try it now (10 minutes)

  1. Open the Gemini app, ChatGPT or Claude. Paste this prompt and note how the model handles a detail it cannot know:
    I am going to describe a fictional company. Answer only from what I give you. If something is not stated, reply "not stated".
    
    Company: Northwind Bakery, founded 2019 in Leeds, 4 staff, sells sourdough and cinnamon rolls.
    
    Questions:
    1. What year was it founded?
    2. What is its annual revenue?
    3. Who is the founder?
    4. What products does it sell?
    A well-behaved model answers 2 and 3 with "not stated". If it invents a number or a name, you have just watched a hallucination.
  2. Now test the context window. Start a new chat and paste this:
    Remember this code word: PELICAN-42. Later I will ask for it. For now, summarise the history of the bicycle in 300 words.
    Then paste four or five long articles (anything you have handy) and ask for a summary of each. Finally ask: "What was the code word?" Most modern models will recall it, but note how long it took and whether the answer became less precise. Repeat with a chat that is already very long and compare.
  3. Find your provider's token counter (search the docs for "tokenizer" or "count tokens"). Paste an email and a page of your own writing. Write down the ratio of words to tokens you observe.

How this helps your business

  • Fewer embarrassing errors. Knowing that numbers, citations and prices are the highest-risk outputs lets you build a two-minute verification habit before anything reaches a client.
  • Lower AI bills. API pricing is per token. Trimming context, using a smaller model for simple tasks, and reserving reasoning modes for hard questions can cut costs sharply.
  • Better tool choices. When a task involves a large document set, you will reach for NotebookLM or a Project instead of pasting into a chat and hoping.
  • Clearer staff guidance. You can explain to a team in one sentence why "the AI said so" is not a source, which reduces risk without banning the tools.

Common mistakes

  • Treating the model as a search engine and accepting statistics, quotes or URLs without checking them.
  • Keeping one enormous chat open for weeks and wondering why quality drops.
  • Assuming the model remembers your previous conversation when the product's memory feature is off.
  • Asking about events after the training cutoff without enabling web search.
  • Judging a model on one run. Sampling means you should try the prompt two or three times before concluding it "can't do it".
Key takeaway: An LLM predicts plausible next tokens inside a limited context window, so it can sound certain while being wrong; give it sources, manage its context, and verify high-risk facts.

Key Terms — Flip to Reveal

📘

Large language model (LLM)

A neural network trained on huge amounts of text to predict the next token. Chat assistants like ChatGPT, Gemini and Claude are built on top of one.

📘

Token

The chunk of text a model reads and writes, roughly three quarters of an English word. Pricing and context limits are measured in tokens.

📘

Context window

The maximum number of tokens a model can consider at once, including instructions, chat history, attached files and its own answer.

📘

Hallucination

Fluent, confident output that is false, such as an invented citation or price. A natural side effect of predicting plausible text.

📘

Temperature

A setting that controls how random the model's token choices are. Low values give consistent answers; higher values give more varied ones.

📘

Training cutoff

The date after which the model has seen no data. Questions about later events need web search or supplied documents.

📘

Reasoning mode

A mode where the model generates hidden working-out tokens before answering. Improves accuracy on hard problems at higher cost and latency.

📘

Grounding

Giving the model source material so it answers from that text rather than from memory, which sharply reduces hallucinations.

Quick Check

What does a large language model fundamentally do when it generates an answer?

Select your answer