How Do Large Language Models Work? I Traced a Single Prompt to Find Out

Published by

on

Young wizard handing a book to a glowing magical figure surrounded by floating books in a grand library

After writing about what an LLM is, I kept getting the same follow-up question from colleagues: “Okay, but what actually happens when I type something and hit enter?”

Fair question. Most explanations either wave their hands (“it predicts the next word!”) or dive straight into linear algebra. Neither satisfies a curious developer who wants to understand the machinery without getting a PhD first.

So I did what engineers do — I built a mini transformer visualizer that traces a single prompt through every stage of GPT-2’s pipeline and prints the actual numbers at each step. This post is the walkthrough I wish I’d had when I first started working with LLMs — with real model output to back it up.

Related: If you haven’t read the basics yet, start with I Explained LLMs to My Non-Technical Team. This post goes one level deeper.

Try it yourself: All the real model output in this post was generated by running a mini transformer visualizer script against GPT-2 (124M parameters, 12 layers, 12 attention heads, 768-dimensional embeddings). 


Why I Needed to Understand the Internals

I didn’t dig into this out of academic curiosity. I needed it because:

  • I was debugging prompt issues where small wording changes produced wildly different outputs — and I wanted to understand why
  • Our team was making architectural decisions about context windows, token limits, and caching — and I was guessing instead of reasoning
  • I was evaluating whether to use RAG, fine-tuning, or better prompting for a project — and the right answer depends on understanding what the model actually does with your input

Once I understood the pipeline, I stopped treating the model like a magic box and started treating it like an engineering system with predictable behaviors.


The Full Pipeline — One Prompt, Start to Finish

Let’s trace what happens when you type: The capital of France”

Here’s the full journey:

"The capital of France is"


┌─────────────────────┐
│ 1. Tokenization │ Text → token IDs
│ "The" → 464 │
│ " capital" → 3139│
│ " of" → 286 │
│ " France" → 4881│
│ " is" → 318 │
└────────┬────────────┘


┌─────────────────────┐
│ 2. Embedding │ Token IDs → vectors
│ Each token │ (numbers that capture
│ becomes a │ meaning)
│ high-dim vector │
└────────┬────────────┘


┌─────────────────────┐
│ 3. Transformer │ Vectors → context-aware
│ Layers │ representations
│ (Attention + │
│ Feed-Forward) │ "France" pays attention
│ × 12 layers* │ to "capital" and "The"
└────────┬────────────┘


┌─────────────────────┐
│ 4. Prediction │ → probability distribution
│ Head │ over all 50,257 tokens
│ │
│ " the": 8.5% │ ← GPT-2 (124M params)
│ " Paris": 3.2% │ Larger models are more
│ " now": 4.8% │ confident on "Paris"
└────────┬────────────┘


" the" (greedy)

* GPT-2 has 12 layers; GPT-4 scale models have 96+

That’s the entire pipeline. Let’s walk through each stage.


Step 1: Tokenization — Breaking Text Into Pieces

The model doesn’t see words. It sees tokens — numerical IDs that represent pieces of text.

“The capital of France” becomes something like: [464, 3139, 286, 4881, 318,]

Here’s the actual GPT-2 tokenization output:

Tokens aren’t always whole words:

  • “understanding” might split into “under” + “standing” (two tokens)
  • “AI” is one token
  • A space before a word is often part of the token — notice " capital" not "capital" in the output above

Why this matters in practice: Token limits (like “128K context window”) refer to these tokens, not words. A rough rule: 1 token ≈ 0.75 words. So a 128K token window is roughly 96,000 words — about a full novel.

This is also why you get charged per token in API pricing. Every token in your prompt and the response costs money.


Step 2: Embeddings — Numbers That Capture Meaning

Each token ID gets converted into a vector — a long list of numbers (typically 4,096 to 12,288 dimensions depending on the model).

These vectors aren’t random. They’re learned during training so that tokens with similar meanings end up close together in this high-dimensional space. “King” and “queen” are near each other. “Paris” and “France” are near each other. “The” and “quantum” are far apart.

Here’s what GPT-2 actually produces for “The” (showing just the first 10 of 768 dimensions):


Each of these 768 numbers encodes something about the token’s meaning. Individually they’re hard to interpret, but collectively they create a rich representation that the model can work with.

At this point, each token has a vector, but the vectors don’t know about each other yet.


Step 3: The Transformer — Where the Magic Happens

This is the core of every modern LLM. The transformer processes all the token embeddings and makes them context-aware.

Attention: “What Should I Pay Attention To?”

The key mechanism is self-attention. For each token, the model asks: “Which other tokens in this sequence are most relevant to understanding me?”

For our prompt “The capital of France”:

  • When processing “capital,” the model attends heavily to “France” and “of” — because “capital of France” is a meaningful phrase
  • When processing “is,” it attends to the entire phrase — because the answer depends on the whole question

This is what makes transformers powerful: they can capture relationships across the entire input, not just between adjacent words. “The cat that the dog chased ran away” — a transformer understands that “ran” refers to “cat,” not “dog,” even though “dog” is closer.

Multiple Layers: Depth of Understanding

A large LLM has many transformer layers — GPT-4 scale models have 96+. Each layer refines the representations:

  • Early layers capture syntax and local patterns (“capital of” is a prepositional phrase)
  • Middle layers build semantic understanding (“capital of France” means the city that is the seat of government)
  • Late layers prepare for the specific prediction task (“the answer to this factual question is a city name”)

Real Attention in Action

Here’s what actually happens inside GPT-2 when it processes “The capital of France is.” At the final layer (Layer 11), the last token “is” — the position where the model must predict the next word — distributes its attention like this:


The model is paying 66.5% of its attention to “The” — the start of the sentence — and the next-highest attention goes to “France” (9.91%). It has learned that to predict what comes after “The capital of France is ,” the most important context clues are the sentence structure (signaled by “The”) and the country name.

Across all 12 layers, the dominant attention pattern is “capital” → “The” (weight up to 0.98 in middle layers), showing how the model progressively builds an understanding that this is a factual statement about a capital city.

Multiple Attention Heads: Different Perspectives

Each layer has multiple attention heads — typically 64-128. Each head learns to focus on different types of relationships:

  • One head might focus on grammatical structure
  • Another on entity relationships (“France” → “capital”)
  • Another on positional patterns

The outputs from all heads are combined, giving the model a rich, multi-faceted understanding of each token in context.


Step 4: Prediction — Choosing the Next Token

After all transformer layers have processed the input, the final token’s representation (“is”) gets passed through a prediction head — a layer that converts the vector into a probability distribution over the entire vocabulary.

Here’s the real GPT-2 output — the top 10 predicted next tokens after “What is the capital of France?:


Wait — “Paris” is only #5 at 3.2%? This is GPT-2, a 124M parameter model from 2019. It’s hedging its bets across many plausible continuations (“the capital of the French Republic,” “now the largest city,” etc.). A larger model like GPT-4 (rumored to be 1.7T+ parameters with 96+ layers) would be far more confident, likely putting “Paris” at 90%+. This is a concrete demonstration of why model scale matters.

The model picks from this distribution. Usually it picks the highest probability token, but there’s a parameter called temperature that controls randomness:

  • Temperature 0 → always pick the highest probability (deterministic, predictable)
  • Temperature 0.7 → mostly pick high-probability tokens, with some variety (good for creative writing)
  • Temperature 1.0+ → more random, more surprising, more likely to produce nonsense

This is why the same prompt can give different answers when you run it twice — the model is sampling from a distribution, not looking up a fixed answer.


Step 5: Repeat — The Autoregressive Loop

Here’s the part that’s easy to miss: the model generates one token at a time, then feeds that token back in as input.

Here’s the real GPT-2 greedy generation, showing each step:

Final output:

“The capital of France is the capital of the French Republic, and the capital of the French Republic is the capital of the French Republic.”

Two things jump out. First, GPT-2 never says “Paris” — it goes for a verbose, circular definition instead. This is a small model being cautious. Second, notice the repetition loop: “the capital of the French Republic” repeats three times. This is a classic failure mode — once a pattern gets high probability, it reinforces itself in the next prediction step.

Each step runs the full pipeline — tokenize, embed, transform, predict. For a 500-token response, that’s 500 full passes through the model. This is why LLM responses take time to generate and why you see text streaming in word by word — each word is a separate computation.


What Training Actually Does

All of the above describes inference — what happens when you use the model. But how did the model learn these patterns in the first place?

Pre-training: Next-Token Prediction at Scale

The model sees trillions of tokens from books, websites, code, and conversations. For each sequence, it tries to predict the next token, gets told the right answer, and adjusts its parameters to do better next time.

That’s it. The entire capability of an LLM — writing code, answering questions, translating languages, reasoning about problems — emerges from doing next-token prediction at massive scale. The model was never explicitly taught to “answer questions” or “write code.” It learned those as patterns in the data.

Fine-tuning: Teaching Behavior

After pre-training, most models go through fine-tuning — training on curated conversations where humans demonstrate the desired behavior (helpful, honest, safe). This is why ChatGPT responds like an assistant rather than randomly completing text.

RLHF: Learning Preferences

Reinforcement Learning from Human Feedback (RLHF) is the final step. Humans rank different model outputs, and the model learns to prefer responses that humans rated higher. This is what makes modern LLMs feel polished and conversational.


What This Explains About LLM Behavior

Once you understand the pipeline, a lot of LLM quirks make sense:

BehaviorExplanation
HallucinationsThe model optimizes for plausible next tokens, not correct facts. A confident wrong answer is one where the wrong tokens had high probability.
Prompt sensitivityDifferent input tokens create different attention patterns, which cascade through 96 layers into different predictions. Small input changes → big output changes.
Context window limitsSelf-attention computes relationships between every pair of tokens. More tokens = quadratically more computation. That’s why context windows have limits.
Temperature effectsHigher temperature flattens the probability distribution — making unlikely tokens more likely to be chosen. Great for creativity, bad for factual accuracy.
Streaming responsesEach token is generated sequentially. The model literally doesn’t know its full answer when it starts — it’s composing it one piece at a time.
RepetitionIf “the” gets high probability once, the generated “the” becomes input for the next prediction, which can reinforce the same pattern — creating repetitive loops. (We saw this exact behavior in GPT-2’s output above.)

What I Changed After Understanding This

Knowing the pipeline changed how I work with LLMs in three concrete ways:

1. I prompt differently. I front-load the most important context, because attention patterns are strongest on tokens that appear near the query. Burying the key instruction at the end of a long prompt reduces its influence.

2. I debug differently. When a model gives a wrong answer, I don’t just rephrase and retry. I think about which tokens are creating misleading attention patterns and restructure the input to give the model better signal.

3. I evaluate solutions differently. When choosing between RAG, fine-tuning, or prompt engineering, I now reason about where in the pipeline each solution operates: RAG changes the input tokens, fine-tuning changes the model weights, prompt engineering changes the attention patterns. Different problems need different interventions.


Final Thoughts: It’s Pattern Prediction All the Way Down

The entire system — tokenization, embeddings, transformer layers, attention, next-token prediction — serves one purpose: predicting the most likely next token given everything that came before.

That’s a simple idea. But the depth of the transformer (96+ layers of attention and computation) and the scale of the training data (trillions of tokens) turn that simple idea into something that can write essays, debug code, and hold conversations.

The model doesn’t understand. It doesn’t think. It predicts — with extraordinary sophistication. And once you see the pipeline for what it is, you stop being surprised by its capabilities and its limitations. Both are natural consequences of the same mechanism.

Next, the practical question: how do you get the most out of this system?


Previously: I Explained LLMs to My Non-Technical Team. Next: Prompt Engineering Techniques: A Practical Guide.


Discover more from ByteMind AI : Build. Break. Understand.

Subscribe to get the latest posts sent to your email.

One response to “How Do Large Language Models Work? I Traced a Single Prompt to Find Out”

  1. […] read the high-level guides What is an LLM? A Simple Guide to Large Language Models and How Do Large Language Models Actually Work? ,  You know what a Large Language Model is and you have a basic grasp of how they work. You’re […]

Leave a Reply

Discover more from ByteMind AI : Build. Break. Understand.

Subscribe now to keep reading and get access to the full archive.

Continue reading