Building an LLM from scratch means training a transformer model on a large text corpus from random weight initialization — not fine-tuning an existing model. It's what companies like OpenAI, Anthropic, Google, and DeepSeek do to create their foundation models. Most developers will never need to do this, but understanding the process is essential for anyone working seriously with LLMs.
Modern LLMs are decoder-only transformers — they process input left to right and generate one token at a time. The core components:
Text must be converted to integer token IDs before training. BPE (Byte Pair Encoding) is standard — it builds a vocabulary of common subword units. Training a tokenizer requires running BPE on a representative text sample, typically 10-100GB. Most teams reuse an existing tokenizer (Llama's, Qwen's, or a custom one) rather than building from scratch.
Pre-training requires trillions of tokens of high-quality text. The data pipeline involves:
for batch in dataloader:
tokens = batch["input_ids"] # (batch_size, seq_len)
logits = model(tokens[:, :-1]) # predict next token
loss = cross_entropy(logits, tokens[:, 1:]) # compare to actual next token
loss.backward()
optimizer.step()
optimizer.zero_grad()
Standard optimizer: AdamW with cosine learning rate schedule and warmup. Newer models (including DeepSeek V4) use the Muon optimizer for better sample efficiency.
| Model size | Layers | Hidden dim | Attention heads | Training tokens |
|---|---|---|---|---|
| 1B | 16 | 2048 | 16 | ~1T |
| 7B | 32 | 4096 | 32 | ~2T |
| 13B | 40 | 5120 | 40 | ~2T |
| 70B | 80 | 8192 | 64 | ~3-4T |
| Model size | GPU hours (A100) | Estimated cost |
|---|---|---|
| 1B (2T tokens) | ~2,000 A100-hours | ~$6,000 |
| 7B (2T tokens) | ~35,000 A100-hours | ~$100,000 |
| 70B (3T tokens) | ~400,000 A100-hours | ~$1.2M |
| Frontier model (1T+ params) | Millions of GPU-hours | $50M-$500M+ |
Building from scratch is only justified when:
For everything else: start from an open-weight model and fine-tune with Unsloth or LoRA. A fine-tuned Llama 3.3-70B will outperform a scratch-trained 7B on almost any task, at a fraction of the cost.
The best LLM for your use case is almost never one you trained from scratch. It's an open-weight model fine-tuned on your specific data and aligned with your users' preferences.
Tokoscope works with any OpenAI-compatible endpoint. Free to start.
Get started free →