Article complete

In Just 243 Lines of Python Code, Andrej Karpathy Recreates GPT From Scratch

Andrej Karpathy stripped down the LLM architecture and loss function to basic mathematical operations.

A man with a beard smiles in front of a digitally abstract background with circular shapes and a tech-inspired design.

Key takeaways

  • Andrej Karpathy created microGPT, a minimal GPT using only 243 lines of Python code.
  • The project simplifies LLM architecture to basic mathematical operations without external libraries.
  • Karpathy's code includes essential components like tokenization, embeddings, and multi-head self-attention.
  • Users appreciate the accessibility of understanding LLMs through this condensed code.
  • The initiative encourages transparency in AI, moving away from treating models as black boxes.

Takeaways drafted with AI from the article text.

Watch the video· 4:22

Subscribe to AIM Network
Andrej Karpathy Shrinks GPT to 243 LinesWatch on AIM Network

The project, called microGPT, shows how a GPT-style language model can be trained and used for inference using only 243 lines of pure, dependency-free Python code—without PyTorch, TensorFlow, NumPy, or any external machine learning frameworks.

“This is the full algorithmic content of what is needed. Everything else is just for efficiency. I cannot simplify this any further,” said Karpathy in a post on X. 

He shared the project on GitHub and included an HTML file containing the code for a single web page. 

Karpathy explained that the full LLM architecture and loss function are stripped down to the most basic mathematical operations—such as addition, multiplication, exponentiation, logarithms, and exponentials—and a tiny scalar-valued autograd engine (“micrograd”) is used to compute gradients, with Adam handling optimisation.

The code that Karpathy shared also includes a simple character-level tokeniser, positional and token embeddings, multi-head self-attention with residual connections, RMS (root mean square) normalisation in place of layer normalisation, and an autoregressive sampling loop that generates text token by token after training.

Users online reacted with widespread fascination and admiration, given that the project compresses an entire working GPT into just a few hundred lines of plain Python code. 

Anand Iyer, Venture Partner at Lightspeed Ventures, wrote on X, “You can read it in one sitting and actually understand how LLMs work instead of treating them as black boxes.”

“When someone (Karpathy) who led Tesla's Autopilot and helped found OpenAI says this is as simple as it gets, it means the field is maturing from research mystery to engineering clarity,” added Iyer, calling it the K&R of language models—a reference to the classic book The C Programming Language by Brian Kernighan and Dennis Ritchie, which defined the canonical, minimal expression of C language.

He authored and taught Stanford’s influential CS231n deep learning course, helped popularise the idea of “Software 2.0”, “Vibe Coding’and created hands-on projects and tutorials, including building neural network components and GPT-like models from scratch. 





Published

Spotted an error? Tell our editors. Read our editorial standards and corrections.

Selling to AI teams? Put your brand in front of the 5M+ people who read AIM every month. Plan a campaign

More in this story

Andrej Karpathy · 3 stories
  1. Machines Are Getting Better at Coding, Should You Be Worried?
  2. Now Everyone’s a Developer, Thanks to Microsoft
  3. You are here: In Just 243 Lines of Python Code, Andrej Karpathy Recreates GPT From Scratch
About the author

Principal Correspondent

All stories
Belamy · Daily

The day’s AI news, before your first meeting.