How LLMs Work (for Developers)

Tokens, probabilities, temperature, training, embeddings, context windows, prompts, and hallucinations — how large language models really work, learned by building a tiny one yourself.

Everyone is using AI assistants, and almost everyone has the same questions. Is it really thinking? Why does it sometimes make things up with total confidence? Why does the same question give a different answer each time? Why does it forget what I said ten minutes ago? Why does it cost money per word? If you build software in the AI era, you will be asked these questions by your users, your teammates, and your students, so it helps to know the real answers instead of guessing.

This series explains how a large language model (LLM) works, in plain language and with code instead of math. You will see that the core idea is surprisingly small: a model reads text, predicts the next piece, adds it, and repeats. Everything else (the clever answers, the long essays, the code it writes) grows out of doing that one thing at enormous scale. You will learn what tokens are and why they decide your bill, how a model turns scores into probabilities, what "temperature" really does, how a model learns from text, how words become numbers (embeddings), why a model has a limited memory (the context window), how prompts and roles steer it, and why hallucinations happen and what you can do about them.

You won't just read about it. Across the series you build llmlab, a small JavaScript toolkit that runs on your own computer: a toy tokenizer, a tiny language model that writes text, a sampler with temperature, a training loop that you can watch learn, a word-similarity search, a conversation memory manager, a prompt builder, and a grounded answer checker. Real LLMs are millions of times bigger, but they follow the same ideas, and a model you built yourself is the best way to understand one you did not.

You need to be comfortable with the basics of JavaScript — variables, functions, arrays, objects, loops, and import/export — from JavaScript & TypeScript Fundamentals, and with the terminal from Command Line Basics. The Probability Basics chapter of Math for Programmers is a helpful warm-up for chapters 3 and 4. You need Node.js (the LTS version) and a code editor such as Visual Studio Code. No API keys, no accounts, and nothing costs money. The next series, Build a Chatbot with an LLM API, is where you call a real hosted model.

Every chapter ends with a Quick Review: Common Questions section: short, honest answers to the questions beginners most often ask about that chapter's topic. Try answering each one yourself first, then check your answer. If you can explain it in your own words, you understood the chapter.

  1. 01What Is an LLM?A large language model is a next-word predictor run in a loop: see the whole idea in one small program, learn what an LLM is and is not, and set up the Node.js folder called llmlab that we build in every chapter.
  2. 02Tokens and TokenizersHow text is cut into tokens before a model ever sees it, how byte-pair encoding builds those pieces, why tokens decide your bill and your limits, and how to add a toy tokenizer to llmlab.
  3. 03Build a Tiny Language ModelBuild a working next-word model in about 40 lines of JavaScript: count which word follows which, turn counts into probabilities, add a seeded random number generator, generate text, and see why more context makes better text.
  4. 04Probabilities, Temperature and SamplingLearn how a model turns raw scores into probabilities with softmax, what temperature really does, how greedy, top-k and top-p sampling choose the next token, and why the same prompt can give different answers.
  5. 05How a Model Is TrainedWatch a tiny model learn: a table of numbers, a loss that measures how wrong it is, and gradient descent that nudges the numbers until the loss falls, plus pretraining, fine-tuning, preference tuning, and why training costs so much more than using a model.
  6. 06Embeddings and AttentionTurn words into lists of numbers where similar words sit close together, measure closeness with dot product and cosine similarity, then build a tiny attention demo that shows how a word blends in information from the words around it, the core idea inside the Transformer.
  7. 07Context Windows and MemoryWhy an LLM forgets everything between calls, how a chat 'remembers' by resending the whole conversation, what a context window is, why long chats cost more, and how to trim, summarize and cache history in a small llmlab module.
  8. 08Prompts, Roles and Structured OutputHow to build a messages array with system, user and assistant roles, steer a model with few-shot examples, get JSON you can trust by parsing, validating and retrying, and why prompt injection means untrusted text must never be treated as instructions.
  9. 09Why Models HallucinateWhy an LLM can sound confident and still be wrong: it predicts plausible text, not true text. See it in a toy model, then build a grounded answer checker and a small eval harness that says I don't know.
  10. 10Limits, Cost and Common QuestionsThe capstone: the whole LLM pipeline in one picture, a command-line report of tokens, cost and context use, an honest tour of what LLMs are good and bad at, and 18 straight answers for a talk to students and developers.