↓ Skip to main content
AI & Data

How an LLM is built, part 1: the plan, the data and the design

·11 mins
How an LLM is built - This article is part of a series.
Part 1: This Article
An LLM is raised in two stages. First it reads a library far bigger than anyone could read in many lifetimes and learns to guess the next word. Then it does an apprenticeship: it is shown how to answer, made to practise, and graded until it is useful. Before either can start, someone has to build the library and design the student.
The nine stages of building Kimi K2, left to right: data (15.5T tokens), tokenizer (160K vocabulary), architecture (1.04T parameters, 32.6B active), pre-training (MuonClip, no loss spikes), long context (4K to 128K), supervised fine-tuning (23,000+ tools), reinforcement learning (verifiable rewards and self-critique), evaluation (SWE-bench Verified 65.8) and release (block-FP8, modified MIT). Stages 1 to 3 are highlighted as part 1, 4 and 5 as part 2, 6 to 9 as part 3.
Kimi K2’s pipeline; this part covers the first three stages
New terms are explained in plain words in Keywords at the end.

Why follow one real model
#

Most “how LLMs work” articles stop at the transformer diagram. The hard part, and the part that decides whether a model is any good, is everything around it: which data, how much compute, how to keep a months-long training run from blowing up, and how to turn a text predictor into an assistant that can use tools.

So I followed one model from start to finish. Kimi K2, from the Beijing company Moonshot AI, was released in July 2025 with downloadable weights and a detailed technical report. That makes it a rare case where the whole recipe is public, with real numbers. Moonshot’s newer K3 line (July 2026) is reported at 2.7 trillion parameters, but K2’s report is the one I could read stage by stage.

This series has three parts:

  1. The plan, the data and the design (this post): what goes in and what shape the model takes.
  2. Pre-training: the months on GPUs that produce a base model.
  3. From base model to assistant: fine-tuning, reinforcement learning, evaluation and release.

The whole pipeline
#

#StageWhat happensKimi K2
1DataCollect, clean, deduplicate and mix text15.5 trillion tokens: web, code, maths, knowledge
2TokenizerDecide how text is cut into pieces160K-token vocabulary
3ArchitectureChoose the network’s shape and sizeMixture-of-Experts: 1.04T parameters, 32.6B used per token
4Pre-trainingLearn to predict the next tokenMuonClip optimiser, no loss spikes
5Long contextLearn to read long inputs4K → 32K → 128K tokens
6Fine-tuningLearn from examples of good answers and tool useAgent conversations built from 23,000+ tools
7Reinforcement learningPractise, get graded, improveVerifiable rewards plus self-critique
8EvaluationMeasure and find weaknessesSWE-bench Verified 65.8
9ReleasePackage, license and serveBlock-FP8 weights, modified MIT licence

Stages 1–5 make the base model, a brilliant autocomplete. Stages 6–9 turn it into an assistant.

Stage 1: data, the library
#

A model can only know what it has read. Everything it will later “know” about Kubernetes, cooking or Hindi grammar comes from this stage, so most of a model team’s effort goes here.

Where the text comes from
#

SourceWhy it is there
Web pages (mostly Common Crawl, a public crawl of the web)Breadth: language, facts, opinions, styles
Code (public repositories)Programming, and a strong dose of step-by-step logic
Maths (papers, textbooks, solutions)Reasoning that can be checked
Knowledge (books, encyclopaedias, reference)Dense, reliable facts

K2 used exactly these four domains: web text, code, mathematics and knowledge.

Turning a crawl into training text
#

Raw web pages are mostly menus, adverts and near-copies. Moonshot does not publish its filtering code, but the open FineWeb dataset (15 trillion tokens from 96 Common Crawl snapshots, the same size class as K2’s data) documents every step:

  1. Block bad URLs with a list of adult and spam sites.
  2. Extract the text from the raw HTML, dropping navigation and boilerplate.
  3. Keep the right language with a language classifier (FineWeb keeps English with a score of at least 0.65).
  4. Quality filters: drop pages that are mostly short lines, repeated lines, lists of links or placeholder text.
  5. Deduplicate. Near-identical pages are found with MinHash (a fast way to spot documents that share most of their phrases) and only one copy is kept. Repeated text teaches a model to memorise instead of understand.
  6. Remove personal data such as email addresses before release.

A final step many teams now add is a quality classifier. FineWeb-Edu had a large model score 460,000 pages for educational value from 0 to 5, trained a small classifier on those scores, and kept pages scoring 3 or more. That cut 15 trillion tokens to 1.3 trillion, and models trained on the smaller set did much better on knowledge and reasoning tests. Less, but better.

How much text is enough?
#

The Chinchilla paper found that model size and data should grow together: “for every doubling of model size the number of training tokens should also be doubled”. Its 70B model, trained on four times more data than the 280B Gopher with the same compute, beat it. K2 was trained on 15.5 trillion tokens.

When good data runs out: rephrase it
#

There is only so much high-quality text in the world. The obvious move, showing the same good text many times, makes the model memorise it. K2 did something smarter: an LLM rewrote knowledge documents in different styles and from different perspectives, chunk by chunk, with checks that the facts survived.

The report’s test on SimpleQA (short factual questions) shows the effect:

Training dataSimpleQA score
Raw text, read 10 times23.76
10 different rephrasings, each read once28.94

Maths documents got the same treatment, rewritten in a “learning note” style, and good maths material in other languages was translated into English. Each corpus was rephrased at most twice, to limit the risk of drifting from the original.

The mix
#

The last data decision is the mix: how much of each domain the model sees. More code makes a better programmer; too much and its everyday writing suffers. Teams settle the mix with small test runs before the big one, and often change it near the end of training to favour the best data (part 2 covers that).

Stage 2: the tokenizer, cutting text into pieces
#

A model does not see letters or words. It sees tokens: pieces of text from a fixed list (the vocabulary), each mapped to a number. Common words are one token; rare words are split into pieces.

Most tokenizers are built with byte pair encoding (BPE), first used for translating rare words as subword units. The idea fits in a few lines:

  1. Start with single characters (or bytes) as the vocabulary.
  2. Count which pair of neighbours appears most often in the training text, say t + h.
  3. Merge it into a new token th and add it to the vocabulary.
  4. Repeat until the vocabulary is the size you want.

You can see the result with any tokenizer. This uses GPT-2’s (not K2’s, which needs the full model files), but the idea is the same:

# pip install transformers
from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("gpt2")
for text in ["Kubernetes", "the cat sat", "Fall. Get up. Build again."]:
    ids = tok.encode(text)                       # text -> token numbers
    pieces = [tok.decode([i]) for i in ids]      # each number back to its piece of text
    print(f"{text!r:30} {len(ids)} tokens: {pieces}")

On my machine (transformers 5.22) it prints:

'Kubernetes'                   4 tokens: ['K', 'uber', 'net', 'es']
'the cat sat'                  3 tokens: ['the', ' cat', ' sat']
'Fall. Get up. Build again.'   8 tokens: ['Fall', '.', ' Get', ' up', '.', ' Build', ' again', '.']

Common words are one token each (the leading space is part of the token), while a rarer word like Kubernetes is split into four pieces.

The trade-off is size. K2’s vocabulary has 160K entries, about three times GPT-2’s 50K. A bigger vocabulary means fewer tokens per sentence, so text is cheaper to process and more fits in the context window, especially for languages other than English. The price is a bigger table of token meanings (the embedding table) for the model to learn. The tokenizer is fixed before training; changing it later means starting again. AI in practice, part 3 shows why token counts also drive the bill.

Stage 3: the architecture, designing the student
#

The transformer in one paragraph
#

Almost every modern LLM is a transformer, the design from Attention Is All You Need. Each token becomes a long list of numbers (a vector). The vectors then pass through a stack of identical layers. In each layer:

  1. Attention: every token looks back at the tokens before it and decides which ones matter. In “the cat sat on the mat because it was tired”, attention is how it finds cat.
  2. Feed-forward network: each token’s vector goes through a small neural network on its own, where most of the model’s stored knowledge lives.

At the top, the final vector is turned into a probability for every token in the vocabulary: the guess for the next token. K2 has 61 layers, and each token’s vector has 7,168 numbers.

Mixture-of-Experts: a kitchen of specialists
#

The obvious way to make a model smarter is to make it bigger, but in a normal (dense) model every parameter works on every token, so cost grows with size. K2 instead uses Mixture-of-Experts (MoE).

Picture a restaurant kitchen with 384 specialist cooks in each MoE layer. For every token, a small router picks the 8 most suitable cooks, and 1 shared cook works on every dish. The kitchen holds the skill of all 384, but each dish costs only the time of nine.

StoredUsed per token
Parameters1.04 trillion32.6 billion
Experts per MoE layer3848 + 1 shared

That is a sparsity of 48: K2 stores 48 times more expertise than it pays for on each token. The catch is memory: all 1.04 trillion parameters must still be loaded somewhere, even if only 3% are used at a time.

Attention that remembers less
#

While generating, a model keeps a memory of every earlier token, the KV cache, so it does not recompute them. For long inputs this cache becomes the main cost of serving. K2 uses Multi-head Latent Attention (MLA), which compresses that memory into a smaller form; in DeepSeek-V2, where it was introduced, MLA cut the cache by 93.3%. K2 also uses 64 attention heads (parallel ways of looking back) where DeepSeek-V3 uses 128, which the report chose to make long inputs cheaper to run.

The design in numbers
#

SettingKimi K2
Layers61 (1 dense, the rest MoE)
Hidden size (numbers per token)7,168
Experts per MoE layer384, 8 active + 1 shared
AttentionMLA, 64 heads
Vocabulary160K
Parameters1.04T total, 32.6B active

With the library built and the student designed, the expensive part can begin. Part 2 follows the training run itself: 15.5 trillion tokens, thousands of GPUs, and the small trick that kept it from blowing up.

Takeaways
#

  • Two halves: stages 1–5 make a base model that predicts text; stages 6–9 make it an assistant.
  • Data is the product. Filtering, deduplication and a quality classifier matter more than raw size.
  • Rephrasing beats repeating: ten rewrites read once beat the same text read ten times.
  • Tokens are a design choice: a bigger vocabulary means shorter inputs but a bigger model to train.
  • MoE buys knowledge cheaply: 1.04 trillion parameters stored, 32.6 billion used per token.

Keywords
#

TermWhat it means
LLMLarge language model: a network trained on huge amounts of text to predict the next token.
TokenA piece of text (a word, part of a word, a symbol) from a fixed list.
VocabularyThe fixed list of tokens a model knows; 160K for K2.
BPEByte pair encoding: builds the vocabulary by repeatedly merging the most common pair of pieces.
Parameter / weightOne of the numbers inside the network that training adjusts.
Common CrawlA free, public crawl of the web, the starting point of most training data.
DeduplicationRemoving repeated or near-identical documents.
MinHashA fast technique to find documents that share most of their phrases.
Quality classifierA small model that scores pages so only good ones are kept.
RephrasingRewriting good documents with an LLM to get more varied training text.
TransformerThe standard LLM design: layers of attention plus feed-forward networks.
AttentionHow each token looks at earlier tokens to decide what matters.
LayerOne block of attention plus feed-forward network; models stack dozens.
Dense modelA model where every parameter works on every token.
Mixture-of-Experts (MoE)Many specialist sub-networks; a router uses only a few per token.
Active parametersThe parameters actually used for one token (32.6B in K2).
SparsityTotal experts divided by experts used; 48 for K2.
KV cacheThe model’s memory of earlier tokens while generating.
MLAMulti-head Latent Attention: compresses the KV cache.
Base modelThe model after pre-training: good at continuing text, not yet an assistant.

References
#

Related: Language Model and AI in practice, part 3: tokens and tokenomics.

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.
How an LLM is built - This article is part of a series.
Part 1: This Article

Related

What model weights are: from one neuron to a 19 GB file

·9 mins
A recipe is a list of quantities: 200 g of flour, 3 eggs, 180 °C. Change the numbers and you get a different cake. A model’s weights are its quantities, about 30 billion of them in the model I have been running, and the 19 GB file I downloaded is nothing more than that list, written down. Same idea at every scale: numbers that were learned, then written to disk. In my comparison of Ollama and Colibri I wrote “weights” a dozen times without saying what they are. This post fills that gap. Every number in it is either from a primary source or something I measured by opening the files on my own disk.

AI in practice, part 1: Choosing the right framework

“Which AI framework should I use?” has no single answer, because the names people throw around do completely different jobs. Put them on a map first, and the choice usually makes itself. This series picks up where my earlier posts left off. Those covered the ideas: what generative AI is, how transformers pay attention, why GPUs matter, and two hands-on builds (LangChain with Ollama and a RAG assistant). This part is the map that connects them.

AI on this blog: a reading map

·2 mins
I’ve written about AI and data on and off since 2023, one post at a time. Read in this order, they tell one story: from how machines learn, to how language models work, to building with them, to letting them act. 1. How machines learn # Post In one line The Core Divide Supervised vs unsupervised learning K-Nearest Neighbors Classify by the company you keep Mastering TF-IDF Turning text into numbers that mean something Data Mining Essentials Distances, clusters and the core vocabulary Matrix Multiplication to GPUs Why AI runs on graphics cards 2. How language models work # Post In one line GenAI What generative AI is, and its building blocks (2023) Language Model Transformers and the attention breakthrough Two Students, Two Talents Auto-regressive (GPT) vs auto-encoding (BERT) models The Evolution of Language Models From 2017’s transformer to today’s families The Probability Engine The maths of choosing the next token Tokens and tokenomics What a token is, context windows, and the bill 3. Building with models # Post In one line Ollama Running language models on your own machine A minimal LangChain + Ollama demo Prompt, model and chain, end to end A practical RAG assistant Answering from your own documents From speech to text Speech recognition in Python Choosing the right framework The AI stack, layer by layer ML pipelines on Vertex AI Train, evaluate and deploy, repeatably 4. Letting models act # Post In one line From RAG to tools and agents Function calling and the agent loop MCP, the Model Context Protocol One standard plug for tools and data The Ghost Board A story about agents nobody was watching The data shelf # Analytics and big-data posts that sit underneath much of the above: