0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Why does AI feel slow?
A customer sends a message. Nearly two seconds pass before anything appears. Then the reply streams out, and finishes after about nine seconds. To the customer, the assistant feels sluggish. But "slow" isn't one problem. That time is made of slices: the network, waiting in a queue, searching the policies, reranking, reading the prompt, writing the reply token by token, and a final safety check. Each slice has a different cause, and a different fix. In this module, we make Brightcart's assistant fast and affordable.
Today: how to measure speed properly, why generating text is limited by memory more than maths, and what the hardware is actually doing. Season Five, Module Twenty-One: inference optimisation. First stop: latency metrics and accelerators.
1:11Prefill and decode: two phases of generation
Generating a reply happens in two phases. First, prefill: the model reads the entire prompt, about three thousand four hundred tokens of instructions, policies and conversation, all at once, in parallel, and stores the keys and values in its K-V cache. This phase determines how long the customer waits before the first word appears. Then, decode: the model produces the reply one token at a time. Each new token needs another pass through the network, reading the cache and all of the model's weights. This phase determines how fast the text streams out.
1:50TTFT, TPOT and latency percentiles
So we measure two main numbers. Time to first token: in our example, zero point four seconds. Time per output token: twenty-five milliseconds, about forty tokens a second. A two-hundred-token reply then takes zero point four, plus twenty-five milliseconds for each of the remaining hundred and ninety-nine tokens: about five point four seconds in total. Always look at the distribution, not just the average. The ninety-fifth percentile, the slowest one in twenty requests, is what frustrated customers remember. And measure end to end: the network, queues, retrieval and tool calls all add to what the customer actually experiences.
2:36Throughput vs goodput
Speed for one user isn't the whole story. Throughput measures the total work a server does: tokens or requests per second, across everyone at once. Serving more conversations simultaneously usually raises throughput, but each customer's reply may stream a little slower. That's why many teams track goodput: the share of requests that meet a service target, say the first token within one second and the whole reply within eight. If eighty-seven requests out of a hundred meet both, the goodput is eighty-seven percent. A system can have impressive throughput and still poor goodput, meaning lots of work done, and lots of customers kept waiting.
3:22The memory wall: why decoding is slow
Why is decoding the slow part? Hardware can be limited in two ways: by how fast it can do arithmetic, which is called compute-bound, or by how fast it can move data from memory to where the arithmetic happens, which is called memory-bound. Prefill processes thousands of tokens at once, so it does a lot of arithmetic for every weight it reads: mostly compute-bound. But during decoding for a single user, producing each token means reading every weight in the model from memory, to do very little arithmetic. That's memory-bound.
Here's a back-of-the-envelope bound. Suppose the memory can deliver two trillion bytes a second. With fourteen gigabytes of B-F sixteen weights to read for every token, the most you could ever get is about a hundred and forty-three tokens a second. Shrink the weights to four bits, three and a half gigabytes, and the ceiling rises to about five hundred and seventy. That's why quantisation from episode seventy speeds up generation, and why serving many users from a single read of the weights, called batching, is so powerful.
Both are coming up.
4:36Utilisation: MFU and memory bandwidth
To know which kind of fix will help, engineers measure utilisation. Model FLOP utilisation: how much of the hardware's peak arithmetic you're actually using. Memory bandwidth utilisation: how much of its peak memory speed you're using. Prefill tends to use the arithmetic units heavily; decoding tends to saturate memory bandwidth. If you're memory-bound, shrink or batch. If you're compute-bound, use more efficient code and lower-precision arithmetic.
5:08What to look for in an AI accelerator
What should you look for in AI hardware? Raw arithmetic speed, usually much higher at lower precisions like eight-bit. Memory capacity: can the model, plus the K-V cache for all your users, fit at all? Memory bandwidth, which, as we saw, sets the ceiling on decoding speed. Fast connections between chips, for models too large for one. And of course power, price and availability. Graphics processors and custom AI chips do most of this work, while small, quantised models can even run on ordinary computers and phones. For a chat assistant with long prompts and many users, memory capacity and bandwidth often matter more than headline arithmetic speed.
A quick note: names, prices and rankings in AI change fast. Everything on screen was accurate when this video was made, but some of it may have changed by the time you're watching.
6:07Where Brightcart's time went
So, where did Brightcart's nine seconds go? Some was queueing during busy hours: a capacity and batching problem. Some was retrieval and reranking: fixable with caching and a lighter reranker. Some was prefill over three thousand four hundred prompt tokens, much of it identical every time, which prompt caching can skip. Some was decoding three hundred tokens, which quantisation, a technique called speculative decoding, and simply shorter replies can all speed up. And some was the final safety check, which can run in parallel.
The next two episodes tackle these one by one.
6:47Try it yourself: how long does prefill take?
How long does prefill take for Brightcart's three-thousand-four-hundred-token prompt on a seven-billion-parameter model? The work is about two operations per parameter per token. Assume the GPU sustains four hundred trillion operations a second. Pause.
About forty-eight trillion operations, so roughly a tenth of a second. Prefill is a lot of work, but it's done in parallel, so it's fast. If the first word takes two seconds to appear, the culprit is usually elsewhere: waiting in a queue, retrieval, reranking. Measure each stage before blaming the model.
7:33Try it yourself: why decoding wastes the GPU
Here's why decoding one customer at a time wastes a GPU. Each new token needs about fourteen billion operations, but also requires reading all fourteen gigabytes of weights: roughly one operation per byte read. Our illustrative GPU could do two hundred operations for every byte it reads. So how busy are its maths units? Pause.
About half of one percent. The chip spends almost all its time waiting for weights to arrive from memory. That's the memory wall, and it's why the next two episodes focus on reading fewer bytes per token, and on serving many customers with each read.
8:19Throughput isn't goodput
Throughput counts how many requests a server handles; goodput counts how many it handles well enough. If a server processes a hundred requests a second, but only eighty-seven meet Brightcart's targets, a first word within a second and a full reply within eight, its goodput is eighty-seven. Squeezing in more requests can raise throughput while goodput falls, because everyone starts waiting longer. Customers only experience goodput.
8:52Latency percentiles for every metric
Every latency metric deserves percentiles. The median time to the first token describes most customers; the ninety-ninth percentile describes the unluckiest one in a hundred. In one illustrative incident, the median stays flat while the ninety-ninth percentile jumps: a new feature is sending very long prompts that queue behind one another. An average would have hidden it. The median tells you about most customers; the tail tells you about the angry ones.
9:24Pause & Predict: which latency number matters?
Which latency number matters most for live chat, for overnight catalogue tagging, and for a voice assistant? Pause. For live chat: the time to the first word, and a smooth stream after it, because the customer is watching. For overnight tagging: total throughput and cost, because nobody is waiting. For a voice assistant: the time to first token above everything, because silence on a phone call feels broken. Optimise the number your users actually feel.
10:00Choosing hardware by the bottleneck
How do you choose hardware? Start from the bottleneck. Memory capacity decides which models and how much KV cache fit at all. Memory bandwidth sets the ceiling on how fast tokens can be decoded. Raw compute decides how fast long prompts are processed and how well large batches run. And the connections between chips matter when one model is split across several of them. For a chat assistant, capacity and bandwidth usually matter more than peak compute figures.
10:34How to benchmark LLM inference honestly
Performance numbers are easy to fake by accident. Benchmark with realistic prompts, using the real distribution of Brightcart's prompt lengths, not ten-token toy questions. Simulate many users at once, arriving at random. Warm the server up before measuring. Report percentiles, goodput and cost per thousand requests, never a single best-case figure. And re-run the benchmark after any change to the model, the prompt or the serving software.
11:05Cost per thousand requests
To compare serving options, convert everything into cost per thousand requests. For self-hosting, divide the hourly hardware cost by the number of requests served per hour, while still meeting the latency targets, and multiply by a thousand. For an API, it's the token counts times the prices. As traffic grows and batching improves, the self-hosted cost per request falls, while the API's stays flat. Where the lines cross depends on your own traffic, which is why you measure it.
11:41Pause & Predict: how fast is fast enough?
People read comfortably at about two hundred and fifty words a minute: roughly five and a half tokens a second. Brightcart's assistant streams at forty tokens a second, about thirty words a second. Would doubling that speed be worth a lot of money? Pause.
For chat, usually not. The text already appears several times faster than anyone can read it. What customers feel is the wait before the first word, and the total time for long answers. Once streaming outpaces reading, money is usually better spent elsewhere.
12:23Traffic has a daily shape
Traffic has a daily shape. Brightcart is quiet overnight, has a big peak at lunchtime and another in the evening. Size the hardware for the peak, and it sits mostly idle at four in the morning; size it for the average, and the lunchtime rush overwhelms it. So the team scales servers up and down with demand, schedules batch jobs like catalogue tagging for the quiet hours, and puts live chat at the front of the queue, always.
12:54Myth vs Fact
Myth: faster AI just needs a chip with more arithmetic power. Fact: generating text is usually limited by memory bandwidth, and what customers feel also includes queues, retrieval and tools. Measure each slice first, then fix the slowest one.
13:12Recap
Let's recap. Generation has two phases: prefill reads the prompt in parallel and sets the time to first token; decode writes one token at a time and sets the streaming speed. Measure end to end, including the slow tail. Balance throughput against per-user speed, and track goodput against a clear target. Decoding is usually limited by memory bandwidth, which is why smaller weights and batching help. And choose hardware by memory capacity and bandwidth, not just arithmetic speed. One sentence to remember: prefill thinks in parallel; decode drips one token at a time, as fast as memory allows.
13:54Next: faster models
Mission complete. Dev now knows exactly where the assistant's time goes, tracks the slow tail and goodput, and has a fix in mind for every slice. Next, we make the model itself faster: quantisation and distillation in practice, a clever trick where a small model drafts and a big one checks, and the attention optimisations that keep long conversations quick. See you on the map.