How Andrej Karpathy Built a Working Transformer in 243 Lines of Code

AI-generated summary of reporting by Analytics Vidhya Gen AI Blog. Read the original for the full story.

Why it matters

Very little changes for practitioners, this is an educational teaching artifact that strips transformers down to readable pure Python, useful for learning, not for production.

Read this as

AI researcher Andrej Karpathy released microGPT, a working version of GPT (the technology behind chatbots like ChatGPT) written in just 243 lines of Python code with no extra libraries. It shows every mathematical step of how the model learns and generates text, instead of hiding them inside big frameworks like PyTorch. It trains a model that can produce believable names. The trade-off is speed: it is purely educational and runs extremely slowly.

Karpathy's microGPT is a teaching tool, not a product: a minimal GPT-2-style model in 243 dependency-free Python lines. Teams gain no production capability from it, and training that takes seconds in PyTorch reportedly takes about three hours here on a CPU. Its value is onboarding and demystification for engineers new to how large language models actually work. No pricing, licensing or availability constraints are mentioned.

microGPT implements a simplified GPT-2 architecture in 243 lines of pure, dependency-free Python: a hand-rolled autograd engine using a scalar Value class, character-level token and positional embeddings, multi-head self-attention with 1/sqrt(head_dim) scaling, feed-forward blocks using ReLU² instead of GeLU, no bias terms, and a from-scratch Adam optimizer with bias correction. Training runs 500 steps, predicting next characters; inference uses temperature sampling. It can load GPT-2 state dicts. Limitations: pure-Python scalar math, no GPU or NumPy, roughly three hours training where PyTorch takes seconds.

Read the original

This summary exists to save you time, not to replace the source.

Key terms

microGPT GPT-2 PyTorch GitHub NumPy TensorFlow Adam optimizer

Was this summary useful?

One click, anonymous. It tells us which summaries to re-run.

Related reading

datasette-charts 0.5a0

Datasette Charts 0.5a0 adds calendar heatmaps, box plots and active-over-time charts, enabling richer data visualisation directly in the browser without extra libraries.