Before we begin: The goal of this article is to help you understand how LLMs work in simple language. I’ve intentionally avoided deep technical details.
If you’ve used ChatGPT, Claude, Gemini, or another AI chatbot, you’ve probably wondered:
How does it understand my question and write an answer?
The answer is simpler than it sounds.
A Large Language Model (LLM) doesn’t write the entire response at once. It first understands your input, then generates the answer one small piece at a time.
Behind this process are a few important ideas:
Transformer
Attention
Self-Attention
Multi-Head Attention
Next Token Prediction
Let’s understand each one in simple language.
What Happens Inside an LLM?
Whenever you ask a question, the process looks something like this:
Your Question
│
▼
Transformer reads it
│
▼
Attention finds important words
│
▼
Model understands the context
│
▼
Predict the next token
│
▼
Repeat until the answer is complete
Enter fullscreen mode Exit fullscreen mode
This entire process usually takes only a few seconds.
Now let’s look at each step.
The Transformer – The Engine Behind an LLM
Think of the Transformer as the engine of a modern LLM.
Its job is to:
understand your input
understand how the words are connected
generate a meaningful response
The original Transformer architecture contains two main parts:
Transformer
┌──────┴──────┐
│ │
Encoder Decoder
(Understand) (Generate)
Enter fullscreen mode Exit fullscreen mode
Encoder – Understands the Input
Imagine someone asks:
Explain gravity in simple words.
Before answering, you first read the question and understand what the person is asking.
The encoder works in a similar way. It reads the input and understands the relationships between the words.
Encoder = Understands the input.
Decoder – Generates the Response
After understanding the input, the decoder starts writing the answer.
It doesn’t generate the whole response at once. Instead, it predicts one token at a time until the response is complete.
Decoder = Generates the response.
Attention – Focusing on What Matters
Not every word in a sentence is equally important.
Imagine you read:
Rahul gave Deepak his laptop because he was travelling.
Who is he?
To answer that, your brain naturally looks back at the earlier words.
The model does the same thing.
It checks which words are most related to he and gives those words more importance.
This process is called Attention.
he
│
┌──────┴──────┐
│ │
Rahul Deepak
Enter fullscreen mode Exit fullscreen mode
Attention helps the model focus on the most useful words instead of treating every word the same.
Attention = Focuses on important words.
Self-Attention – Words Learning From Each Other
Self-Attention is one of the biggest reasons Transformers work so well.
It simply means that every word can look at the other words in the same sentence.
For example:
The cat sat on the mat
│ ▲ │ ▲
└──────┼──────┴────────┘
Enter fullscreen mode Exit fullscreen mode
When understanding sat, the model also looks at cat and mat.
This helps it understand that the cat is the one sitting on the mat.
Self-Attention = Each word learns from the other words in the sentence.
Multi-Head Attention – Looking in Different Ways
Sometimes looking at something from one angle is not enough.
Imagine several people reading the same story.
One notices the grammar.
Another notices the meaning.
Another notices the relationship between the characters.
Together, they understand the story much better.
Multi-Head Attention works in a similar way.
Sentence
│
┌───────────┼───────────┐
│ │ │
Grammar Meaning Context
│ │ │
└───────────┼───────────┘
▼
Better understanding
Enter fullscreen mode Exit fullscreen mode
Each attention head focuses on something different, and together they build a better understanding of the sentence.
Multi-Head Attention = Looks at the sentence from different perspectives.
Next Token Prediction
Once the model understands your question, it starts generating the answer.
Suppose your prompt is:
The sky is…
The model predicts the next token.
The sky is
↓
blue
↓
today
↓
.
Enter fullscreen mode Exit fullscreen mode
Then it predicts the next one.
Then the next one.
This process continues until the answer is complete.
Every response from ChatGPT is created this way.
Next Token Prediction = Predict one token, add it, and repeat.
Putting Everything Together
Here’s the complete flow:
Your Prompt
│
▼
Transformer reads it
│
▼
Attention finds important words
│
▼
Model understands the meaning
│
▼
Predict one token
│
▼
Predict the next token
│
▼
Final Response
Enter fullscreen mode Exit fullscreen mode
Although this looks like many steps, it all happens in just a few seconds.
Key Takeaways
Transformer → The engine behind modern LLMs.
Encoder → Understands the input.
Decoder → Generates the response.
Attention → Focuses on the important words.
Self-Attention → Lets every word learn from other words in the sentence.
Multi-Head Attention → Looks at the sentence from different perspectives.
Next Token Prediction → Generates the response one token at a time.
These concepts work together to help an LLM understand your prompt and generate natural, human-like text.
Note
The original Transformer architecture includes both an Encoder and a Decoder. However, not all modern AI models use both. For example, GPT, ChatGPT, Llama, and Mistral are decoder-only models, while BERT is an encoder-only model. This article explains the original Transformer because it provides the easiest way to understand the ideas behind today’s LLMs.
This post was initially published on my blog. Check out the original source using the link below:
답글 남기기