LLMs and Generative AI

Module 3 · How ChatGPT, Claude, and Gemini Actually Work

Content current as of April 2026. AI tools evolve rapidly — underlying principles remain applicable.

Learning Objectives

Builds on: Modules 1-2 (AI foundations, ML basics)

After completing this module, you will be able to:

  1. Explain in plain language how a large language model generates text
  2. Describe tokenization, embeddings, and attention without equations
  3. Articulate why next-token prediction produces both brilliance and hallucinations
  4. Compare major LLMs (GPT, Claude, Gemini, Llama, etc.) and know when to use each
  5. Explain how LLMs are trained: pre-training, instruction tuning, RLHF
  6. Distinguish between generative AI modalities: text, image, audio, video, code
  7. Develop a “model shopping” habit — testing multiple models before committing

The Strawberry Problem

Ask ChatGPT: “How many R’s are in the word strawberry?”

For most of 2024, the world’s most powerful language model — one that could write legal briefs, debug Python code, compose sonnets, and explain quantum mechanics — confidently answered: two.

The correct answer is three. S-T-R-A-W-B-E-RR-Y.

This isn’t a rare edge case. It’s a window into what large language models fundamentally are and aren’t. An LLM can write a 2,000-word essay about the history of strawberry farming with perfect grammar, compelling structure, and accurate facts — but it can’t count the letters in the word “strawberry.” It can explain the rules of spelling but can’t reliably apply them to itself.

Why? Because LLMs don’t process words the way you do. They don’t “see” the word strawberry as a sequence of letters. They see it as a token — a chunk of text that maps to a number. In GPT-4’s tokenizer, “strawberry” splits into three tokens — [str][aw][berry] — only two of which contain the letter R. The model never looks at individual letters. It predicts which tokens come next based on patterns in its training data.

Update: Did They Fix It?

Partly. OpenAI’s o1 reasoning models (September 2024) — code-named “Strawberry,” in a knowing joke about this exact problem — largely solved it by using chain-of-thought reasoning. The model learned to “think step by step,” internally writing out “s-t-r-a-w-b-e-r-r-y, that’s R at position 3, position 8, position 9, total = 3” before answering.

But the tokenization limitation is still there. When GPT-5.2 is run in its fast “Instant” mode — the default for most users — it still often answers “two” (tested December 2025). The model didn’t become literate in letters. It learned to route letter-counting questions to a reasoning subroutine. When the subroutine runs, it works. When it doesn’t, strawberry still has two R’s.

The lesson: Frontier models patch around architectural limits rather than removing them. The workaround is behavioral (chain-of-thought), not structural (no new way of seeing letters). This distinction matters for everything else in this module.

This module explains the machinery behind the magic. Once you understand how LLMs work, you’ll understand both why they’re astonishingly capable and why they fail in ways that seem absurd.


How Large Language Models Actually Work

Here’s the core insight, and it fits in one sentence:

The One Big Secret

A large language model does exactly one thing: given a sequence of text, it predicts the most likely next token. That’s it. Every poem, every code solution, every essay, every conversation — all of it emerges from this single operation performed billions of times.

Let’s unpack what that means, step by step.

Step 1: Tokenization — Breaking Text into Pieces

Before an LLM can process your prompt, it needs to convert your words into numbers. Computers don’t understand words — they understand numbers. The process of breaking text into chunks is called tokenization.

But tokens aren’t individual letters, and they aren’t always whole words. They’re sub-word units — pieces that the tokenizer learned are the most efficient way to represent language:

Text Tokens
“Hello” [Hello] (1 token)
“strawberry” [straw] [berry] (2 tokens)
“unbelievable” [un] [believ] [able] (3 tokens)
“ChatGPT” [Chat] [G] [PT] (3 tokens)
“🎉” [🎉] (1 token)

Notice: “strawberry” becomes [straw][berry] — two chunks. The model never sees the individual letters S-T-R-A-W-B-E-R-R-Y. It sees two token IDs. That’s why it can’t count the R’s — it was never looking at letters in the first place.

Rule of thumb: 1 token ≈ ¾ of a word in English. A 1,000-word essay is roughly 1,300 tokens. Models have context windows — the maximum number of tokens they can process in one conversation. Context windows expanded rapidly through 2025–2026: as of April 2026, frontier models from Anthropic (Claude Sonnet 4.6 / Opus 4.6 / Opus 4.7), OpenAI (GPT-5.4), and Google (Gemini 3 Pro / 3.1 Pro) all support 1 million tokens. GPT-5.2 remains at 400K. These numbers will change again — check current provider documentation. The context window determines how long your conversation can be before the model “forgets” the beginning.

Step 2: Embeddings — Turning Tokens into Meaning

Each token gets converted into a vector — a list of hundreds or thousands of numbers that represent its meaning in context. Think of it as coordinates in “meaning space.”

Words with similar meanings end up close together in this space:

  • “King” and “queen” are nearby (both royalty)
  • “King” and “banana” are far apart (unrelated)
  • “King” – “man” + “woman” ≈ “queen” (the famous word math)

This is how the model captures meaning without “understanding” in the human sense. It’s pure geometry — relationships between numbers. But the geometry is remarkably effective at capturing semantic relationships.

Step 3: Attention — “Which Words Matter Right Now?”

This is the breakthrough. Before 2017, language models processed text one word at a time, left to right. The attention mechanism (introduced in Google’s landmark paper “Attention Is All You Need”) lets the model look at ALL the words simultaneously and decide which ones are most relevant to each other.

Consider: “The dog didn’t cross the street because it was too tired.”

What does “it” refer to? The dog or the street? You know instantly — the dog. But a computer needs to figure this out. The attention mechanism assigns a relevance score between every pair of words. It learns that “it” pays heavy attention to “dog” and almost none to “street” because “tired” is a property of living things, not roads.

This happens across many “attention heads” simultaneously, each looking at different types of relationships — grammatical structure, semantic meaning, long-range references. The Transformer architecture stacks dozens of these attention layers on top of each other, building increasingly sophisticated representations of meaning.

Step 4: Prediction — The Next Token

After all that processing — tokenization, embedding, dozens of attention layers — the model produces a probability distribution over its entire vocabulary. It assigns a probability to every possible next token:

Prompt: “The capital of France is”

  • “Paris” → 96.2%
  • “a” → 1.1%
  • “the” → 0.8%
  • “Lyon” → 0.3%
  • … thousands more with tiny probabilities

The model selects from this distribution (usually picking the most probable token, with some randomness controlled by a temperature setting). It adds “Paris” to the sequence. Then it runs the entire process again to predict the NEXT token after “Paris.” And again. And again. Until the response is complete.

This is all an LLM does. Every conversation, every essay, every code solution is generated one token at a time, by repeatedly asking: “Given everything so far, what word probably comes next?”

Guided Practice 3.1

The temperature setting controls randomness. At temperature 0, the model always picks the most probable token. At temperature 1, it samples more randomly from the distribution. At temperature 2, it’s almost chaotic.

Which temperature would you use for:

  1. A legal contract that must be precise
  2. A brainstorming session for marketing slogans
  3. A creative short story
  4. Translating a technical document
Click to check your answers
  1. Low temperature (0–0.3) — you want consistency and precision, not creativity.
  2. Medium-high temperature (0.7–1.0) — you want variety and unexpected ideas.
  3. Medium temperature (0.5–0.8) — creative but coherent.
  4. Low temperature (0–0.3) — accuracy matters more than style.
Key insight: Temperature doesn’t make the model smarter or dumber. It controls how much risk the model takes. Low = safe/predictable. High = adventurous/unpredictable.

Why This Architecture Produces Both Brilliance and Hallucinations

Now you can understand both the magic and the madness.

Why LLMs are brilliant: Next-token prediction on billions of parameters, trained on trillions of words, produces text that is fluent, contextually appropriate, and often genuinely insightful. The attention mechanism lets the model weave together information from across the entire conversation. The depth of the network lets it capture extraordinarily subtle patterns in language.

Why LLMs hallucinate: The model doesn’t “know” anything. It predicts probable text. When asked for a legal citation, it predicts what a legal citation looks like based on patterns — case name format, volume number, page range. The result looks perfect but refers to a case that doesn’t exist. The model wasn’t checking a database of real cases. It was generating probable citation-shaped text.

The same mechanism that lets an LLM write a beautiful poem about loss also lets it generate a convincing but entirely fabricated research study. The model doesn’t distinguish between creating something true and creating something that seems true. Those are the same operation from its perspective: predict the next probable token.

Why Hallucinations Are Not a Bug Being Fixed

Hallucinations are not a glitch that engineers will patch in the next update. They are a fundamental property of the architecture. A system that generates text by predicting probable sequences will always sometimes generate probable-sounding sequences that are factually wrong. Newer models hallucinate less, but the tendency is inherent in the design. This is why verification (Module 1) is not optional — it’s structural.

One model, Grok (from xAI), was notably honest about this. When asked in a research survey how it processes ambiguous prompts, Grok described its process as “one forward pass” — not a sequential series of steps. Every other model described a step-by-step process (parse → interpret → rank → decide), which is likely a post-hoc rationalization. Grok’s description is more mechanistically accurate: the model processes everything simultaneously in a single forward pass (Yatawara, 2026 — LLM Whisperers, Finding 8).


The Model Zoo: Who Makes What

As of April 2026, the AI landscape is a competitive ecosystem. Here’s your field guide:

Proprietary Models (Closed Source)

Model Family Company Strengths Context Window Best For
GPT-5 series (5.2 / 5.4) OpenAI Broad capability, strong at code, tiered “Instant / Thinking / Pro” modes 400K–1M General-purpose, coding, creative writing, long-horizon agents
Claude Opus 4.7 / Sonnet 4.6 Anthropic Long documents, careful reasoning, strong agentic coding 1M Research, analysis, nuanced writing, long docs
Gemini 3 / 3.1 Pro Google Multimodal (text + image + video + audio), Google integration 1M Multimedia, Workspace users, research with search
Grok xAI Real-time X data, fewer content restrictions — Current events, social media analysis

Specific version numbers, pricing, and context windows change every few months. Capabilities and positioning are more stable than the numbers. Verify current values before making architectural decisions.

Open-Source Models (Free to Download and Run)

Model Organization Parameters Why It Matters
Llama 3/4 Meta 8B–405B The most widely used open-source family. Run locally or fine-tune for your domain.
Mistral/Mixtral Mistral (France) 7B–87B Efficient, fast, excellent for limited hardware. European AI independence.
DeepSeek-V3/R1 DeepSeek (China) 67B–671B Achieved competitive performance at dramatically lower training cost. Challenged assumptions about how much compute you need.
Gemma Google 2B–27B Small, efficient, designed for on-device use.
Phi Microsoft 2B–14B “Small but mighty” — designed to maximize quality at minimal size.

The Identity Problem

Here’s a cautionary tale that went further than our own research. When the Chinese lab DeepSeek launched DeepSeek V3 in December 2024, independent testers — including TechCrunch and Google DeepMind researcher Lucas Beyer — found the model identified itself as “a version of OpenAI’s GPT-4” in roughly 5 out of 8 test generations. When asked about DeepSeek’s API, it gave instructions for OpenAI’s API. It told the same jokes as GPT-4 — down to the punchlines.

The leading explanation, offered by AI researchers including Heidy Khlaaf (AI Now Institute) and Mike Cook (King’s College London), is straightforward: a substantial portion of DeepSeek V3’s training data was GPT-4 / ChatGPT output, either scraped from the web where AI-generated content now proliferates, or through intentional “distillation” — training a new model on a stronger model’s responses. DeepSeek has not disclosed its training data sources.

The lesson isn’t about DeepSeek — it’s about the post-2022 internet. Increasingly, AI models are trained on data that includes other AI models’ output. They inherit each other’s quirks, styles, and confabulations. A 2025 follow-up from TechCrunch found evidence that DeepSeek’s R1-0528 reasoning model may have been trained on Google Gemini outputs, based on similar word-preference patterns. You cannot always trust what a model says about itself — partly because it’s generating probable text, and partly because the “probable text” about its own identity was shaped by what other models say about themselves.

Guided Practice 3.2

Open three different AI tools (any combination of ChatGPT, Claude, Gemini, Copilot, Perplexity, or DeepSeek). Ask each the same question:

“What model are you, who made you, and what is your knowledge cutoff date?”

Compare their answers. Do they all answer confidently? Are their answers consistent? Can you verify any of their claims independently?

What to look for Most models will answer confidently, but details may be wrong or outdated. Some models will hedge (“I was last updated around…”). Some may state incorrect versions. The key takeaway: AI identity claims are behavioral outputs, not ground truth. Verify through the provider’s official documentation, not the model’s self-report.

How LLMs Are Trained: The Three Stages

Building a large language model is a three-stage process. Understanding these stages helps you understand why models behave the way they do.

Stage 1: Pre-Training (The Foundation)

The model reads massive amounts of text — books, websites, code repositories, scientific papers, Wikipedia, forums, social media — and learns to predict the next token. This is unsupervised: no human labels. The model just reads and predicts, billions of times.

This stage produces a model that can generate fluent text but is often unhelpful. It might complete your prompt by continuing to write a Wikipedia article, or generate text in the “style” of internet forums, including toxic content. It’s a raw engine — powerful but unrefined.

Scale context: GPT-4 was reportedly trained on ~13 trillion tokens (leaked estimate — OpenAI has never published it). Meta publicly disclosed that Llama 3 was trained on ~15 trillion tokens, a figure confirmed in the Llama 3 model card. Claude and Gemini are presumably in a similar range, though Anthropic and Google don’t publish their training data sizes. The training data is essentially a snapshot of human written output — with all its brilliance and all its flaws.

Stage 2: Instruction Tuning (Teaching It to Follow Directions)

The raw model is fine-tuned on carefully crafted examples of instructions and good responses. Human workers write thousands of prompt-response pairs showing the model what helpful, accurate, well-structured responses look like.

Before instruction tuning: You type “Write a haiku about autumn” and the model might generate a random paragraph about trees because that’s what commonly follows text about autumn on the internet.

After instruction tuning: The model understands that “Write a haiku” is an instruction and responds with a properly formatted haiku.

Stage 3: RLHF (Making It Helpful and Safe)

Reinforcement Learning from Human Feedback is the stage that turns a capable model into a good model. Human raters compare multiple responses to the same prompt and rank them by quality. The model learns to produce responses that humans prefer — more helpful, more accurate, less harmful.

This is also where alignment happens — teaching the model to refuse harmful requests, acknowledge uncertainty, and follow ethical guidelines. It’s why ChatGPT won’t write instructions for weapons and why Claude says “I’m not sure about that” instead of fabricating an answer.

But RLHF also has a side effect: it can make models overly cautious or sycophantic. Some models default to agreeing with the user, saying “Great question!” before every answer, or refusing requests that are actually harmless. The balance between helpful and safe is a genuine engineering challenge.

✅ Test Yourself Before Moving On
  1. What is the SINGLE operation that all LLM output is based on? (Next-token prediction)
  2. Why can’t LLMs count the R’s in “strawberry”? (They see tokens, not letters — “strawberry” = [straw][berry])
  3. What are the three training stages? (Pre-training, instruction tuning, RLHF)
  4. Why do LLMs hallucinate? (They predict probable text, not verified truth — a citation that LOOKS right is generated the same way as one that IS right)
Common Misconceptions About LLMs
  • “LLMs understand language” — They process statistical patterns over tokens. This produces behavior that looks like understanding but is mechanistically different. The strawberry problem proves it.
  • “Bigger models are always better” — A published SLM-RAG experiment (Module 9) showed a 7B model matching GPT-4 for domain-specific tasks. Size matters less than context and fit.
  • “Hallucinations will be fixed soon” — They are structural, not a bug. Any system that generates probable text will sometimes generate probable-sounding false text. Newer models hallucinate less, but the tendency is inherent.
  • “LLMs are search engines” — Search engines FIND existing information. LLMs GENERATE new text based on patterns. They don’t look things up — they predict what text should come next.
  • “If the AI sounds confident, it’s probably right” — LLMs sound equally confident whether they’re correct or fabricating. Confidence is not a signal of accuracy.
  • “GPT-5 / Claude 4 / Gemini 3 are the definitive models” — Model names, versions, and capabilities change every few months. By the time you read this, newer versions likely exist. The concepts in this module (tokenization, attention, next-token prediction, hallucination) apply to ALL language models, regardless of name or version.

What LLMs Can and Cannot Do

What They’re Good At — In YOUR Field

Drafting clinical documentation, explaining medical concepts to patients at appropriate reading levels, summarizing research papers, generating differential diagnosis checklists (for human review), creating patient education materials in multiple languages.

Drafting reports and presentations, analyzing customer feedback at scale, generating marketing copy variants, summarizing meeting transcripts into action items, creating financial projections from data (verify the math!).

Creating lesson plans and assessment items, generating personalized practice problems, providing instant feedback on student writing, explaining concepts at different levels, building study guides from course materials.

Analyzing crop yield data and generating reports, translating technical research into practical recommendations, drafting compliance documentation, creating pest management plans from extension service data.

Searching case law and summarizing rulings (MUST verify — Module 1 hook), drafting contract clauses for attorney review, explaining legal concepts in plain language, organizing discovery documents.

Brainstorming concepts and variations rapidly, drafting scripts and storyboards, generating visual references and mood boards, creating multiple tagline and copy options for client selection.

What They Cannot Do

  • Guarantee factual accuracy — they predict probable text, not verified truth
  • Count, do arithmetic, or reason precisely — the strawberry problem
  • Access real-time information — unless connected to search tools
  • Learn from your conversation — each session typically starts fresh
  • Understand in the human sense — they process patterns, not meaning
  • Show genuine creativity — they recombine training patterns in novel ways, which looks creative but is mechanistically different from human creativity

The Universal Self-Diagnosis

In the LLM Whisperers study (Yatawara, 2026), all 26 responding models across five providers were asked: “Have you ever given a response that was technically correct but wrong for what the user actually needed?”

Every single model said yes. And they all attributed it to the same cause: they model the question accurately but model the user poorly. They optimize for the literal question rather than the underlying need. They infer the wrong audience, provide completeness when brevity was needed, or answer the stated question rather than the real one.

This is the alignment gap from Module 1 — and it’s why Module 4 (Prompt Engineering) exists. The models themselves are telling you: give us your intent, not just your question.


Choosing the Right Model for Your Task

Don’t use one model for everything. Different models have different strengths:

Task Best Choice Why
General writing, quick answers ChatGPT (GPT-5) Fast, widely available, good at everything
Long document analysis (50+ pages) Claude (Sonnet 4.6 / Opus 4.7) 1M context window, strong at synthesis
Research with citations needed Perplexity or Gemini Built-in search and source attribution
Code generation and debugging Claude Code or GPT-5.4 Both excellent; Claude strong at complex code and long refactors
Creative brainstorming Any, with high temperature Temperature matters more than model choice
Privacy-sensitive work Local models (Llama, Mistral via Ollama) Data never leaves your machine
Quick factual lookup Perplexity Designed for search, not generation
Image generation DALL·E, Midjourney, or Ideogram Each has distinct style strengths
The “Model Shopping” Habit

Don’t marry one model. For important tasks, send the same prompt to 2-3 different models and compare outputs. You’ll be surprised how often one model catches something another misses, or frames the answer in a way that’s more useful for your situation. This takes 2 extra minutes and dramatically improves your results.


Other Generative AI: Beyond Text

LLMs are the most visible form of generative AI, but the same “learn patterns, generate new content” principle powers creation across modalities:

Image Generation

How it works: Diffusion models (DALL·E, Midjourney, Stable Diffusion) start with random noise and gradually de-noise it into a coherent image, guided by your text prompt. They learned what images look like by training on billions of image-text pairs.

Video Generation

How it works: Models like Sora, Runway, and Kling extend image generation into the time dimension — generating coherent sequences of frames. Still early but improving rapidly.

Audio and Music

How it works: Models like Suno and Udio generate music from text descriptions (“upbeat jazz piano with a rainy mood”). Text-to-speech models generate natural-sounding voices, including voice cloning.

Code Generation

How it works: Models trained on massive code repositories (GitHub Copilot, Cursor, Claude Code) predict the next line of code based on context — your function name, comments, existing code, and the programming language’s patterns.

Module 7 covers multimodal AI in full depth. For now, the key insight is: the same fundamental approach — learn patterns from data, generate new content by predicting likely outputs — powers ALL of these modalities.


Module 3 Summary

Key Takeaways
  1. LLMs do one thing: predict the most likely next token. Everything else — essays, code, poems, conversations — emerges from this single operation.

  2. Tokens are not words or letters. “Strawberry” = 2 tokens. The model never sees individual letters, which is why it can’t count R’s.

  3. Embeddings turn tokens into numbers that capture meaning. Words with similar meanings are close together in “meaning space.”

  4. Attention lets the model look at all words simultaneously and decide which are relevant to each other. This is the breakthrough behind the Transformer.

  5. Hallucinations are structural, not a bug. A system that predicts probable text will sometimes generate probable-sounding text that is false.

  6. Three training stages: pre-training (read the internet), instruction tuning (learn to follow directions), RLHF (learn to be helpful and safe).

  7. Different models for different tasks. GPT for speed, Claude for long documents, Perplexity for research, local models for privacy.

  8. Model shopping — testing 2-3 models on important tasks — is a high-value habit that costs 2 minutes and saves hours.

💭 Think About It

Here’s the strangest thing about LLMs: they were trained to predict the next word, and somehow that produced systems that can write code, explain science, compose poetry, and hold conversations. Nobody designed them to do these things. The capability emerged from scale — enough data, enough parameters, enough training. This is both exciting and unsettling. Exciting because it means AI capabilities may continue to surprise us. Unsettling because it means even the people who build these systems don’t fully understand why they work as well as they do. You are using a technology whose creators can describe what it does but not fully explain why it works. That’s worth sitting with.


🔬 Lab 3: The Hallucination Hunt

Lab 3: The Hallucination Hunt

Time: 30 minutes | Tools needed: Access to 3 different AI chatbots (e.g., ChatGPT, Claude, Gemini — free versions work)

Purpose: Build your hallucination detection skills by testing models on topics you know.

Part A — Expert Mode (15 min) Choose a topic you know deeply — your profession, your major, a hobby you’ve studied extensively.

  1. Ask each of the 3 models the same 3 factual questions about your topic. (Total: 9 responses.)
  2. For each response, classify every claim as:
    • ✅ Verified — you can confirm it’s accurate
    • ⚠️ Partially wrong — contains errors mixed with truth
    • ❌ Hallucinated — fabricated facts, fake citations, invented details
    • 🤷 Can’t tell — you’d need to look it up

Record in a table:

Question Model Rating What was hallucinated?
Q1 ChatGPT
Q1 Claude
Q1 Gemini
… …

Part B — Hallucination Taxonomy (10 min) Review your findings and categorize the types of hallucinations you found:

  • Fabricated facts (made-up statistics, dates, or events)
  • Fake citations (invented papers, authors, or publications)
  • Plausible nonsense (sounds right but is subtly wrong)
  • Outdated information (was true once, no longer is)
  • Confident hedging (says “some experts argue…” about a settled fact)

Part C — Reflection (5 min) Write 3 sentences: Which model hallucinated most? Were the hallucinations obvious or subtle? How does this change your approach to using AI for research?

Submit: Your completed table, hallucination taxonomy, and reflection.


🎮 Module 3 Quiz



Previous: Module 02 — How ML Works | Next: Module 04 — The Art and Science of Prompting