Key takeaways

  • Building an LLM from scratch means training a tokenizer, pretraining a transformer on next-token prediction, supervised fine-tuning on conversations, and preference tuning.
  • Minimind's README estimates pretraining plus fine-tuning its 64M-parameter model at about 2.31 hours on one RTX 3090, for about 3 yuan.
  • Nanochat's README says a GPT-2-level model takes about 2 hours on an 8-GPU H100 node, about $48 at $3 per GPU-hour.

Building an LLM from scratch takes four steps: train a tokenizer, pretrain a transformer to predict the next token, fine-tune it on conversations, then tune it on preferences. The open-source minimind repository does all four for a 64-million-parameter model, and its README puts the first two at about 2.3 hours on one rented Nvidia RTX 3090, for about 3 yuan.

Minimind was number four on GitHub's trending page on November 24, 2026, with 828 new stars that day and 69,868 in total. Together with Andrej Karpathy's nanochat, it shows the whole pipeline in plain PyTorch, with real hardware and real bills attached.

Step 1: a tokenizer turns text into numbers

A model can't read letters. A tokenizer splits text into pieces called tokens and gives each one an ID. Most LLMs use byte pair encoding (BPE), which starts from single bytes and keeps merging the most common pairs. A 2015 paper by Sennrich and colleagues brought it to neural translation, and the original transformer paper used a shared BPE vocabulary of about 37,000 tokens. There is more on what tokens are in our token explainer.

Vocabulary size is a trade-off. Minimind uses just 6,400 tokens, against 128,000 for Llama 3 and 151,643 for Qwen2, according to its README. Every token needs its own row in the embedding table, and a big vocabulary would eat most of a 64M-parameter budget. Nanochat trains its own tokenizer with 32,768 tokens on about 2 billion characters of its pretraining data.

Step 2: pretraining teaches the model to continue text

The model itself is a transformer, the architecture from the 2017 paper "Attention Is All You Need". Its base model trained for 12 hours on eight Nvidia P100 GPUs, and the bigger one for 3.5 days, reaching 28.4 BLEU on English-to-German translation.

Minimind's current model stacks 8 transformer layers, each 768 numbers wide, with 8 query heads sharing 4 key-value heads. A mixture-of-experts version adds 4 experts per layer and sends each token to one of them: 198M parameters in total, 64M active per token.

Pretraining means showing the model huge amounts of text and having it guess each next token, over and over. Nothing is labeled. Minimind's quick pretraining file is 1.2 GB of text, and the full one is 10 GB. Nanochat downloads about 150 shards of roughly 250 million characters each to reach GPT-2 level.

How much text is enough? DeepMind's Chinchilla paper trained over 400 models and found that model size and training tokens should grow together: double one, double the other. Its 70B model saw 1.4 trillion tokens. Nanochat sets its default ratio at 10.5 tokens per parameter, and its speedrun drops that to 8 to finish faster.

Step 3: supervised fine-tuning teaches it to chat

A pretrained model only continues text. Supervised fine-tuning (SFT) trains it further on example conversations, so it learns the roles of user, assistant, system and tool, and answers instead of rambling on. There's a longer explanation in what is fine-tuning.

Minimind's quick SFT file is 1.6 GB and the full one 14 GB, larger than its pretraining data. In nanochat, SFT teaches the conversation tokens, tool use and multiple-choice answers.

OpenAI's InstructGPT paper showed why this stage matters. Human raters preferred answers from a 1.3B-parameter model tuned this way over the 175B GPT-3 it started from, despite it having 100 times fewer parameters.

Step 4: preference tuning teaches it which answers are better

SFT shows the model good answers. Preference tuning shows it pairs, a better answer and a worse one, and pushes it toward the better kind. InstructGPT did this with RLHF, which first trains a separate reward model. Direct Preference Optimization (DPO) skips the reward model and uses a simple classification loss. Its authors report it matches or beats PPO-based RLHF on summarization and single-turn dialogue.

Minimind implements DPO on a 53 MB preference file, plus the reinforcement learning methods PPO, GRPO and CISPO, all marked optional. Nanochat's speedrun stops after SFT. It ships a separate script that runs a stripped-down GRPO on GSM8K math problems.

What the hardware and the bill look like

The repos state these numbers themselves (as of November 24, 2026):

Repo Model Hardware Time Cost stated
minimind 64M parameters, pretrain + SFT on the mini datasets 1 RTX 3090 (24 GB) about 2.31 hours about 3.0 yuan, at 1.3 yuan per GPU-hour
minimind 198M MoE, same data 1 RTX 3090 about 3.23 hours about 4.2 yuan
nanochat GPT-2-level model, depth 24 1 node of 8 H100s about 2 hours (record: 1.65) about $48 at $3 per GPU-hour, about $15 on spot
nanoGPT GPT-2 124M on OpenWebText 1 node of 8 A100 40 GB about 4 days not stated

Minimind's README converts at 7 yuan to the dollar, which makes its 3 yuan run about $0.43. Its top line promises 2 hours, and a note says that figure is one epoch of SFT. The cost table puts pretraining plus SFT at 2.31 hours.

Nanochat measures against GPT-2. Karpathy writes that training GPT-2 in 2019 cost about $43,000 on 32 TPU v3 chips over 168 hours. His leaderboard's fastest run beat GPT-2's CORE score in 1.65 hours on March 14, 2026. On one GPU the same code runs about 8 times longer, and cards with less than 80 GB of memory need a smaller batch size. nanoGPT, Karpathy's older repo, is now marked deprecated in favor of nanochat. Its Shakespeare demo still trains a tiny character-level model in about 3 minutes on one A100.

What these models can and can't do

They are small, and their authors say so. Minimind-3 scores 28.49 on ARC-Easy and 50.65 on PIQA, against 58.50 and 68.17 for Hugging Face's SmolLM2-135M. The README calls its benchmark results "only for fun" and notes its training data leans heavily toward Chinese. Karpathy compares talking to a nanochat speedrun model to talking to a kindergartener.

That's the point of these repos. Nobody will replace a frontier model with a 64M-parameter one. What you get is every stage of the pipeline in plain PyTorch you can read line by line, and a run cheap enough to repeat after breaking something.

What we ran

In a cloud VM with no GPU, we cloned minimind at its November 23, 2026 commit, installed CPU-only PyTorch 2.6.0 and Transformers 4.57.6, and built the model from its default config without downloading any weights. The dense model came to 63,912,192 parameters, of which 4,915,200 sit in the shared embedding table. The MoE config came to 198,416,640. The bundled tokenizer has 6,400 entries and split "The sun sets in the west" into 8 tokens. The README's example shows 6. The untrained model's first loss on random tokens was 8.945, close to ln(6,400) = 8.76, which is what pure guessing scores. One forward and backward pass on a batch of two 128-token sequences took 0.96 seconds on the CPU.

For background on the model itself, start with what an LLM is and how a neural network learns.

Frequently asked questions

How much does it cost to train an LLM from scratch?

It depends on size. Minimind's README puts a 64M-parameter chat model at about 3 yuan (about $0.43) of rented RTX 3090 time, and nanochat's README puts a GPT-2-level model at about $48 on an 8-GPU H100 node, or about $15 on spot instances.

Where can I find an LLM from scratch on GitHub?

Two current options are jingyaogong/minimind (Apache 2.0), which covers tokenizer, pretraining, SFT, DPO and RL for a 64M model, and karpathy/nanochat (MIT), which trains a GPT-2-level chat model. Karpathy's older nanoGPT is now marked deprecated in favor of nanochat.

Can I build an LLM from scratch in Python?

Yes. Both minimind and nanochat are written in Python with PyTorch, and minimind implements its training algorithms directly in PyTorch rather than through higher-level libraries.

Can I train an LLM from scratch without a GPU?

Only a very small one. Nanochat ships a CPU script that shrinks the model to train in a few tens of minutes, and its README warns the results will not be strong.

Sources

  1. minimind READMEGitHub (jingyaogong)
  2. nanochat README and speedrun scriptGitHub (Andrej Karpathy)
  3. nanoGPT READMEGitHub (Andrej Karpathy)
  4. Attention Is All You NeedarXiv
  5. Neural Machine Translation of Rare Words with Subword UnitsarXiv
  6. Training Compute-Optimal Large Language ModelsarXiv
  7. Training language models to follow instructions with human feedbackarXiv
  8. Direct Preference Optimization: Your Language Model is Secretly a Reward ModelarXiv
  9. GitHub trending repositoriesGitHub
  10. minimind-3 model cardHugging Face