Open the WaytoAGI knowledge base on Feishu · 100M+ visits, open to everyone →
WaytoAGI

Knowledge Base · Learning paths

Mixture of Experts (MoE) Architecture Explained

Mixture of Experts (MoE) is a neural network architecture that uses multiple smaller expert networks combined with a router to achieve sparse activation. By activating only a few experts for each input token, MoE enables massive-scale language models to deliver faster inference and lower computational costs, making it the core architecture behind today's top-performing LLMs.

Mixture of ExpertsMoE architectureMoE LLMsparse activationexpert networks

Translated from Chinese by AI. Spotted an issue? Use the feedback button.

Original post link: https://x.com/amitiitbhu/status/2042214049350664668

In this blog post, we will explore the Mixture of Experts (MoE) architecture, understand what “experts” are, how the router selects them, why MoE makes large models faster and cheaper, and why it powers many of today’s most powerful large language models (LLMs).

When we hear the term “Mixture of Experts,” it sounds complicated. But don’t worry. If we break it down into its individual parts, each piece is actually quite simple. Our goal is to explain this architecture clearly enough that, by the end, you’ll be able to explain how Mixture of Experts works to anyone.

We will cover the following topics:

  • Why we need Mixture of Experts
  • What an “expert” really means
  • The router and how it selects experts
  • Where MoE fits inside a Transformer
  • Sparse activation and why it saves compute
  • Load balancing between experts
  • The advantages and challenges of MoE
  • Why MoE powers many modern LLMs

I’m Amit Shekhar, from Outcome School, where I teach Artificial Intelligence and Machine Learning.

Let’s get started.

High-Level Overview

Before diving into the details, let’s first understand the big picture.

A Mixture of Experts model is a special kind of neural network. Instead of using one giant network for every input, it has many smaller networks called experts, and only activates a few of them for each input. A small component called the router decides which experts should be used for each input.

In simple terms:
Mixture of Experts = many small expert networks + one router that picks them.

You can think of it like a hospital. A hospital doesn’t have just one doctor treating every patient. It has many specialists — cardiologists, dermatologists, neurologists, and so on. When a patient arrives, the receptionist (the router) directs them to the appropriate specialist (the expert). The patient doesn’t need to see every doctor in the hospital — only the ones relevant to their problem.

This is exactly how Mixture of Experts works inside an LLM.

Why Do We Need Mixture of Experts Models?

Before MoE, large language models were dense. “Dense” means that every token uses every parameter in the model. If a model has 100 billion parameters, then during inference, every single token uses all 100 billion parameters.

This creates two major problems:

Problem 1: Computational Cost
As models grow larger to become smarter, the computational cost increases dramatically. Larger models mean slower inference and higher GPU cost per token.

Problem 2: Capacity Waste
Not every input needs every part of the model. Different inputs benefit from different sub-networks. But in a dense model, every input is forced to pass through every parameter, whether it needs them or not.

Mixture of Experts solves this problem. It allows us to build models with an enormous total number of parameters, while only using a small fraction of them for each input. We get the benefits of a massive model without having to pay the full computational cost.

What Is an “Expert”?

An expert is essentially just a small neural network. In most modern LLMs, each expert is a small feed-forward network — the same type of feed-forward network we’re familiar with from the Transformer architecture.

Inside a single Transformer layer of an MoE model, you might find 8 experts placed side by side, or 64, or even 128. Each expert is a complete feed-forward network on its own.

Note: The term “expert” can be a bit misleading. These experts are not specially trained to handle math or physics. During training, the model itself learns which experts should handle which types of inputs. In practice, experts tend to specialize in low-level patterns — such as punctuation, word types, or token shapes — rather than high-level topics. No one manually labels them.

Routing Module

Now that we have many experts sitting side by side, the next question is: how does the model decide which expert to use for each input?

This is where the router comes in. The router is a very small neural network that looks at each input token and decides which experts should process it. In practice, the router is usually just a linear layer followed by a softmax, rather than a deep network.

The router works through three simple steps.

First, the router examines the input token and generates a score for each expert. If there are 8 experts, the router produces 8 scores. The math behind it is straightforward:
Router(x) = Softmax(x × W_router), where x is the input token and W_router is the weight matrix learned by the router. The softmax here is simply a function that converts the raw scores into probabilities that sum to 1.

Next, the router selects the top-k experts with the highest scores. In most MoE models, k = 2, meaning each token is routed to only the two highest-scoring experts. Some models, such as the Switch Transformer, use k = 1.

Finally, the token is sent only to these top-k experts. All other experts are completely skipped for this token.

Below is a simple diagram illustrating how the router selects experts. In this example, the router has chosen E3 and E5:

markdown
Token
                            |
                         Router
                            |
         +------+------+----+----+------+------+
         |      |      |         |      |      |
         v      v      v         v      v      v
        E1     E2   [ E3 ]      E4   [ E5 ]   ... E8
                      |                |
                      v                v
                    Out_3            Out_5
                       \             /
                        \           /
                         \         /
                       Weighted Sum
                         (Combined)
                             |
                             v
                          Output

Only the selected experts are executed. The rest remain idle for this token. This is the core mechanism of how Mixture of Experts (MoE) works.

Where is (MoE) Located in the Transformer Architecture?

Before diving deeper, let’s quickly review the structure of a Transformer layer. Each Transformer layer consists of two main sublayers:

  • A multi-head attention sublayer
  • A feed-forward sublayer

In a standard (dense) Transformer, the feed-forward sublayer is a single large network that processes every token.

In an MoE Transformer, the feed-forward sublayer is replaced with a Mixture of Experts. The multi-head attention sublayer remains unchanged. Only the feed-forward portion is replaced.

Here is a simple diagram:

markdown
Input to Transformer Layer
                  |
                  v
       Multi-Head Attention
                  |
                  v
                Router               (picks top-k experts)
                  |
       +----+----+----+----+----+----+----+
       |    |    |    |    |    |    |    |
       v    v    v    v    v    v    v    v
       E1   E2 [E3]   E4 [E5]   E6   E7   E8     (only top-k run)
                  |
                  v
                Output

Therefore, MoE does not replace the entire Transformer. It only replaces the feed-forward sublayer in each Transformer layer. Everything else remains unchanged.

Note: Not every layer has to be an MoE layer. Some MoE models alternate between dense layers and MoE layers. This is a design choice.

Sparse Activation

This is exactly where MoE becomes truly powerful. Because each token only activates a small number of experts, we say the model is sparsely activated.

Let’s take a concrete example. Suppose we have an MoE model with 8 experts per layer, and the router selects the top 2 experts for each token. This means each token only runs 2 out of the 8 experts; the remaining 6 stay idle.

Note: Each MoE layer has its own independent router and its own learned routing weights. So if a model has 32 MoE layers, it has 32 separate routers—one per layer. Routers are not shared across layers, because each layer must learn its own strategy for routing tokens to the experts in that specific layer.

The following simple diagram illustrates that every MoE layer has its own router:

markdown
Token
                            |
                            v
              +-------------------------+
              |   Transformer Layer 1   |
              |                         |
              |   Multi-Head Attention  |
              |            |            |
              |        Router 1         |
              |            |            |
              |   E1 ..  [E3]  .. E8    |
              +------------|------------+
                           v
              +-------------------------+
              |   Transformer Layer 2   |
              |                         |
              |   Multi-Head Attention  |
              |            |            |
              |        Router 2         |
              |            |            |
              |   E1 ..  [E5]  .. E8    |
              +------------|------------+
                           v
                          ...
                           |
                           v
              +-------------------------+
              |   Transformer Layer 32  |
              |                         |
              |   Multi-Head Attention  |
              |            |            |
              |        Router 32        |
              |            |            |
              |   E1 ..  [E2]  .. E8    |
              +------------|------------+
                           v
                         Output

Each MoE layer has its own router and its own set of experts. The experts chosen at each layer can be different.

Therefore, although the model has the total parameter count of all 8 experts, the actual compute used per token corresponds to only 2 experts. This represents a massive saving in computational cost.

Activation Parameters vs. Total Parameters

MoE models are typically described as having “total parameters” and “active parameters.” Total parameters refer to the model’s complete size, while active parameters are the actual parameters used for each token.

Suppose each expert has 10 billion parameters. Then 8 experts together have 80 billion parameters in total. However, for each token, only 20 billion parameters are actually used. This model behaves in terms of compute cost like a 20B-parameter model, yet it carries the knowledge capacity of an 80B-parameter model.

For example, Mixtral 8x7B has 8 experts built on top of a 7B-parameter base model. Its total size is approximately 47 billion parameters, but only about 13 billion parameters are activated per token. This model runs at the speed of a 13B model but delivers performance closer to a 47B model.

Note: A common point of confusion is: “8 × 7 = 56, so why is Mixtral 47B instead of 56B?” The reason is that only the feed-forward sub-layers are duplicated across experts. The attention layers, embeddings, and normalization layers are shared across all experts. Therefore, the total parameter count is not simply (number of experts) × (expert size).

In simple terms: it has a big brain, but a small appetite.

Load Balancing

There’s another critical point. What if the router always selects the same two experts for every token? Then the other six experts would never learn anything, and the model’s capacity would be wasted.

This is a real issue during MoE training, known as the load imbalance problem.

To solve it, MoE models employ a special technique called load balancing. During training, in addition to the main loss, an auxiliary signal called the auxiliary load balancing loss is added (as proposed in papers such as Switch Transformer and GShard). This extra loss encourages the router to distribute tokens more evenly across all experts, ensuring each expert receives a fair share of tokens to learn from.

You can think of it as a manager in an office. If only two employees receive all the work while everyone else sits idle, the team is imbalanced. A good manager ensures work is distributed fairly across the entire team. The load balancing loss acts exactly like that manager for the router.

Without load balancing, an MoE model tends to collapse: only a few experts get used, while the rest become dead weight. With load balancing, all experts learn useful capabilities and the model can fully utilize its capacity.

There is also an additional practical detail called expert capacity. Each expert can only process a fixed number of tokens per batch. If too many tokens are routed to the same expert, the excess tokens are either dropped or sent to the next-best expert. This capacity limit is exactly why load balancing is so crucial in real-world systems.

Output Combination in MoE

When the router selects top-k experts for a token, each selected expert generates its own output. So how does the model combine these outputs into a single final result?

The router doesn’t just pick the top-k experts — it also produces weights for them, indicating how much we should trust each expert. The final output is simply the weighted sum of the outputs from the selected experts.

In simple terms:
final output = weight_1 * Expert_1(input) + weight_2 * Expert_2(input) (for top-2 experts).

These weights are obtained by applying a softmax only over the scores of the selected experts (not all experts). As a result, the weights of the chosen experts always sum to 1.

Let’s look at a concrete example. Suppose a token enters an MoE layer with 8 experts:

  • Step 1: The router scores all 8 experts. Assume the top-2 are Expert 3 (score 0.7) and Expert 5 (score 0.3).
  • Step 2: Only Expert 3 and Expert 5 are activated. Expert 3 produces output vector Out_3, and Expert 5 produces Out_5.
  • Step 3: The model combines them as follows:
    final output = 0.7 * Out_3 + 0.3 * Out_5
  • Step 4: This final output is passed to the next Transformer layer.

Because Expert 3 received a higher router score, its output is trusted more than Expert 5’s. This gives the model a smooth, blended response from the top experts rather than a crude single-expert answer.


Advantages of Mixture of Experts

Here’s why MoE has become so popular:

Massive scale at a fraction of the cost
We can build models with hundreds of billions or even trillions of total parameters, while keeping the number of active parameters per token much smaller. This is currently the only practical way to push model scale to the extreme.

Faster inference
Since each token only activates a small number of experts, the actual computation per token is relatively low. This translates to faster responses in production and significantly lower GPU costs.

Specialization
Different experts naturally learn to handle different types of inputs. The router figures out on its own which expert is good at what — no manual assignment needed. This gives the model rich, highly specialized knowledge.

Better scaling
Research shows that MoE models can typically reach the same quality as dense models using far less compute during training.


Challenges of Mixture of Experts

MoE is not free. It comes with its own set of challenges:

Memory cost
Even though only a few experts run per token, all experts must be loaded into memory. So an 80B-parameter MoE model still requires enough GPU memory to hold all 80B parameters, even if it only uses about 20B of them per token.

Load balancing is tricky
If training is not carefully designed, the router can collapse and always pick the same few experts. Engineers must add load-balancing losses and tune them meticulously.

Communication cost
In large-scale MoE models, experts are often distributed across different GPUs. Routing tokens to the correct GPU and retrieving results adds significant communication overhead.

Harder to fine-tune
Fine-tuning MoE models is more complex than fine-tuning dense models, because the router’s behavior can drift during the fine-tuning process.

Why MoE Powers So Many Modern Large Language Models

Today, many of the most powerful LLMs are built on Mixture of Experts (MoE) at their core. Mixtral, DeepSeek-V2, DeepSeek-V3, and several other cutting-edge models are all based on MoE. There are also strong indications that many top closed-source models use MoE internally.

The reason is straightforward. As models continue to grow, dense models hit a wall. Training and serving a dense model with hundreds of billions of parameters becomes extremely expensive. MoE breaks through this wall, allowing us to scale up the total model size without increasing the computational cost per token at the same rate.

In short, MoE is the answer to this question: How can we make the model smarter without making it slower or more expensive?

Summary

Let’s recap what we’ve learned:

  • Mixture of Experts is an architecture in which many small expert networks sit side by side, and a router selects only a few of them for each token.
  • Experts are small feed-forward networks. They are not manually assigned specialties—the model learns on its own which expert should handle what.
  • The router is a small network that scores the experts and selects the top-k for each token.
  • Sparse activation means only the selected experts are executed. This keeps MoE models fast even when the total parameter count is enormous.
  • MoE replaces the feed-forward sub-layer inside Transformer layers. The attention part remains unchanged.
  • Load balancing is necessary during training to ensure all experts are utilized.
  • The final output is a weighted sum of the selected experts, based on the router’s scores.
  • MoE enables massive model scale at a relatively low cost—which is why it powers so many of today’s most capable LLMs.

You now understand the Mixture of Experts architecture and how it works.

Prepare for your AI Engineering Interview: AI Engineering Interview Questions

That’s all for this post.

Amit Shekhar
Founder @ Outcome School