Building an LLM from Scratch in TypeScript: A Show HN Project Draws Attention

A developer built an LLM from scratch in TypeScript, showing the educational value and feasibility of large model development outside the Python ecosystem.
A developer built a large language model from scratch in TypeScript and posted it on Hacker News' Show HN section, relying on no Python bindings or external inference services. The article reviews the core components of building an LLM from scratch: tensor operations and automatic differentiation, the Decoder-only Transformer architecture (multi-head self-attention, positional encoding, layer normalization, residual connections), the training loop and Adam optimizer, and autoregressive inference with KV caching. The author notes that such projects are highly educational for understanding Transformer internals, especially for frontend developers accustomed to the TypeScript/JavaScript ecosystem. However, TypeScript's numerical computation performance is far inferior to Python + CUDA, and model scale typically stays at the million-parameter level, making it better suited for teaching and small-scale experiments. As WebGPU and ONNX Runtime Web mature, TypeScript's role in browser-side AI inference will become increasingly important.
In today's world of increasingly ubiquitous AI, most developers are used to calling APIs from companies like OpenAI and Anthropic to build their applications. But one developer chose a much more hardcore path — building a large language model from scratch in TypeScript — and posted the project on Hacker News' Show HN section.

Why Build an LLM in TypeScript?
When it comes to LLM development, Python is almost the default choice. From PyTorch to the Hugging Face ecosystem, Python has the most mature toolchain. However, TypeScript is becoming one of the most active languages at the AI application layer — the rise of frameworks like the Vercel AI SDK and LangChain.js has made it easy for frontend developers to integrate large model capabilities.
So what does it mean to write an LLM from scratch in TypeScript? It means relying on no Python bindings, calling no external inference services, and instead implementing tensor operations, forward propagation, backpropagation, and even the attention mechanism purely within the JavaScript runtime. For frontend engineers, this is an extremely educational exercise for understanding the inner workings of the Transformer architecture.
For developers who want to deeply understand the underlying principles of LLMs but are used to the TypeScript/JavaScript ecosystem, projects like this offer a low-barrier entry point. You don't need to switch to Python to implement a working language model with your own hands.
What Core Components Are Involved in Building an LLM from Scratch?
A complete from-scratch LLM implementation typically needs to cover the following key modules:
Tensor Operations and Automatic Differentiation
This is the foundation of every deep learning framework. Implementing tensors in TypeScript means handling the creation of multi-dimensional arrays, shape transformations, matrix multiplication, and element-wise operations. Automatic differentiation requires building a computation graph and implementing backpropagation via the chain rule — something Autograd handles in Python, but which must be built from scratch in TypeScript.
What is automatic differentiation? Automatic differentiation is the core mechanism of deep learning frameworks. It records each operation in the forward pass, builds a computation graph, and then applies the chain rule in reverse along that graph to automatically compute the gradient of the loss function with respect to each parameter. Unlike numerical differentiation (which approximates with finite differences) and symbolic differentiation (which directly manipulates mathematical expressions), automatic differentiation strikes a balance between precision and efficiency, and is the engineered implementation of the backpropagation algorithm. In Python, PyTorch's autograd module provides this capability; developers only need to define the forward computation, and the framework handles gradient computation automatically. Implementing automatic differentiation from scratch in TypeScript means defining a corresponding backward function for each tensor operation and maintaining a traceable computation graph — which is precisely the best entry point for understanding how deep learning frameworks work internally.
Attention is All You Need and the origins of the Transformer. This architecture comes from Google's 2017 paper of the same name. It completely abandoned the sequence-processing approaches of recurrent neural networks (RNNs) and convolutional neural networks (CNNs), modeling dependencies between any two positions in a sequence entirely through the attention mechanism. Its core innovation is "self-attention": each token can directly "see" all other tokens in the sequence and dynamically compute a weighted representation, thereby solving the RNN problems of long-range dependency decay and difficulty in parallelization. The GPT series adopted the Decoder-only variant of the Transformer — keeping only the decoder portion, paired with a causal mask to ensure each position can only attend to preceding tokens, thus enabling autoregressive text generation.
The Transformer Architecture
The core of the GPT series is the Decoder-only Transformer structure. This includes multi-head self-attention, positional encoding, layer normalization, feed-forward networks, and residual connections. Implementing these components in TypeScript requires precise handling of tensor dimension transformations — any shape mismatch will cause a runtime error.
A detailed look at Multi-Head Self-Attention. The basic idea of self-attention is: for each token in the sequence, use its own query vector to take the dot product with the key vectors of all tokens, producing attention weights, and then compute a weighted sum of those tokens' value vectors. "Multi-head" means repeating this process in parallel multiple times, each with different linear projection matrices, allowing the model to simultaneously capture semantic relationships from multiple different "representation subspaces" — for example, one head focusing on syntactic structure while another focuses on coreference. Finally, the outputs of each head are concatenated and passed through another linear transformation. This design greatly enhances the model's expressive power. When implementing this in TypeScript, the trickiest part is correctly managing tensor shapes: the input must be split into multiple heads, attention computed separately, and then merged back together. Any error in reshape or transpose will produce completely wrong output.
Why the KV Cache matters. In autoregressive text generation, each time the model generates a new token, it needs to recompute its attention with all previous tokens. If the Key and Value matrices for the entire sequence were recalculated from scratch every time, computation would grow quadratically with sequence length. The core idea of the KV cache is to cache the Key and Value vectors of all previous tokens, so that when generating a new token, only the current token's Query needs to be computed while the cached Keys and Values are reused. This reduces the computational complexity of each generation step from O(n²) to O(n), and is an inference optimization that is almost always used in real LLM deployments. Manually implementing the KV cache in TypeScript requires carefully maintaining cache state for each layer and each head, making it an excellent exercise for understanding how an inference engine operates.
Training Loop and Optimizers
Once the model is built, the training loop still needs to be implemented: forward propagation to compute the loss, backpropagation to compute gradients, and the optimizer to update parameters. Common optimizers like Adam or AdamW require maintaining momentum buffers, which also have to be implemented manually in TypeScript.
Inference and Text Generation
Finally, the model needs to be able to perform autoregressive inference — given a prompt, generating subsequent text token by token. This involves sampling strategies (such as top-k and temperature sampling) and optimization techniques like KV caching.
The role of positional encoding and layer normalization. Because the self-attention mechanism itself is insensitive to input order (shuffle the tokens and the attention computation stays the same), the Transformer must explicitly inject positional information — this is the job of positional encoding. The original paper used sine/cosine functions to generate fixed encodings, while the GPT series mostly uses learnable positional embeddings. Layer normalization normalizes the feature dimension of each sample, stabilizing the numerical distribution during training and accelerating convergence. Since GPT-2, the Pre-LN structure (normalization placed before the sublayer) has generally been adopted, which trains more stably than the original Post-LN structure. Residual connections allow gradients to bypass sublayers and flow back directly, and are key to training deep networks. These three may seem simple, but they are the important guarantees that allow the Transformer to stack dozens of layers without degrading.
The Value and Limitations of This Project
From an educational perspective, the value of projects like this cannot be underestimated. Once you've implemented the attention mechanism by hand in TypeScript, your understanding of "Attention is All You Need" will no longer remain at the paper level. This kind of hands-on experience also provides practical help in troubleshooting model behavior issues in real applications.
But we need to view things rationally: TypeScript's performance in numerical computation is far inferior to the combination of Python + CUDA. Projects like this are better suited as teaching tools and small-scale experimental platforms rather than production-grade model training solutions. The models it trains are limited in scale, with parameter counts usually staying in the millions, which is fundamentally different from industrial-grade models with billions of parameters.
However, for a Show HN project, it successfully demonstrated that LLMs are no longer Python's exclusive territory. As technologies like WebGPU and ONNX Runtime Web mature, running small language models in the browser is becoming a reality, and TypeScript's role in this field will become increasingly important.
The AI Learning Path for Frontend Developers
This project also reflects a trend: the barrier to AI development is being lowered, and language boundaries are blurring. For frontend developers, understanding LLM principles is no longer "someone else's business" — when you can build a working model in a language you're familiar with, AI is no longer a black box.
If you're interested in trying a similar direction, it's recommended to start with the smallest runnable version: first implement a single-layer one-dimensional tensor operations library, then gradually expand to a full Transformer. The intuition about matrix operations and gradient flow you accumulate along the way will be more profound than reading ten papers.
One-line Summary
A developer built an LLM from scratch in TypeScript, demonstrating the educational value and feasibility of building large models outside the Python ecosystem.
Paragraph Summary
A developer built a large language model from scratch in TypeScript and posted it on Hacker News' Show HN section, relying on no Python bindings or external inference services. The article reviews the core components of building an LLM from scratch: tensor operations and automatic differentiation, the Decoder-only Transformer architecture (multi-head self-attention, positional encoding, layer normalization, residual connections), the training loop and Adam optimizer, and autoregressive inference with KV caching. The author notes that projects like this are highly educational for understanding the inner workings of the Transformer, and are especially suitable for frontend developers accustomed to the TypeScript/JavaScript ecosystem. However, TypeScript's numerical computation performance is far inferior to Python + CUDA, and model scale typically stays at the million-parameter level, making it better suited for teaching and small-scale experiments. As WebGPU and ONNX Runtime Web mature, TypeScript's role in browser-side AI inference will become increasingly important.
Related articles

Free Unlimited Claude Code Solution: Agnes AI + CC Switch Setup Guide
No paid subscription needed — pair the free AI platform Agnes AI with the open-source tool CC Switch to provide a backend for Claude Code, enabling unlimited free calls to text, image, and video models. This guide covers the full setup, test results, and usage boundaries.

A Complete Zero-Cost Guide to Getting Started with Claude Code for Beginners in China
A beginner's guide to Claude Code for users in China: two installation methods (command line and IDE plugin), full configuration steps for replacing the official API with the free domestic LongCat model, and a hands-on demo of building a small tool with natural language, plus tips.

When the NSA Gets Involved: The AI Supply Chain Crisis Behind Open-Source CVEs and API Key Hijacking
Multiple open-source projects disclosed severe CVEs in one day, PHP flaws alarmed the FBI, and Railway and OpenCode were breached with API keys stolen. This article breaks down the vulnerability window, AI-accelerated exploitation, and the AI supply chain crisis behind the API key black market, with a developer defense checklist.