AI Run Lab

All chapters

Anatomy of a language model

What have you actually downloaded when you download an LLM? Follow a token from a file on disk to the tensors used during a prediction.

Open this chapter with experiments and audio

What lives in a checkpoint?

A directory for an open-weight model usually contains settings describing the architecture, tokenizer assets, and one or more files of numerical weights. A loader uses the settings to construct layers of the right shape. It places the saved arrays into the corresponding parameter slots. The tokenizer must produce the IDs expected by those arrays.

ComponentExampleDuring inference
Configurationhidden size = 256Sets the width of each hidden state.
Tokenizer“room” → ID 42Converts the prompt into IDs using saved rules.
ParametersEmbedding row 42Supplies the learned numbers used for this token.
ActivationsHidden state at position 8Computed for the current sequence.

A checkpoint saved during training can additionally contain optimizer state, the training step and random-number-generator state to help resume training. An inference distribution may contain only the material needed to run the trained model.

Can you estimate the size of one layer?

Take a dense projection that maps 256 input features to 1,024 hidden features. Each output has one weight for every input. That gives 256 × 1,024 = 262,144 weights. Including one bias per output adds 1,024 numbers. Like the small network in chapter 14, this layer uses one set of parameters for all token positions.

Try 7 billion parameters at 16 bits. The raw weight arrays occupy 14 GB in decimal units. A request containing 10 tokens and a request containing 1,000 tokens reuse those same arrays. The longer request usually needs more temporary state, cache space and processing time. A context window limits how many tokens the model or runtime supports for a single sequence.

How does a mixture of experts change the count?

Some layers offer several feed-forward networks called experts. A learned router selects a small set of experts for each token and combines their outputs. If a layer has eight equally sized experts and selects two per token, it uses a quarter of that expert bank per token. Shared attention, embeddings and other operations still run. Different tokens can select different experts.

Follow a token through a four-expert layer. Compare a single selected expert with two selected experts, then change the token. The example computes a weighted mixture of scalar expert outputs.

With one selected expert its mixing weight is 100%. With two, the router scores determine their relative contributions. The enabled experts can change for each token even though the available set of four stays the same.

The total parameter count includes all experts. The active count reflects only the path executed for a given token. Across many tokens all experts may be used, so memory still needs to accommodate or fetch their weights. A parameter count alone does not determine serving cost or quality.

Inspect the shape without downloading a model

features, hidden, bytes_per_value = 256, 1024, 2
weights = features * hidden
biases = hidden
print("weights", weights, "biases", biases)
print("raw bytes", (weights + biases) * bytes_per_value)

The last line prints 526,336 bytes for this one layer. To account for a whole model, repeat this for all distinct parameter tensors. Some models share the input embedding matrix with the output projection. A shared matrix is stored once even though the computation uses it twice.

Which artifact changes if you submit a longer question to a frozen model?

The input IDs, temporary activations and often the KV cache. The saved parameters and architecture configuration stay fixed.

Source · Hugging Face · Safetensors

A mixture of experts routes each token inside a layer

A dense feed-forward layer uses the same parameter matrices for every position. A sparse mixture-of-experts layer owns several such networks. A learned router scores them from the current token vector. Suppose it activates two experts with renormalized weights 0.75 and 0.25. If one output coordinate is 2 in the first expert and −2 in the second, their combined coordinate is 1. The next token may select another pair.

The Qwen3 release of 29 April 2025 lists dense and sparse models. Qwen3-235B-A22B has about 235 billion total parameters and activates about 22 billion per token. Active parameters help explain per-token computation. The full collection still has to be stored somewhere. With many simultaneous requests, tokens can visit many different experts. Movement between devices then matters as well.

Meta’s April 2025 description of Llama 4 Maverick gives a different design. It alternates dense and expert layers. In an expert layer one expert is shared by all tokens, and the router selects one additional expert from 128. Meta reports about 17 billion active parameters out of 400 billion total. Looking inside the routing rule explains why the expert count alone cannot determine the number of weights used for one token.

Change the router input and inspect the selected experts →

Sources · Qwen3, 2025 · Meta, Llama 4, 2025

What a model family name lets you infer

In its system card of 7 August 2025, OpenAI describes GPT-5 in ChatGPT as a system that can route a request to a fast model or a reasoning model. That decision concerns a request. The Qwen3 example above routes individual token representations inside a network layer. The same word “router” describes decisions at two different levels of the stack.

Anthropic’s Claude 4 announcement of 22 May 2025 describes standard and extended-thinking modes. Google’s Gemini 3.1 Pro announcement of 19 February 2026 describes availability through the Gemini API, Vertex AI and user applications. These are dated examples of behavior or delivery. Those descriptions alone cannot establish a layer count, number of experts, or training corpus. To reproduce a comparison, record the dated version, interface, permitted tools, output budget and evaluation cases.

A version change can alter a concrete dependency. On 16 April 2026 Anthropic reported that its updated tokenizer for Claude Opus 4.7 could yield about 1.0–1.35 times as many tokens for the same input as before, depending on the content. A comparison with a fixed limit in tokens may therefore supply different amounts of text. Count both the input characters and the tokens for each model when testing this effect.

Locate the model endpoint inside a complete application →

Sources · OpenAI, GPT-5 System Card, 2025 · Anthropic, Claude 4, 2025 · Google, Gemini 3.1 Pro, 2026 · Anthropic, Claude Opus 4.7, 2026