Balian.dev
St. Paul, MN · remote Book a call

Blog

How does AI work, and how does it learn?

Most people use AI every day with no picture of what is going on inside it. I built a page that shows the machinery with a small real model running in your browser, and this post walks through the same ideas in plain words, including why it sometimes makes things up.

The business owners I talk to use AI every week and have no picture at all of what happens between typing a question and getting an answer back. That is not their fault. The explanations out there either stop at “it learned from the internet” or jump straight to diagrams of matrices, and nothing in between shows the thing working. So I built a page that does, at how-ai-works.balian.dev, where every number comes from a real model or a published paper. At the end a small real language model, GPT-2, runs on your own computer while you type to it. Below is the short version, with a link in each section to the part of the page that shows it.

A reply being written one token at a time, then the playground at the end, where GPT-2's odds for the next token light up the ring.

What is a token in AI?

An AI chatbot does not write its answer all at once. It writes one small piece, then looks at everything written so far and writes the next, and it keeps doing that until it decides it is done. Those pieces are called tokens. A token is often a whole word, sometimes part of one, and sometimes just a comma, and before a model can read anything the text is cut into tokens and each one is swapped for a number. GPT-2 knows 50,257 of them. “Strawberry” is a single token, number 41236, and these models have trouble counting the letters in a word because they only ever see the number, never the letters inside it. Tokens are also what AI companies count when they bill you, and in GPT-2 an English token works out to about three quarters of a word.

At every step the model gives every token it knows a probability of coming next, and then one is picked. The one word at a time stage on the page shows this happening with GPT-2’s real odds, and the sampling stage slows one step right down. The pick is a roll of the dice weighted by those odds, and a setting called temperature decides how adventurous the roll is: turn it down and the favorite almost always wins, turn it up and unlikely tokens get more of a chance. That roll is why you can ask the same question twice and get two different answers, since the question was the same both times and the roll came out different.

How does AI learn?

Inside, a model is built out of very simple parts. The page starts with one of them deciding whether to go to the beach: three numbers come in for how sunny it is, whether it is the weekend and how much homework is due, each is multiplied by how much it counts, the results are added up, and the total is squashed into an answer between no and yes. That is a neuron, and each of those “how much it counts” numbers is a weight, also called a parameter. Change one weight, say make homework count twice as much against, and you can watch the answer flip on the neuron stage with the same inputs.

Nobody sets those weights by hand. A network starts with them at random, which makes it useless, and training means showing it examples, measuring how wrong its guesses are, and nudging every weight a little in the direction that makes it less wrong, then doing it again, an enormous number of times. On the page a small network with 354 weights learns to tell two spirals of dots apart while you watch, and you can see its error roll downhill like a ball on a landscape. Large models learn exactly the same way, and the only difference is size. GPT-2 has about 124 million weights, and the largest models anyone can download today have more than a trillion. Every one of those numbers was set the same way, and together they are everything the model knows. An AI model is a very large pile of learned numbers plus the arithmetic that runs text through them.

The cost of all that nudging is where the money goes. The rule of thumb is about six operations for every parameter for every token read, and Meta’s Llama 3 405B read 15.6 trillion tokens. Meta reports 30.84 million hours of chip time on 16,384 of NVIDIA’s H100 chips to train it, and the training stage works out how the sum gets there.

How a model knows what a word means

The first thing a model does with a token is swap its number for a long list of numbers, 768 of them in GPT-2, and those numbers act like a position in a space where related words end up near each other. Nobody wrote them. They came out of training, because words used in similar ways got similar numbers, so places gather in one region, verbs in another and Python code in another. Directions in that space carry meaning too. Take the step from “man” to “king” and apply it to “woman”, and the closest word to where you land is “queen”. It is not always that neat, and the page says so: Paris minus France plus Italy lands closest to Milan, with Rome second, because the numbers record how words are used and Milan is used a lot like Paris.

Then the words have to look at each other, because a word on its own is ambiguous. In “The trophy didn’t fit in the suitcase because it was too big”, you know “it” is the trophy. The model works this out with something called attention, where each token looks back at the ones before it and decides how much each one matters to it. On the attention stage you can see one part of GPT-2 pull “it” toward “trophy” with real weights from the model. A model is that step, attention followed by a pass through a big grid of weights, repeated in a stack of blocks: 12 in GPT-2, 126 in Llama 3 405B. At the top of the stack the numbers point at the next token, and that is where the odds in the first section come from.

How does a chatbot learn to answer questions?

A model that has only been trained on internet text does one thing, which is continue text. Ask a small open model fresh from that stage “What is the capital of France?” and it answers Paris and then keeps going with another question and another capital, because online a line like that usually starts a quiz page. To make it answer like an assistant, it is trained further on example conversations, a question followed by a good answer, and then on pairs of replies where a judge picked the better one. When the judges are people that is called reinforcement learning from human feedback. After that the same model answers in seven tokens and stops, and on the assistant stage you can see that question asked at each point in its training. Newer models are also trained to work through a problem in writing before they answer, which is what “reasoning” means when a product advertises it.

What is a context window?

A model has no memory between conversations. Everything it can see of the current one, your messages, its replies, and any files or search results, has to fit in a fixed stretch of recent tokens called the context window, and when the conversation ends those tokens are gone and nothing in the model has changed. On Anthropic’s current Claude models the window holds a million tokens, roughly 1,850 printed pages. If an app seems to remember you, it saved notes somewhere and pasted them back in as text. A model also learned from text written up to a certain date, its knowledge cutoff, and knows nothing after that unless it is told or given a search tool.

Why does AI make things up?

The limit that matters most is that it picks the tokens that sound likely, whether or not they are true. Ask GPT-2 for the capital of Australia and its favorite answer is Sydney at 22 percent. Canberra, the right answer, is its fifth choice at 4.4 percent, and the wrong answer comes out just as fluent as a right one would. That is what people mean by a hallucination. Bigger and better-trained models do it far less, but not never, so anything that matters still needs checking by someone who knows the subject. The limits stage shows GPT-2 getting it wrong, and the stage before it covers how much is still not understood about what happens inside these models.

What this means for a business

Once you see that a language model is predicting likely text from everything in front of it, most of what it is good and bad at follows. It is very good at drafting, summarizing, rewording and pulling answers out of documents you hand it, and it is good at code, partly because code can be run and checked. It is bad at being the last word on a fact nobody verified, and it knows nothing about your company except what you put in its window. That is why the useful business setups give it your own material to work from, like the search over a company’s documents I described in the big data post, and why a person who knows the subject reads what comes out before it reaches a customer.

The page itself was built by one engineer with AI in a fraction of the time it used to take, and the AI-assisted engineering page says how I work and what I check. The AI integration page covers putting it to work inside a company, connected to the systems you already run.

If something about AI still feels like a black box, try the playground at the end of how-ai-works.balian.dev and type to the model yourself. Nothing you type leaves your browser. The rest of what I have built is on the demos page, and if you are wondering where AI fits in your own business, tell me what you have in mind and I will tell you whether it is a good use of it.

Want a second opinion on what you are paying for?