Recommended resources
The freely available resources this lesson will direct you to.
Introduction
Welcome to the start of your journey into the world of Large Language Models (LLMs) and Agentic AI. This first module lays the essential theoretical groundwork, providing the conceptual tools you'll need to understand how models like GPT-4 operate. With your background in computer science and extensive industry experience, we'll be able to move quickly through these fundamentals, focusing on the architectural principles and design choices that have defined the current state of AI.
Today's lesson addresses a pivotal moment in the history of Natural Language Processing (NLP): the shift from sequential models to the Transformer architecture. Our goal is to contrast the Transformer with its predecessors, like Recurrent Neural Networks (RNNs), and explain precisely why this shift was so revolutionary for processing sequences of text. Understanding this is key to appreciating the power and scalability of modern LLMs.
The Challenge of Sequential Data
Before we can build software that understands language, we first need a way for a computer to process it. Language is inherently sequential—the order of words defines its meaning. Consider "The dog chased the cat" versus "The cat chased the dog." The words are the same, but the sequence completely changes the meaning.
For years, the go-to architecture for handling such sequential data was the Recurrent Neural Network (RNN).
An RNN processes a sentence one word (or token) at a time. At each step, it takes in a new word and combines it with a hidden state—a vector that represents a compressed summary of all the words it has seen so far. This hidden state acts as the network's "memory," which is then passed to the next step.
To get a feel for this sequential process, watch the first couple of minutes of the following video.
Transformers, explained: Understand the model behind GPT, BERT, and T5
Watch this segment from Google Cloud Tech's video on Transformers. It provides a clear, high-level overview of how RNNs work and their primary limitations.
Focus on the explanation from the section on RNNs, which visualizes the sequential nature of the processing and introduces the problems of handling long sequences and the difficulty of parallelization.
As the video points out, this sequential design has two major flaws:
- The Long-Range Dependency Problem: The hidden state is a bottleneck. As the RNN processes a long sentence, information from the beginning has to be carried through many successive transformations. This often leads to the vanishing gradient problem, where the influence of earlier words gets diluted, effectively making the model "forget" the initial context.
- Lack of Parallelization: Since each step depends on the output of the previous one, you cannot process the words in a sentence simultaneously. This makes training RNNs on massive datasets incredibly slow and inefficient.
An Incremental Fix: LSTMs and GRUs
To combat the vanishing gradient problem, more sophisticated recurrent models were developed, most notably Long Short-Term Memory (LSTM) networks and Gated Recurrent Units (GRUs). These architectures introduced internal mechanisms called "gates" that allow the network to more intelligently control its memory.
- A forget gate can decide to discard irrelevant information from the past.
- An input gate can decide which new information is worth storing.
- An output gate controls what information from the memory is used for the current step's output.
These gates made LSTMs and GRUs much better at capturing longer-range dependencies than simple RNNs. However, they did not solve the fundamental problem: they are still recurrent and therefore process data sequentially. The inability to parallelize remained a major bottleneck to scaling.
The following reading from aman.ai provides an excellent breakdown of these architectures and their trade-offs.
Deep Learning Architectures Comparative Analysis - aman.ai
This article offers a structured comparison of different neural network architectures. We'll focus on its clear explanation of RNNs, LSTMs, and GRUs.
Please read the sections on Recurrent Neural Networks and the section covering GRUs and LSTMs. Pay close attention to the explanation of the vanishing gradient problem and how the gate mechanisms in LSTMs and GRUs are designed to mitigate it.
The Paradigm Shift: The Transformer
In 2017, researchers from Google published a paper titled "Attention Is All You Need," which introduced the Transformer architecture and completely changed the landscape of NLP. Its core innovation was to discard recurrence entirely and rely solely on a mechanism called attention.

The Transformer processes all words in an input sequence simultaneously. This immediately solves the parallelization problem, allowing researchers to train vastly larger models on web-scale datasets—a key reason for the dramatic leap in capabilities we see in models like GPT.
But if the model processes all words at once, how does it handle long-range dependencies and understand the relationships between words? This is where self-attention comes in.
Self-Attention: Direct Communication Between Words
Self-attention allows every word in a sequence to directly look at and weigh the importance of every other word in that same sequence.
Think of it this way: In an RNN, for the last word to relate to the first word, its signal must pass sequentially through all intermediate words. In a Transformer, the last word can directly access the first word. The "path length" for information to travel between any two words is always 1, regardless of their distance in the sequence. This eliminates the vanishing gradient problem associated with long sequences.
This next reading from a Stack Exchange discussion provides a concise yet powerful explanation of this fundamental difference.
Why does the transformer do better than RNN and LSTM in ...
This discussion gets right to the heart of the matter, contrasting the core properties of Transformers and RNNs.
Start by reading the sections under the first answer titled "Transformers" and "RNN / LSTM". Then, review the second answer, focusing on the concept of direct access via self-attention. Finally, the third answer provides a crucial insight regarding path length, which succinctly explains why Transformers avoid the vanishing/exploding gradient issue.
To see how this attention mechanism works under the hood, we'll turn to a more detailed video walkthrough. Given your CS background, you'll appreciate the mechanical explanation of queries, keys, and values.
This video by Yannic Kilcher is a classic deep dive into the "Attention Is All You Need" paper. We will watch specific segments that build the intuition from RNNs to Transformers.
First, watch the segment from the start of the RNN explanation to the end of the long-range dependency problem discussion. This recaps the limitations of sequential processing in detail. Next, see how attention was first introduced as a fix for RNNs from this section. This context is useful for understanding its evolution. Then, jump to the introduction of the Transformer architecture, which removes recurrence entirely, from this point. Finally, watch the explanation of how self-attention works using Queries, Keys, and Values. This is the core mechanism that replaced recurrence.
What About Word Order? Positional Encodings
A natural question arises: if the model processes all words at once, how does it know their original order? The Transformer solves this by adding a piece of information called a positional encoding to the embedding of each word. This is a vector that encodes the absolute or relative position of a word in the sequence. The model can then learn to interpret these positional signals to understand word order, syntax, and structure.
Summary: RNN vs. Transformer
Let's summarize the critical distinctions:
| Feature | RNN / LSTM | Transformer |
|---|---|---|
| Processing | Sequential (one token at a time) | Parallel (all tokens at once) |
| Dependencies | Relies on hidden states passed through time | Relies on self-attention for direct access between any two tokens |
| Information Path | Path length is O(n), where n is the distance between tokens |
Path length is O(1) between any two tokens |
| Scalability | Hard to parallelize, slow to train on large data | Highly parallelizable, enabling training on massive datasets |
| Complexity | Computation grows linearly with sequence length: | Computation grows quadratically with sequence length: |
The quadratic complexity of Transformers is a crucial trade-off. While self-attention solves the long-dependency problem, its computational cost grows rapidly with the length of the sequence. This is why most LLMs have a fixed maximum context window (e.g., 4096 or 128k tokens).
Conclusion
In this lesson, we've traced the evolution of sequence processing models. We saw that while RNNs and their variants like LSTMs were foundational, their inherently sequential nature created a bottleneck, limiting both their memory of long-range context and their ability to scale. The Transformer architecture represented a paradigm shift by completely removing recurrence. Its use of parallel processing and the self-attention mechanism solved the long-range dependency problem and, most critically, unlocked the ability to train on unprecedented amounts of data. This scalability is the single most important factor that has led to the powerful LLMs we have today.
In our next lesson, we will zoom in on the very first step of the NLP pipeline: tokenization. We'll explore how raw text is converted into the numerical representations that these sophisticated architectures can actually process.