Abhishek Kumar

How LLMs Generate Text

"A simple guide to how LLMs process prompts and generate text one token at a time."

Abhishek Kumar

How LLMs Generate Text

Table of contents

    Introduction

    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?


    1. Your Text Is Converted Into Tokens

    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.


    2. Tokens Become Numbers

    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
    

    3. The Model Understands the Context

    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.


    4. The Model Predicts the Next Token

    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.


    5. Why Does the Output Vary?

    LLMs don't always choose the exact same next token.

    Generation can use parameters such as temperature to control randomness.

    • Lower temperature → More predictable output
    • Higher temperature → More varied output

    This is why an LLM can sometimes give different answers to the same question.


    The Complete Process

    Your Prompt
        ↓
    Tokenization
        ↓
    Embeddings
        ↓
    Transformer + Attention
        ↓
    Predict Next Token
        ↓
    Repeat
        ↓
    Generated Text
    

    Final Takeaway

    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.