MacforAI

Which Mac runs your local LLM: memory, speed and Apple price

← All articles

How AI Works: Tokens, Transformers and Attention

Published · Updated · 12 min read

Watch the video on YouTube

How AI works comes down to one repeated move: a language model looks at the text so far and guesses the next small piece of text, called a token, then adds it and guesses again. The "knowledge" it uses is a huge list of numbers called weights, and since 2017 an idea called attention lets it relate every word to every other word. Those weights have to sit in your computer's memory and be read for every token, which is exactly why memory decides which Mac you need.

Key takeaways

  • A neural network is a large collection of numbers (weights) that training tunes until the network is good at one task: guessing what comes next.
  • A language model writes by chain: predict one token, append it, predict again. Nothing more mysterious happens.
  • A token is a chunk of a word. In English one token is about three-quarters of a word on average; other languages often need more tokens for the same text.
  • Attention, introduced with the transformer in 2017, lets the model weigh how much each word matters for every other word, even when they are far apart.
  • For a Mac buyer: weights live in memory, and every new token reads the active weights, so memory size decides what fits and memory bandwidth decides how fast it runs.

What a neural network and its weights are

A neural network is a program whose behaviour is not written line by line by a programmer. Instead it is a very large pile of numbers, called weights, combined by simple arithmetic: multiply, add, repeat. Feed in some numbers at one end, and numbers come out at the other. Which numbers come out depends entirely on the weights.

Training is the process that sets them. The network starts with essentially random weights and produces nonsense. It is then shown an enormous amount of examples, and after each guess the weights are nudged slightly so that the same guess would have been a little less wrong. After many rounds, the network gets good at one specific task. For a chatbot, that task is guessing what comes next in a piece of text.

You will also see the word parameter. For the purposes of buying a computer it means the same thing as a weight: one stored number. When a model is called "8B", it has roughly 8 billion of them. That count is the single most useful number for estimating how much memory a model needs.

How a language model writes: a chain of guesses

A language model is a neural network trained for exactly one job: given some text, predict what comes next. It does not retrieve a stored answer. It produces a ranked list of likely next pieces, picks one (usually a likely one, with a little controlled randomness), and appends it to the text.

Then it does the same thing again with the longer text. And again. Every answer you have ever received from a chatbot is a chain of such guesses, one piece at a time. This is why you see replies appear word by word, and why the speed of a local model is measured in pieces per second.

The result can look like understanding because predicting text well over a huge range of subjects forces the network to capture grammar, facts and patterns of reasoning. But the mechanism stays simple, and it also explains a familiar failure: a model can produce a fluent, confident sentence that is wrong, because "likely next piece" is not the same as "verified fact".

What tokens are, and how many make a word

The pieces the model predicts are called tokens. A token is a chunk of text, usually a word or part of a word. Before the model sees your text, a separate step cuts it into tokens from a fixed vocabulary, and each token gets a number.

A short, common word is often a single token. A long or rare word may be split into two or three. Punctuation, spaces and numbers also take tokens. As a rule of thumb in English, one token is about three-quarters of a word, so 1,000 tokens is roughly 750 words. That is an average, not a guarantee.

Two practical notes follow. First, token counts depend on the language. Vocabularies built mostly from English text cut English efficiently, while Russian, Spanish, French or other languages often need more tokens for the same meaning, so the same page costs more tokens. Second, code and numbers tend to be token-hungry too. Whenever a tool quotes a context limit or a speed in "tokens", remember that a token is smaller than a word.

Before 2017: why models forgot the start of a sentence

Earlier language models, built on designs called recurrent networks, read text strictly in order, one word at a time. They kept a small running summary of everything read so far and updated it with each new word. By the end of a long sentence, details from the beginning had been squeezed and overwritten many times, so the model effectively forgot the start.

Reading in order had a second cost: it was slow to train, because each step had to wait for the previous one. You cannot easily spread that work across thousands of chips at once. Both problems, short memory and slow training, were what the next idea solved.

The 2017 paper and the transformer

In 2017 researchers at Google published a paper titled "Attention Is All You Need". It introduced the transformer, an architecture that does not read in order. Instead, it looks at all the words of the input at the same time and learns which words matter for which other words. Because it processes everything in parallel, it also trains far faster on modern chips, which is what made today's very large models practical.

Almost every large language model you will meet in a Mac calculator, from small 3B models to 70B giants, is a transformer or a close variant of one. The follow-up article what a transformer is, plus BERT, model size and quantization goes one level deeper into the architecture.

Attention in one sentence

Take the sentence: "The animal did not cross the street because it was too tired."

What does "it" refer to? A person answers instantly: the animal, because a street cannot be tired. For a program, this is genuinely hard, since "it" could grammatically point to either noun. Attention is the mechanism that solves it. For each word, the model scores how relevant every other word is, and mixes in information from the highest-scoring ones. When it processes "it", the score for "animal" comes out high, so the meaning of "animal" flows into "it". Change the last word to "wide" ("...because it was too wide") and the scores shift toward "street".

The key point is that this works regardless of distance. A word at the end of a long paragraph can attend directly to a word at the start, instead of relying on a summary that has been rewritten a hundred times. That is the fix for the old forgetting problem.

Why this decides which Mac you need

Now the bridge to hardware. Everything above has three consequences for a computer.

  1. The weights must be in memory. A model with 8 billion weights stored at 16 bits each takes about 16 GB. Compressed to roughly 4 bits per weight, it takes about 4.85 GB. If the model does not fit, it cannot run well. That is a question of RAM, covered in how much RAM a local LLM needs on a Mac.
  2. Every token reads the active weights. To guess one token, the chip streams the active weights through its arithmetic units. So speed is limited mostly by how many bytes per second memory can deliver. That is memory bandwidth, explained in memory bandwidth and tokens per second on a Mac.
  3. Attention keeps a working memory. To avoid recomputing the past, the model stores some intermediate values for every earlier token, called the KV cache. It grows with the length of the conversation and takes extra memory.

So memory size decides whether a model fits, and memory bandwidth decides how many tokens per second you get. That is why the calculator on this site asks about both, and why two Macs with the same chip family can behave very differently.

A worked example: an 8B model on three chips

Take Llama 3.1 8B at 4-bit (the Q4_K_M format, which uses about 4.85 bits per weight, as in our model data).

  • Active weights: 8 billion x 4.85 bits / 8 = about 4.85 GB, or about 4.5 GiB. That is the "about 4.5 to 5 GB" read for every token.
  • Theoretical ceiling = memory bandwidth / bytes per token.
  • Our engine multiplies bandwidth by an efficiency factor (0.68 up to 200 GB/s, 0.64 up to 320 GB/s, 0.60 up to 650 GB/s) because real software never reaches the ceiling.
Chip (our data)BandwidthCeiling: bandwidth / 4.85 GBEstimate with efficiency factor
M5 (MacBook Air)153 GB/sabout 32 tokens/sabout 23 tokens/s
M5 Pro307 GB/sabout 63 tokens/sabout 44 tokens/s
M5 Max (40-core GPU)614 GB/sabout 127 tokens/sabout 82 tokens/s

The right-hand column divides by 4.52 GiB, ignores the KV cache, thermal throttling and runtime differences, so treat it as an estimate in the same sense as the calculator's own ranges. The pattern is the useful part: doubling bandwidth roughly doubles speed for the same model.

Now change the model. A 70B model at the same 4-bit format weighs about 42 GB, roughly 8.75 times more than the 8B model. The same chip reads 8.75 times more data per token, so it generates roughly 8.75 times fewer tokens per second, and it needs a Mac with enough RAM to hold 42 GB first. You can try any combination yourself in the Mac calculator for local LLMs or in the LLM speed simulator article.

Glossary: the six terms to remember

TermPlain meaningWhy it matters for a Mac
WeightOne stored number inside the network, tuned during trainingAll weights must sit in memory
ParameterAnother word for a weight; "8B" means 8 billionSets the model's file size
TokenA chunk of text, about three-quarters of an English wordSpeed is measured in tokens per second
ContextThe amount of text, in tokens, the model can consider at onceLonger context needs more memory
AttentionScoring how much each token matters for each other tokenGives long-range understanding
KV cacheStored intermediate values for earlier tokens, so they are not recomputedGrows with context, adds memory and reading

How to read the rest of the series

This article is Part 1 of a short series. A sensible order:

  1. Read this article for the vocabulary: weights, tokens, attention.
  2. Continue with Part 2: what a transformer is, BERT, model size and quantization to learn why a model's size in gigabytes is what you actually plan around.
  3. Read how much RAM you need to turn model size into a memory target.
  4. Read memory bandwidth and tokens per second to understand speed.
  5. Open the speed simulator article and then the calculator to test your own model and Mac.
  6. Finish with the buying guide for local LLM Macs to pick a configuration.

FAQ

What is a token in AI?

A token is the small chunk of text a language model reads and writes, usually a word or part of a word. In English, one token is about three-quarters of a word on average. Different languages and tools split text differently, so counts vary.

What does "attention" mean in a language model?

Attention is the step where the model scores how relevant every other word is to the word it is processing, then blends in information from the most relevant ones. In "the animal did not cross the street because it was too tired", it lets "it" connect to "animal". It works even when the related words are far apart.

What was "Attention Is All You Need"?

It is a 2017 paper by Google researchers that introduced the transformer. The transformer reads all words at once instead of in order, which fixed the forgetting problem of older models and made training on many chips in parallel practical. Most large language models today are built on it.

Does an AI model actually understand text?

A language model is trained to predict the next token, and doing that well requires capturing grammar, facts and patterns. Whether that counts as understanding is debated, so this article makes no claim either way. In practice it can be fluent and still wrong, so important answers are worth checking.

Why does AI need so much memory on a Mac?

The model's weights must all be held in memory, and the chip reads the active ones for every token it produces. A 4-bit 8B model needs about 4.85 GB for weights alone, and a 70B model about 42 GB. Memory size decides whether the model fits, and memory bandwidth decides how fast it runs.

Are token counts the same in every language?

No. Token vocabularies are usually built mostly from English text, so other languages often need more tokens per word. The same paragraph can cost more tokens in Russian or French than in English, which affects both speed and how much fits in the context.