How Do Large Language Models Work? Inside GPT and Gemini

Artificial Intelligence Published: 11 min read Pravesh Garcia
How Do Large Language Models Work Inside GPT and Gemini
Rate this post

You probably talked to one today. A quick email fix, a summary, an explanation of something you half-remember from school. Then you closed the tab without asking the obvious question: how do large language models work?

Most answers stop at “it predicts the next word.” True, and almost useless. That’s like explaining a car by saying the wheels turn.

The real answer is stranger and more satisfying. One idea from 2017 sits underneath all of it. GPT, Gemini, Claude, DeepSeek: same core mechanism, different budgets, different choices at the edges. That idea goes by the name attention. Once you can see it, the weird behavior stops being weird. The fluency, the invented citations, the strange uselessness of enormous context windows all fall out of the design.

One machine, doing one thing

So how do large language models work? Start with the dullest possible description.

The model takes a sequence of text and guesses what comes next. That’s it. Then it does the same thing again, with its own guess now part of the sequence, until it decides to stop.

There’s no database in there. The model doesn’t look up sentences it saw in training and paste them back at you. It also doesn’t run a separate “understanding” module that hands work to a separate “writing” module. One architecture, the transformer, performs one operation over and over.

What it does have is weights: billions of numbers that training adjusted, very slowly, over an enormous amount of text. Those numbers hold statistical structure about how language behaves. When people say a model “knows” something, they mean that knowledge survived compression into those numbers.

Keep that picture. Every odd behavior you’ve noticed comes from this single design, not from bolted-on quirks.

From words to numbers: tokens aren’t words

Computers don’t read letters. Modern models don’t read words either. They read tokens.

The GPT family uses byte-pair encoding, or BPE. The algorithm starts from raw bytes and repeatedly merges whichever pair shows up most often into a bigger chunk. Common fragments like “ing” end up as a single token. Rare words shatter into several pieces.

Why bother with this? A word-level vocabulary breaks the moment someone types a surname it has never seen. A character-level vocabulary handles anything but wastes enormous effort on spelling. Subwords split the difference. The model learns a grammar pattern once, then reuses it across every word sharing that fragment.

Vocabularies have grown alongside the models. OpenAI’s tokenizer library, tiktoken, used an encoding of roughly 100,000 tokens for the GPT-4 generation. The GPT-4o and GPT-5 generation moved to about 200,000. A bigger vocabulary packs more meaning into fewer tokens, which lowers cost and stretches how much text fits in a context window.

Each token then becomes a vector: a long list of numbers. Tokens with related meanings sit near each other in that space. This is the moment language stops being text and becomes geometry. Everything after this is arithmetic.

Why attention replaced everything before it

Before 2017, sequence models read in order. Recurrent networks took one token, updated an internal state, took the next, and carried on down the line. Two problems followed. They trained slowly, because step twelve couldn’t start until step eleven finished. And they forgot, because information from the start of a paragraph had to survive a long chain of updates.

Then a Google team published “Attention Is All You Need.” The claim sat right there in the abstract: the architecture “dispenses with recurrence and convolutions entirely” and relies on attention alone (Vaswani et al., 2017).

The accuracy held up. Their model reached 28.4 BLEU on English-to-German translation for WMT 2014. But the number that changed the industry was the training cost: three and a half days on eight GPUs.

Read that again. The breakthrough wasn’t accuracy. It was parallelism. Attention looks at every position at once, so a lab can point an entire data center at one model and have it finish. Recurrence made that impossible. We walked through the architecture-by-architecture contrast in Transformers vs RNNs vs CNNs.

Every scaling story of the past decade rests on that one property.

Self-attention, walked through one ambiguous sentence

Take the sentence: “the bank was closed.”

Bank is ambiguous. A river bank, or the place holding your money. You resolved it instantly, and you used the rest of the sentence to do it.

Self-attention does something comparable, with weights instead of intuition. For every token, the model scores how relevant each other token is. Those scores become weights. Each token’s representation then gets rebuilt as a weighted blend of its neighbors. “Bank” absorbs a share of “closed,” which tilts it toward the financial reading. Nothing reads strictly left to right. Everything looks at everything.

Now extend it: “the bank was closed after the flood.” The scores shift. “Flood” pulls hard, and “bank” slides toward the river meaning. Same mechanism, different weights, different answer.

Multi-head attention runs several of these scoring passes side by side. Each head can specialize. One might track grammatical role, another topical relevance, another something researchers haven’t found a clean name for. The original paper ran eight heads and beat the single-head version. Today’s models use far more.

That’s the spine. Stack that operation dozens of times, slot a small feedforward network between the layers, and you have a transformer.

How training actually happens

A freshly pretrained model isn’t a chatbot. It’s a text continuation engine with no manners. Ask it a question and it might reply with five more questions, because that’s what a page of questions usually looks like.

Turning that into an assistant takes four stages.

  1. Pretraining. The model predicts the next token across an enormous body of text, nudging its weights every time it misses. Fluency and most factual knowledge come from here.
  2. Supervised fine-tuning. People write demonstrations of good answers. The model imitates them, and starts responding to instructions rather than continuing them.
  3. A reward model. People rank several candidate answers from best to worst. A second model learns to predict those rankings, which turns fuzzy human preference into a number.
  4. Reinforcement learning. The main model optimizes against that learned reward, usually with PPO. A penalty term holds it close to the pretrained version.

That penalty matters more than it sounds. Strip it out and the model over-optimizes against the reward, drifting away from realistic language while chasing a higher score.

Stages two through four are where a model’s personality lives. Different labs make different calls here, and those calls explain far more about how GPT and Gemini differ than any architectural secret does. We compared the main alignment recipes in RLHF vs Constitutional AI vs RLAIF.

Bigger isn’t automatically better

There’s a stubborn belief that parameter count is the scoreboard. DeepMind punctured it in 2022.

Their Chinchilla work found that most large models of that era carried far more parameters than their training data justified. At a fixed compute budget, model size and token count should grow together, roughly in step. The blunt version, in their words: “For the number of training FLOPs used to train Gopher, a 4x smaller model trained on 4x more data would have been preferable” (DeepMind, 2022).

Then they demonstrated it. A 70-billion-parameter Chinchilla trained on 1.3 trillion tokens outperformed the 280-billion-parameter Gopher at equal compute. Four times smaller. Better results.

So when a vendor quotes a parameter count at you, that number describes their budget. It says nothing reliable about quality.

How do large language models work when you send a prompt?

Nothing clever happens the moment you hit enter. The loop is mechanical.

The system splits your prompt into tokens. Those tokens run forward through every attention layer. The model produces a probability across its whole vocabulary, maybe 200,000 candidates. A sampling step picks one. That token joins the end of the sequence, and the entire pass runs again.

Token by token, that’s the whole show.

Two consequences follow. The model carries no memory between conversations, so whatever context it has arrived in your prompt. And the sampling step explains why one question yields different answers on different days. The randomness is a choice, and you can turn it down.

This is also the honest answer to “what separates GPT from Gemini?” Underneath: much less than the marketing implies. Same next-token prediction, same attention layers. The gaps live in training data, scale, fine-tuning recipes, context length, and multimodal plumbing. Those gaps are real and they show up in output quality. They just aren’t different tricks. Our head-to-head comparison of the frontier models is a story about those choices, not about rival architectures.

Why hallucination is built in

You’ve seen it. The model invents a citation, a statute, a function that doesn’t exist, and delivers it in the same even tone it uses for true things.

The word “hallucination” makes this sound like a malfunction. It isn’t one.

A 2025 paper from OpenAI researchers, led by Adam Tauman Kalai, makes the structural argument. Their line: “language models hallucinate because the training and evaluation procedures reward guessing over acknowledging uncertainty.” Picture a student facing a question they can’t answer. A blank scores zero. A confident guess might score full marks. Standard benchmarks grade models the same way, so guessing pays.

The paper frames these errors as a kind of binary-classification failure inherited from the training objective itself. Which leads somewhere uncomfortable. If the incentive lives in how we score models, no amount of architecture work removes it. The repair has to reach the scoreboard.

I’d push the point further. “Hallucination” was a bad choice of word. The model isn’t perceiving anything falsely. It’s doing precisely what we optimized it to do, and we built a grading system that pays for confidence and never pays for doubt.

The honest limits

A bigger context window doesn’t mean better recall

Context length is the easiest number to advertise. A million tokens sounds like a solved memory problem. It isn’t.

Stanford researchers led by Nelson Liu tested this head on. Their finding: “performance is often highest when relevant information occurs at the beginning or end of the input context, and significantly degrades when models must access relevant information in the middle of long contexts.” They saw the same pattern in models designed specifically for long inputs.

The practical version: put the important thing first or last. Burying it on page forty of a pasted document is a gamble.

We can see inside, a little

Interpretability research has moved further than the black-box cliché suggests. Anthropic’s team used sparse-autoencoder dictionary learning to pull millions of human-readable features out of Claude 3 Sonnet, a model people were actually using in production (Anthropic, 2024). One feature responded to the Golden Gate Bridge across six different languages and in an image.

Then they turned that feature up. With it amplified, the model insisted it was the Golden Gate Bridge when asked about its physical form.

Real result. Limited result. It shows that internal concepts exist as findable, manipulable structures rather than statistical fog. It doesn’t mean anyone can trace a full answer back through the weights and explain why that answer, and not another one.

So does it understand anything?

Here’s where honest people disagree, and pretending otherwise would be a disservice.

One camp says no. The stochastic parrots argument, from Bender, Gebru, McMillan-Major and Mitchell in 2021, holds that these systems statistically mimic text without comprehension, stitching plausible sequences out of what they’ve seen. That paper triggered a real institutional fight. Google requested its retraction, and the dispute contributed to Timnit Gebru’s exit from the company.

The other camp points at results like Anthropic’s. A single feature that tracks one abstract concept across six languages and a photograph is hard to dismiss as surface pattern-matching.

Neither side has closed the argument, and both hold evidence. My own read: “understanding” carries too much weight as a word, and the useful question is narrower. Not whether the model understands, but which specific capabilities it genuinely has and which ones it only performs. We pulled at that thread in Do Large Language Models Understand Language, Really?

What to do with all this

None of this makes the technology less impressive. It makes it legible.

A model is a vast pile of numbers that turns your text into a probability, samples from it, and repeats. Fluency came from pretraining. The manners arrived later, from human feedback. That unshakeable confident tone came from a scoreboard that never once rewarded doubt. The limits you run into aren’t defects awaiting a patch. They’re the shape of the design.

Hold onto that the next time a launch event tells you everything has changed. Ask what actually changed: the data, the scale, the tuning, the context handling, the tools bolted around the outside. “How do large language models work?” will have roughly the same answer in two years as it does today, because the mechanism underneath has held since 2017.

The question I’d keep asking isn’t whether these systems think. It’s whether we’re grading them for the thing we actually want from them. Right now, we aren’t.

Frequently Asked Questions
What is a large language model in simple terms?
It is a very large set of numbers, called weights, that takes a piece of text and predicts what comes next. It repeats that guess one token at a time until it produces a full answer. There is no database of stored sentences inside it.
How do large language models actually generate text?
The model splits your prompt into tokens, runs them through its attention layers, and produces a probability score for every token in its vocabulary. A sampling step picks one, adds it to the sequence, and the loop runs again. That sampling step is why the same question can give you different answers.
What is the difference between a transformer and a large language model?
The transformer is the architecture. The large language model is a specific trained system built on that architecture. Transformers also power image and audio models, so every modern LLM is a transformer, but not every transformer is an LLM.
Why do LLMs sometimes make things up?
Because the way we train and score them rewards a confident guess over an admitted 'I don't know.' Standard benchmarks give zero for both a wrong answer and a blank one, so guessing wins on average. A 2025 paper by OpenAI researchers argues the fix belongs in how we evaluate models, not only in how we build them.
Do LLMs actually understand language, or just predict patterns?
Credible researchers disagree. The stochastic parrots argument says these systems mimic text statistically without comprehension. Interpretability work has found single internal features that track one abstract concept across several languages and even images, which is harder to explain as surface pattern-matching. Nobody has settled it.
What is the attention mechanism and why does it matter?
Attention lets every token in a sequence score how relevant every other token is, then rebuild itself as a weighted blend of them. It matters because it removed the need to read text in order, which made models far more parallel to train and much better at long-range connections.
What is a context window and why does it limit what an LLM can do?
The context window is how much text the model can hold in view at once, including your prompt and its own output. Stanford research found that models use information at the start and end of a long context far better than information in the middle, so a bigger window does not guarantee better recall.
Are larger models always better?
No. DeepMind's Chinchilla work showed that at a fixed compute budget, a 70-billion-parameter model trained on 1.3 trillion tokens beat the 280-billion-parameter Gopher. Parameter count tells you about a lab's budget, not about output quality.