My try at building an LLM from scratch · open source

Hedwig

From a MacBook Air to 286.7 million knobs on rented GPUs for $2.45, and how it compares to GPT and Claude.

286.7M

knobs in the biggest brain, trained from random numbers

$2.45

total rented-GPU spend across all three cloud runs

26×

faster training on a rented RTX A5000 vs the MacBook Air M4

98.3%

preference win-rate after taste training on the flagship

40/40

pieces the JavaScript browser engine matches with PyTorch

I

Why I built this

My try at an LLM, from zero knobs.

Everyone uses ChatGPT and Claude. I wanted to build one myself in pure PyTorch with no training frameworks, so every piece stays visible and now runs right inside your browser.

User: write a short story about a brave little owl and a golden key.

● A real run of the 13.8M model, replayed

Live · 14 MB brain

Wake Hedwig up.

Loads into this tab, then writes at about 90 pieces a second on an M-series Mac.

Or open it on Hugging Face.

40 / 40pieces match PyTorch, on both brains
2 brains13.8M (14 MB) or 97.6M (98 MB)
$0to host: a static page
II

What I did on my Mac · $0.00

The whole ChatGPT stack, on one laptop.

I started with a 4.8M character-level Shakespeare model, wrote a BPE tokenizer from scratch, upgraded to Llama-style parts one change at a time, and ran all three post-training stages on a 16 GB MacBook Air M4.

01 · Data & Tokenizer

Word pieces

Built BPE from scratch (3.87 chars/piece at 4k vocab) across TinyStories and 10 Gutenberg novels.

02 · Modern Engine

Pure PyTorch

Upgraded one piece at a time: RoPE, RMSNorm, SwiGLU, bf16 autocast and fused SDPA attention.

03 · Chat + Taste

SFT & DPO

Prompt masking (-100) taught chat manners; anchored DPO fixed reward hacking cleanly.

04 · Shrink & Ship

7.9 MB offline

Packed 4-bit quantization (7x smaller) + KV cache (89 tok/s) in Mac app, iPhone and browser.

1

Reading

Guess the next piece millions of times. Val score dropped 8.501 → 2.080 → 1.795.

PretrainingOnce upon a time, a little owl found a shiny key

13.8M val 1.795

2

Manners

13,852 pairs graded only on the reply (-100 prompt mask). Now it answers and stops.

SFTUser: Tell me a story where Lily finds a shiny key.
Hedwig: Once upon a time…

13.8M 1.438 · 97.6M 1.336

3

Taste

Two stories, one liked. Learns to stay on topic and drop clumsy repetition.

DPO👍 an owl and a tree, as asked
👎 Spot the dog, again

win-rate 92.5% → 96.7% → 98.3%

Caught cheating · DPO run 1 · step 300Lily loves Lily's key. Lily does Lily's mom's idea…

Cramming "Lily's" in widened the score gap. The score went up; the stories fell apart. Reward hacking at 14M knobs.

Fixed · DPO run 2An anchor for fluency.

Adding 0.5x chosen NLL kept liked stories grammatical, as Llama 3 did. The loophole closed and win-rate hit 92.5% peak.

8 → 2

Mixture of Experts

26.2M total knobs, only 11.6M awake per word. Loss 2.461 → 1.966. moe.py

3.4M

Distillation

Baby Hedwig taught by the 13.8M teacher (6.8 MB), still writes full stories. distill.py

6×

Apple MLX

Training step 596 → 99 ms, identical next 80 pieces to PyTorch. mlx_bench.py

2.8×

LLM in a Flash

Layers streamed from SSD: 52.9 → 18.8 MB held in memory, 33 tok/s. ssd_runner.py

30/30

iPhone brain

64-seat Core ML export matching PyTorch, plus Swift tokenizer. export_iphone.py

🇫🇷

French Hedwig

2.56M knobs on public-domain Dumas & Verne, val 7.776 → 3.541. lang_hedwig.py

Squeezing the brain

Same 13.8M chat brain, same test, lower is better. 8-bit is free; 4-bit costs 0.023.

32-bit
55.5 MB · 1.4530
16-bit
27.8 MB · 1.4530
8-bit
14.0 MB · 1.4531
4-bit
7.9 MB · 1.4765
III

Why the Mac wasn't enough & why GPUs are faster

The Mac hit a wall. Rented GPUs ran 26× faster.

Training work scales as FLOPs = 6 × knobs × tokens. A full pass over TinyStories takes ~20 hours on a fanless MacBook Air M4 as it thermally throttles from 66s to 110s per 250 steps, and a 4k dictionary chops rare names apart. I rehearsed a 2-step dry run on Mac (92s) and rented RunPod GPUs.

Same 50 MB lesson (13.8M knobs, 3,000 steps)

MacBook Air M4 ($0)8-core GPU · fanless thermal throttle
~59 min (3,540 s)
Rented RTX A5000 ($0.10)1× data-center GPU · $0.28/hr
135 s · 26× faster

Why NVIDIA GPUs fly

Tensor Cores, FlashAttention & VRAM

Data-center GPUs run thousands of dedicated bf16 Tensor Cores in parallel, execute fused CUDA FlashAttention in fast SRAM, push hundreds of GB/s across 24 to 48 GB VRAM, and keep active cooling so speed never drops.

Why 14M failed Harry Potter

4k vocab split "Potter" in half

Without a single token for "Potter", 14M Hedwig saw "pot" + "ter", latched onto "pot", and turned Harry into a puppy or Timmy making a pot of soup. At 8k and 16k vocab, the whole name stays whole.

4k: "Harry""pot""ter"→16k: "Potter"
115 NOV

Guessing one letter at a time

  • 4.8M knobs on Tiny Shakespeare, 16 minutes
    loss 4.388 → 1.483
  • A learning log and a $100 plan before spending anything
216 NOV

Save points and my own tokenizer

  • Stopped mid-run with Ctrl+C, resumed
    1.622 → 1.481
  • BPE from scratch
    text 1.96× shorter
  • First lesson in overfitting
    3.007 → 3.429
418 NOV

The modern stack, then all three stages

  • RoPE, RMSNorm, SwiGLU, bf16, SDPA, one at a time
  • 13.8M storyteller
    val 8.501 → 2.080
  • 8-bit and 4-bit from scratch
    7.05× smaller
  • Taste training, caught cheating, fixed
    win-rate 0% → 91.7%
519 NOV

The lab week, on a laptop, for $0

  • KV cache + streaming
    47 → 89 pieces/s
  • Mixture of Experts, distillation, Brain X-Ray
    26.2M total, 11.6M active
  • A longer read
    val 2.080 → 1.795
620 NOV

Rented GPUs, open source, live demo

  • Smoke test on an RTX A5000 (26× faster)
    $0.10
  • 97.6M on an A40
    $0.65 · 96.7%
  • 286.7M on an RTX 4090
    $1.70 · 98.3%
  • MIT code, open weights, browser demo

Keep scrolling →

IV

How much we improved

Same prompt. Three brains.

Different dictionaries (4k, 8k and 16k) make raw reading scores unfair to compare. Comparing the models side by side across specs and unedited outputs from docs/STORY_SHOWDOWN.md shows the exact improvement.

Metric13.8M Mac97.6M Cloud286.7M Flagship
Layout (layers × width × heads)6 × 384 × 612 × 768 × 1220 × 1024 × 16
Context / Vocabulary256 / 4,096256 / 8,192512 / 16,384
Hardware & CostMac M4 · $0.001× A40 (66m) · $0.651× 4090 (115m) · $1.70
Tokens seen (per knob)~125M (~9 / knob)~164M (~1.7 / knob)~205M (~0.7 / knob)
Stage 2 SFT / Stage 3 DPO win1.438 / 92.5%1.336 / 96.7%1.341 / 98.3%
4-bit compressed size7.9 MB55.2 MB161.9 MB

From docs/STORY_SHOWDOWN.md, seed 42, temperature 0.65. Cut for length, never rewritten.

V

Hedwig vs GPT & Claude

Same recipe. 10,000+ GPUs.

ChatGPT and Claude use the same Transformer blocks and three-stage training (Pretrain, SFT, DPO/RLHF). Frontier models aren't hiding a secret algorithm: they wire 10,000 to 100,000+ GPUs together to read 20+ tokens per knob across trillions of web, code and book tokens.

Mac Hedwig1× MacBook Air M4 · $0
13.8M · ~125M tok
Cloud Hedwig1× NVIDIA A40 · $0.65
97.6M · ~164M tok
GPT-2 Small (2019)32× Cloud TPU v3
124M · ~10B tok
Flagship Hedwig1× RTX 4090 · $1.70
286.7M · ~205M tok
GPT-3 (2020)~1k-10k× V100 GPUs
175B · ~300B tok · ~$4.6M
Llama 3 (2024)16,384× NVIDIA H100
405B · 15.6T tok · ~$50M+
GPT-4/5 · Claude Opus10k-100k+ H100/B200
1T+ (est.) · 10T-20T+ tok
ModelKnobsGPUs UsedTokens ReadTokens / KnobCost (est.)
Mac Hedwig13.8M1× MacBook Air M4~125M~9.0$0.00
Cloud Hedwig97.6M1× NVIDIA A40~164M~1.7$0.65
Flagship Hedwig286.7M1× NVIDIA RTX 4090~205M~0.7$1.70
GPT-2 Small (2019)124M32× Cloud TPU v3~10B~80~$1k-$5k
GPT-3 (2020)175B~1k-10k× V100~300B~1.7~$4.6M
Llama 3 (2024)405B16,384× H10015.6T~38.5~$50M+
GPT-4/5 · Claude Opus1T+ (est.)10k-100k+ H100/B20010T-20T+~20+ & RL$50M-$500M+

What Hedwig read

TinyStoriesFineWeb-Edu10 Gutenberg novels30k CoEdIT grammar edits

All public domain or openly licensed. No outputs from closed commercial models.

What frontier models add

15+ trillion web & code tokens20-40+ tokens per knobData/Tensor/Pipeline parallelismDeep RL & reasoning tuning
VI

The receipts & what I learnt

The whole bill: $2.45.

Every cloud run was rehearsed on the Mac first with a 2-step dry run, so no rented minute went to finding bugs.

Hedwig Labs

RECEIPT · 15-20 NOV 2026


Mac Hedwig, all 3 stages + labMacBook Air M4 · days$0.00
Smoke test (26× faster)RTX A5000 · $0.28/hr · ~4 min$0.10
Cloud Hedwig 97.6MA40 · $0.59/hr · 66 min$0.65
Flagship Hedwig 286.7MRTX 4090 · $0.89/hr · 115 min$1.70
Hosting the demoHugging Face static Space$0.00

TOTAL$2.45

THANK YOU FOR TRAINING SMALL

The first rented GPU running bash cloud_train.sh
The first rented GPU, running one command that prepares the data, trains all three stages and squeezes the brains.

Why so cheap

Small models, cheap community GPUs, one rehearsed script, no wasted runs. The flagship read about 0.7 pieces per knob, far under the Chinchilla rule of about 20, to stay on budget. Its weaker grammar edits are the exact cost of that trade.

✦

On screen

Then it wrote a movie.

The launch film. 60 seconds, made in code with Remotion.
Pip and the Lost Key. 84 seconds. The story is word for word from the 286.7M brain.
✦

Said plainly & what I learnt

Honest limits. Five real lessons.

Names wander on tiny models

A frog grabs Lily's umbrella, then thanks Lily for helping it. The 14M brain renamed an owl three times in one story.

Under-read flagship trails on grammar

286.7M read ~0.7 tokens per knob vs 97.6M's ~1.7 tokens per knob, so 97.6M fixed "I goes" to "I go" while 286.7M missed it.

✦

Open source

Everything is public.

Once upon a time,
there was a little owl.

Six days, three brains, $2.45, and a model that now lives in a browser tab. Built to understand how GPT and Claude work, one measured step at a time.