Leap Nonprofit AI Hub

How LLMs Work: Embeddings, Attention, and Feedforward Networks

How LLMs Work: Embeddings, Attention, and Feedforward Networks Oct, 19 2026

You stare at a chatbot, type a question, and get a coherent answer in seconds. It feels like magic, but it’s math. Specifically, it’s three specific mathematical operations working together to turn your words into numbers, understand how those numbers relate, and then decide what comes next. If you’ve ever wondered why Large Language Models (LLMs) can write code or summarize essays, the answer isn’t some mysterious consciousness. It’s the Transformer architecture, which relies on a triad of components: embeddings for representation, attention for context, and feedforward networks for processing.

Most people think AI "thinks" like us. It doesn’t. It calculates probabilities based on vector geometry. To really grasp how models like GPT-4 or Llama 3 function, you need to look under the hood. We’re going to break down the three pillars that make these systems work, skipping the heavy academic jargon for clear, actionable insights.

The Foundation: Turning Words into Vectors with Embeddings

Computers don’t read English; they read numbers. But not just any numbers. They need dense vectors-lists of numbers that capture meaning. This is where Embeddings come in. An embedding layer converts discrete tokens (chunks of text) into continuous vector spaces. Think of it as creating a map where every word has a coordinate. On this map, words with similar meanings sit close together. "King" is near "Queen." "Apple" is near "Pear," but far from "Truck."

This process starts with tokenization. Before an LLM sees your sentence, it breaks it into subwords using algorithms like Byte Pair Encoding (BPE). Why? Because rare words like "antidisestablishmentarianism" are hard to handle. By splitting them into known chunks, the model handles vocabulary more efficiently. Once tokenized, each piece gets mapped to a high-dimensional vector. In BERT-base, these vectors have 768 dimensions. In GPT-3, they jump to 12,288. The higher the dimension, the more nuanced the semantic information the model can store.

But there’s a catch. Transformers process all tokens in parallel, unlike older Recurrent Neural Networks (RNNs) that read left-to-right. Without order, "dog bites man" looks identical to "man bites dog." To fix this, we add Positional Encodings. These are unique patterns added to the token embeddings, telling the model exactly where each word sits in the sequence. Without positional data, the model loses syntax entirely.

The Heart: Understanding Context via Self-Attention

If embeddings are the map, Self-Attention is the compass. It’s the mechanism that allows the model to understand relationships between words, regardless of how far apart they are in the sentence. When you read the sentence "The animal didn't cross the street because it was too tired," you know "it" refers to the animal, not the street. You did this instantly. The LLM does it by calculating attention scores.

Here’s how it works mechanically. For every token, the model generates three vectors: Query (Q), Key (K), and Value (V). Imagine Q as asking a question: "What am I looking for?" K acts as a label: "This is what I offer." V holds the actual content. The model compares Q against all Ks to see which other tokens are relevant. If the match is strong, the attention score is high, and the model pulls more information from that token’s V vector.

Mathematically, this is expressed as $\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V$. Don’t let the formula scare you. It just means: compare queries to keys, normalize the scores so they sum to one (using softmax), and weigh the values accordingly. The division by $\sqrt{d_k}$ prevents the dot products from becoming too large, which would push the softmax function into regions with tiny gradients, slowing down learning.

Modern LLMs use Multi-Head Attention. Instead of one single attention mechanism, they run several in parallel (called heads). GPT-2 uses 12 heads; GPT-3 uses up to 96. One head might focus on grammatical structure, another on coreference (who is "he"?), and another on long-range dependencies. This parallelism allows the model to track multiple types of linguistic relationships simultaneously, which is key to its surprising fluency.

Comparison of Attention Mechanisms in Major LLMs
Model Attention Type Heads Key Feature
BERT Bidirectional 12 Sees past and future tokens; good for classification
GPT-3 Causal (Masked) 96 Only sees previous tokens; enables generation
Llama 2 Grouped-Query 32 Shares KV heads to reduce memory usage
Abstract web of light connecting spheres illustrating attention mechanisms

The Processor: Feedforward Networks and Transformation

Once the attention mechanism has gathered context, the information needs to be processed. This happens in the Feedforward Network (FFN). Unlike attention, which mixes information across tokens, the FFN operates independently on each token. It’s essentially a two-layer neural network that transforms the enriched representation from the attention layer.

The typical structure involves expanding the dimensionality first, applying a non-linear activation function (usually GELU), and then projecting back down. For example, if the embedding size is 768, the FFN might expand it to 3072, process it, and shrink it back to 768. This expansion allows the model to learn complex, non-linear features. Think of it as unpacking a suitcase, reorganizing the clothes, and packing it again. The attention layer decides *what* to look at; the feedforward network decides *how* to interpret it.

Interestingly, research suggests that FFNs act as key-value memories. They store factual knowledge learned during training. When the attention layer retrieves a concept, the FFN helps retrieve associated facts. This separation of duties is crucial: attention handles dynamic context, while FFNs handle static knowledge storage.

Putting It All Together: The Transformer Block

These three components don’t work in isolation. They are stacked in repeating units called Transformer Blocks. A standard block consists of:

  • Layer Normalization: Stabilizes activations before and after sub-layers.
  • Self-Attention Sub-layer: Updates token representations based on context.
  • Residual Connection: Adds the input directly to the output to prevent vanishing gradients.
  • Feedforward Sub-layer: Processes each token individually.
  • Another Residual Connection: Ensures deep networks remain trainable.

A model like GPT-3 stacks 96 of these blocks sequentially. Data flows through them one by one. Each layer refines the understanding slightly. Early layers might focus on basic grammar and part-of-speech tagging. Middle layers handle semantic relationships and coreference. Later layers deal with task-specific reasoning and output prediction. This depth is what gives LLMs their "emergent" capabilities-skills that weren’t explicitly programmed but arise from scale.

Crystals transforming into smooth shapes symbolizing feedforward networks

Why This Architecture Matters for Developers

Understanding these components isn’t just academic trivia. It impacts how you build and optimize applications. Here’s what you need to know:

  • Memory Costs: Attention scales quadratically with sequence length. Doubling the context window quadruples the compute cost. This is why long-context models are expensive.
  • Optimization Targets: Most inference time is spent in attention (60-70%) and FFNs (30-40%). Techniques like FlashAttention speed up attention calculations by optimizing GPU memory access.
  • Fine-Tuning Strategy: If your model struggles with factual accuracy, look at the FFN weights. If it fails at coherence or pronoun resolution, investigate the attention heads.

Developers often hit walls when trying to customize these models. For instance, implementing custom attention masks requires a deep grasp of the QKV mechanics. Many teams rely on libraries like Hugging Face Transformers, which abstract away much of the complexity, but knowing the underlying principles helps you debug issues like position embedding failures or numerical instability.

Common Pitfalls and Misconceptions

One major misconception is that LLMs "understand" language like humans. They don’t. They predict the next token based on statistical likelihoods derived from vector proximity. Another pitfall is ignoring the importance of positional encodings. Older models used fixed sinusoidal functions; newer ones like Llama use Rotary Position Embedding (RoPE), which generalizes better to longer sequences. Using outdated positional methods can degrade performance on long texts.

Also, remember that the feedforward network is opaque. As Dr. Emily Bender noted, the transformation from attention output to final prediction is a "black box." You can’t easily trace why a specific word choice emerged. This lack of interpretability remains a significant challenge for regulated industries needing audit trails.

What is the difference between embeddings and tokens?

Tokens are the raw pieces of text (like words or subwords) after tokenization. Embeddings are the numerical vector representations of those tokens. Tokens are discrete IDs; embeddings are continuous vectors that capture semantic meaning.

Why do LLMs use multi-head attention instead of single-head?

Multi-head attention allows the model to attend to different positions from different representation subspaces. One head might focus on syntax, another on semantics. This parallel processing captures richer linguistic relationships than a single attention mechanism could.

Do feedforward networks contain factual knowledge?

Research suggests yes. Feedforward networks act as key-value memories storing static facts learned during training, while attention mechanisms handle dynamic context retrieval. Editing FFN weights can sometimes update factual knowledge without retraining the whole model.

What is the computational cost of attention?

Self-attention has quadratic complexity O(n²) relative to sequence length n. Processing a 32,000-token sequence requires over 1 billion pairwise comparisons, making long-context inference resource-intensive and expensive.

Can I modify the feedforward network in pre-trained models?

Yes, techniques like LoRA (Low-Rank Adaptation) allow fine-tuning specific parts of the model, including FFNs, by injecting trainable low-rank matrices. This reduces computational costs compared to full fine-tuning.