Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our previous lesson, we demystified the training process of a supervised model, exploring the iterative loop of prediction, loss calculation, and optimization. You learned how a model like a neural network adjusts its internal parameters to minimize errors, effectively "learning" from labeled data. This fundamental process is the engine that powers a vast range of AI applications.
Today, we will focus on a specific, highly impactful application of this technology: Large Language Models (LLMs). Your goal is to understand the precise mechanism LLMs use to generate the coherent, human-like text you see in tools like ChatGPT. Rather than producing entire sentences at once, you'll learn that LLMs operate through a surprisingly simple yet powerful loop of token prediction. This knowledge is essential for your ambition to integrate LLM APIs into your own applications, as it will give you direct control over model behavior.
The Core Principle: Autoregressive Generation
At first glance, an LLM's ability to write code, draft an email, or explain a complex topic can seem like magic. The core mechanism, however, is much more systematic and can be understood from a developer's perspective. The fundamental principle is that LLMs generate text one "token" at a time.
A token is the basic unit of text for an LLM. It isn't always a full word; it can be a part of a word (like inter and net), a punctuation mark, or a common word like the. The model's entire vocabulary is a massive collection of these tokens.
The process of generating text is autoregressive, which is a formal way of saying the model's output at one step becomes part of its input for the next step. It's a recursive loop.
- You provide an initial prompt (e.g.,
"The first principle of system design is"). - The model processes this sequence and predicts the single most likely next token (e.g.,
"to"). - The model then appends this new token to the sequence, creating a new input:
"The first principle of system design is to". - It repeats the process, predicting the next token based on this updated sequence.
This loop continues until the model generates a special <stop> token or reaches a predefined length limit.

The following short article provides a concise, developer-focused overview of this core idea.
How LLM Token Prediction Works (A Simple Guide for Developers)
This article breaks down the fundamental concept of token-by-token generation in a very accessible way.
Please read from the beginning of the article to the end of the section "And Then It Repeats…". Focus on the central idea: LLMs predict the next token based on all previous tokens, and this simple loop is what produces complex text.
The Text Generation Pipeline
Now that you understand the high-level concept, let's break down what happens inside that loop. The process can be modeled as a four-stage pipeline, which is executed for every single token that is generated.
A fantastic article, "How LLMs Generate Text for the Rest of Us," is written specifically for software engineers and provides a clear breakdown. We will use it as our guide.
How LLMs Generate Text for the Rest of Us
This article details the entire text generation pipeline, from tokenization to the final selection of a token.
Please read from the section "The Text Generation Pipeline". As you read, focus on understanding the distinct roles of the four stages: Tokenization, Inference, Decoding, and Detokenization. Pay close attention to the insight that the core model (inference) only scores tokens, while the randomness is introduced later in the decoding stage.
Let's explore these stages in more detail, incorporating insights from a video that visualizes this process perfectly.
How LLMs Actually Generate Text (Every Dev Should Know This)
This video provides a superb step-by-step visual explanation of the entire generation process.
Please watch this video from the beginning to the end of the main explanation. It walks through the five steps (which map closely to our four stages) and provides excellent analogies and visuals for each one.
Stage 1: Tokenization
As you've seen, the first step is to convert the raw input string into a sequence of token IDs that the model can understand. The video "Most devs don't understand how LLM tokens work" explains the practical implications of this for a developer.
Most devs don't understand how LLM tokens work
This video dives into the "why" and "how" of tokenization, explaining why it matters for API usage and cost.
Watch the first part of the video from the beginning. It explains that tokens are the "currency" of LLMs and demonstrates how text is encoded into numbers. Notice how different models can have different tokenizers, leading to different token counts for the same text.
This is a key practical detail. When you use an LLM API, you are often billed per token, and models have maximum context windows measured in tokens, not characters or words. Understanding that const might be one token while a less common function name might be three helps you manage both cost and context limits effectively.
Stage 2: Inference and Probabilities
Once the input is a sequence of token IDs, it's fed into the core of the LLM—typically a Transformer-based neural network. The model performs a forward pass (as we discussed in the previous lesson) and produces a raw score, called a logit, for every single token in its vocabulary. For a model with a 128,000-token vocabulary, this means the output of this stage is a vector with 128,000 numbers.
A higher logit means the model "believes" that token is a more likely continuation of the sequence. However, these are just raw scores, not probabilities. To convert them into a meaningful probability distribution (where all values are between 0 and 1, and sum to 1), a softmax function is applied. You encountered this function in our last lesson when we discussed cross-entropy loss for classification. Here, it's used to turn the logits into the probability of each token being the correct next one.

Stage 3: Decoding (Token Selection)
This is the stage where you, as a developer, have the most control. The model has given us a probability for every possible next token. Now, we must choose one.
The simplest method is greedy search: always pick the token with the highest probability. This is deterministic and predictable but often leads to repetitive and dull text. To generate more creative and human-like output, we use sampling. Instead of always picking the best token, we sample from the probability distribution. This is where parameters you see in API documentation come into play.
Let's return to the "How LLMs Generate Text for the Rest of Us" article to understand these crucial parameters.
How LLMs Generate Text for the Rest of Us
This part of the article explains the key decoding parameters that you will use when interacting with LLM APIs.
Read the sections "Decoding strategies" through "Top-p Sampling". Focus on understanding: The Softmax Function: How it converts logits into a usable probability distribution. temperature: How this parameter acts as a "creativity dial" by sharpening (low temp) or flattening (high temp) the probabilities. top-k Sampling: How it limits the choice to a fixed number of the most likely tokens. top-p (Nucleus) Sampling: How it adaptively creates a candidate set based on a cumulative probability threshold.
As a rule of thumb for your future work:
- For factual, predictable tasks like code generation or data extraction, use a low temperature (e.g., 0.1-0.3).
- For creative tasks like brainstorming or writing marketing copy, use a higher temperature (e.g., 0.7-1.0).
Stage 4: Detokenization and The Loop
Once a token ID is selected through sampling, it is simply converted back into its text representation (e.g., the ID 930 is converted to the string " to") and appended to the running sequence. The new, longer sequence becomes the input for the next iteration of the pipeline. This cycle repeats, generating one token at a time, until a stop token is produced.
The "intelligent" and complex behaviors you see—like reasoning, coding, and maintaining a conversation—are emergent properties of this simple, recursive loop executed at a massive scale. The model isn't "planning" a sentence; it's just getting exceptionally good at the statistical task of predicting the next most plausible token.
Conclusion
In this lesson, we've pulled back the curtain on how Large Language Models generate text. You've moved from seeing them as a black box to understanding the underlying mechanics, which are deeply rooted in the principles of supervised learning we've already covered.
Here are the key takeaways:
- Autoregressive Generation: LLMs build text sequentially by predicting one token at a time, with each new token being added to the input for the next prediction step.
- The Generation Pipeline: This process involves four key stages: Tokenization (text to IDs), Inference (calculating logits), Decoding (selecting a token via sampling), and Detokenization (ID to text).
- Probabilistic Nature: The core of the model is deterministic, producing the same logits for the same input. The creativity and randomness come from the decoding stage, where a token is sampled from a probability distribution.
- Developer Control: You can directly influence the generation process through API parameters like
temperature,top-k, andtop-p, which control the sampling strategy.
You now have the foundational knowledge to not only use LLMs but to reason about their behavior. In our next lesson, we will build on this by examining the practical capabilities and, crucially, the inherent limitations of current AI models, including issues like hallucinations, bias, and finite context windows.