# Chapter 6: Inference

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/06-inferenz

*From the prompt to the first token and beyond: why reading is fast and writing is slow, what batching gets you, and which server suits which job.*

About 35 min to read · about 20 min hands-on exercise

**Start with the missions**

This chapter also comes as 3 short missions with a case, a decision and feedback, about 10 minutes each:

- [6.1 Why does it read fast and write slowly?](https://llm.yoga/en/einsatz-6-1.md)
- [6.2 Over a minute until the first word?](https://llm.yoga/en/einsatz-6-2.md)
- [6.3 One Mac mini with Ollama for 30 people?](https://llm.yoga/en/einsatz-6-3.md)

For agents, this page is split into 5 parts so that each part can be fetched in full. Read the parts the question needs.

## Parts

### Part 1: https://llm.yoga/en/06-inferenz-1.md

- **Getting started**: Picture a translator whose huge dictionary sits in the basement.
- **What happens during a request**: You type “What is a KV cache?” into a chat window.
- **Two phases: prefill and decode**: During prefill, the model already knows the whole input.

### Part 2: https://llm.yoga/en/06-inferenz-2.md

- **Memory bandwidth sets the writing speed**: If decode consists almost entirely of reading, you can work out a simple upper limit.
- **Time to first token**: For a sense of speed, it’s not only how fast a model writes that counts, but when it starts.

### Part 3: https://llm.yoga/en/06-inferenz-3.md

- **Batching: many users, one trip to memory**: Back to the translator with the dictionary in the basement.
- **GPU, CPU, TPU and unified memory**: So far we’ve talked about “the memory” and “the compute units”.

### Part 4: https://llm.yoga/en/06-inferenz-4.md

- **What an inference server does**: A model on its own is just a file full of numbers.
- **The well-known servers at a glance**: For local models and your own servers, there are five programs you keep running into.
- **Exercise: measure prefill and decode yourself**: Goal: you measure the prefill and decode rates separately, compare them with your computer’s theoretical upper limit and see the effect of batching.

### Part 5: https://llm.yoga/en/06-inferenz-5.md

- **Common misconceptions**: A computer with more compute automatically writes faster.
- **Review questions**: 1. A model in Q4_K_M is 9.3 GB.
- **Go further**: NVIDIA Technical Blog: Mastering LLM Techniques: Inference Optimization (November 17, 2023), prefill, decode and batching in detail.
