"A simple guide to how LLMs process prompts and generate text one token at a time."
Abhishek Kumar
Have you ever wondered how tools like ChatGPT or Gemini can generate human-like answers in seconds?
The answer is Large Language Models (LLMs). But how do they actually generate text?
When you send a message, the LLM doesn't directly understand it as humans do.
The text is first broken into smaller pieces called tokens.
For example:
"Hello, how are you?" ↓ ["Hello", ",", " how", " are", " you", "?"]
Tokens can be words, parts of words, or characters.
The tokens are converted into numerical representations called embeddings.
These numbers allow the model to process relationships and patterns in the text.
Text → Tokens → Numbers
LLMs use a neural network architecture called the Transformer.
One of its most important mechanisms is attention.
Attention helps the model determine which parts of the input are important when understanding the current context.
For example:
"The dog didn't eat the food because it was tired."
The model uses context to understand what "it" likely refers to.
This is the key idea behind text generation.
The model predicts what token is most likely to come next based on the previous tokens.
For example:
The sun rises in the ↓ "east"
After predicting east, the model adds it to the context and predicts the next token.
The → sun → rises → in → the → east → ...
This process continues until the model produces a complete response.
LLMs don't always choose the exact same next token.
Generation can use parameters such as temperature to control randomness.
This is why an LLM can sometimes give different answers to the same question.
Your Prompt ↓ Tokenization ↓ Embeddings ↓ Transformer + Attention ↓ Predict Next Token ↓ Repeat ↓ Generated Text
An LLM doesn't write an entire response at once.
It generates text one token at a time, repeatedly predicting what should come next based on the context it has received.
In simple terms:
LLMs generate text by using patterns learned during training to predict the next token, again and again, until the response is complete.