Artificial intelligence · Article

How AI chatbots like ChatGPT and Claude actually work, explained

Published:

The number of publications about Large Language Models by year grouped by publication types. The patent data for 2024 and 2025 are incomplete due to Patent Publication Delay. [ 1 ]
Image credit: ApoieRacional · CC BY-SA 4.0 · Wikimedia Commons

In brief

AI chatbots are built on large language models (LLMs), neural networks trained to predict the next "token" in a sequence of text. They turn words into numbers, use an attention mechanism to weigh how words relate to each other, and are then fine-tuned with human feedback to behave like helpful assistants rather than raw text predictors.

Hundreds of millions of people type questions into ChatGPT, Claude, Gemini or similar tools every day, but the software behind them is often described as a mysterious "black box." According to Google's Machine Learning Crash Course and an explainer by science writer Timothy B. Lee published on Understanding AI, the core mechanism is simpler to describe than it sounds: a large language model (LLM) is a neural network trained to predict the next small chunk of text, called a token, given everything that came before it.

From words to numbers: tokens and embeddings

Before an LLM can process text, it breaks a prompt into tokens — units that can be a whole word, part of a word, or a single character, according to Google's developer documentation. Each token is converted into a vector, a long list of numbers called an embedding, that is meant to capture something about the token's meaning. Understanding AI's explainer compares this to geographic coordinates: words with similar meanings end up positioned near each other in a vast numerical space, the way nearby cities share similar latitude and longitude.

Attention: how the model tracks context

The architecture behind virtually all modern LLMs is called the Transformer. Google's crash course explains that a Transformer relies on a mechanism called self-attention, which calculates how much every token in a piece of text should influence the interpretation of every other token. Understanding AI illustrates this with query and key vectors: each word effectively builds a checklist of the kind of other words it is looking for, and attention matches those checklists across the sentence so the model can, for example, work out which noun a pronoun like "it" refers to.

This step is followed by feed-forward layers that process each word's updated representation and push the model toward predicting what token should come next, Understanding AI notes. Because attention lets every token look directly at every other token regardless of distance, Transformers handle long, complex passages more consistently than the sequential models that came before them.

Training: billions of guesses, no human labels required

LLMs are trained mostly through unsupervised learning on masked or next-token prediction, meaning the system is not given hand-written "correct answers" for most of its training. Instead, Google's documentation describes a process in which tokens in real text are hidden and the model has to guess them; Understanding AI describes the same idea in terms of next-word prediction across huge collections of text scraped from books, websites and other sources. Parameters inside the network — Understanding AI cites GPT-3's 175 billion of them, trained on roughly 500 billion words — start out randomly set and are gradually adjusted, prediction by prediction, until the model's guesses line up with real text.

Turning a raw model into a helpful assistant

A model trained only to predict the next token is not automatically a chatbot that follows instructions politely. Hugging Face's technical explainer on Reinforcement Learning from Human Feedback (RLHF) describes the extra steps companies such as OpenAI use to convert a base model into an assistant. First, the pretrained model can be refined through supervised fine-tuning on example conversations. Second, human reviewers rank multiple model responses to the same prompt, and those rankings train a separate reward model that learns to score outputs the way a person would.

In the third stage, the original model is adjusted using reinforcement learning — commonly Proximal Policy Optimization — so that it produces responses the reward model scores highly, while a mathematical penalty keeps it from drifting so far from its original behavior that it starts generating nonsense purely to game the reward score, per Hugging Face's description of the method.

Why chatbots still make things up

A paper released by OpenAI researchers, "Why Language Models Hallucinate," available on arXiv, argues that fabricated answers are not simply a bug to be patched out. The paper attributes hallucinations to two linked causes: a statistical byproduct of how models learn to predict plausible-sounding continuations during pretraining, and the way models are evaluated afterward. Most benchmarks and leaderboards score an answer as either right or wrong and treat an admission of uncertainty — an "I don't know" — as a wrong answer, the paper states. That setup rewards confident guessing over honesty about uncertainty, which pushes models to keep producing fluent, specific-sounding statements even when they lack a reliable basis for them.

What the model is actually doing, in one sentence

Put together, the process described across these sources is this: a chatbot turns a prompt into tokens, converts them into numerical embeddings, uses self-attention layers to weigh how every token relates to every other, and repeatedly predicts the single most probable next token — a process shaped, after pretraining, by human feedback designed to make those predictions resemble helpful, instruction-following answers rather than raw internet text.

Key stages of building an AI chatbot
StageWhat happensSource basis
TokenizationText is split into word or subword units and converted to numeric IDsGoogle ML Crash Course
EmbeddingEach token becomes a vector of numbers encoding meaningUnderstanding AI
Self-attentionModel calculates how much each token should influence every other tokenGoogle ML Crash Course; Understanding AI
PretrainingModel predicts masked or next tokens across huge text corpora (e.g., ~500 billion words for GPT-3)Understanding AI
Supervised fine-tuningModel is shown example conversations to imitate instruction-following behaviorHugging Face RLHF explainer
Reward modelingHumans rank multiple model outputs; rankings train a reward-scoring modelHugging Face RLHF explainer
RL policy optimizationModel is adjusted via reinforcement learning (e.g., PPO) to maximize the reward scoreHugging Face RLHF explainer

Questions

What is a large language model in simple terms?
It is a neural network trained on huge amounts of text to predict the next token (word or word-piece) in a sequence, based on patterns learned from that training data, according to Google's Machine Learning Crash Course.
How does an AI chatbot know what to say next?
It converts the conversation into numerical tokens, uses a self-attention mechanism to weigh how each token relates to the others, and repeatedly outputs the statistically most probable next token until a full response is generated, per Google's and Understanding AI's explainers.
Why do chatbots sometimes make up false information (hallucinate)?
A 2025 OpenAI research paper on arXiv argues hallucinations stem partly from how models statistically predict plausible text during pretraining, and partly from evaluation benchmarks that score an honest 'I don't know' as a wrong answer, which rewards confident guessing instead.
What is RLHF and why does it matter for chatbots?
Reinforcement Learning from Human Feedback is a training stage, described by Hugging Face, in which human rankings of model answers train a reward model, which is then used to further adjust the AI so its responses better match what people consider helpful and appropriate.
How many parameters do large language models have?
Model size varies by system; Understanding AI cites GPT-3 as having 175 billion parameters trained on roughly 500 billion words, and notes that scaling up parameters, data and compute has historically driven much of the performance improvement in these models.

Sources

  1. Google for Developers — Machine Learning Crash Course: LLMs, what's a large language model?
  2. Understanding AI — Large language models, explained with a minimum of math and jargon
  3. Hugging Face — Illustrating Reinforcement Learning from Human Feedback (RLHF)
  4. arXiv — Why Language Models Hallucinate (OpenAI researchers)

This piece was prepared with artificial intelligence and passed an automated source check before publication. If you see an error, ask for a correction. Request a correction or reply · How we make this

Related