/ module 08 · Inside the model
Context, KV Cache & MoE
Three engineering ideas decide how much a model can read, how fast it answers and how big it can grow: the context window, the KV cache and Mixture of Experts. Each lab below shows the trade-off with numbers.
- Explain why chatbots forget early messages
- Estimate KV cache memory and say why it speeds generation
- Describe how Mixture-of-Experts routes each token
Try this first: Slide the context window up until the bot remembers the project code.
context window
The model only sees what fits
The context window is the maximum number of tokens per request: system prompt, the whole chat so far, retrieved documents and room for the reply. Chat apps silently drop or summarize old turns when a conversation outgrows it, which is why a model "forgets" what you said earlier.
- systemYou are a helpful lab assistant.150 tok
- userHi! I'm planning a CRISPR knockout experiment.dropped
- assistantGreat, which gene and cell line?dropped
- userGene TP53 in HEK293. Our project code is BLUE-42.dropped
- assistantHere is a guide RNA design checklist…dropped
- userHow many guides should I test?dropped
- assistantUsually 3 to 4 per gene, because…dropped
- userCan you draft the transfection protocol?dropped
- assistantDay 1: seed cells… Day 2: transfect…dropped
- userAnd the validation by sequencing?45 tok
- assistantUse Sanger + TIDE analysis, steps…700 tok
- userWhat was our project code again?30 tok
"I'm sorry, I don't see a project code in our conversation." The message with the code fell out of the window.
Even inside the window, recall is uneven: studies on long contexts found facts in the middle of a long prompt are recalled worse than those at the start or end ("lost in the middle"). Put key instructions first and repeat critical facts near the question.
inference
The KV cache: remember, don't recompute
A model writes one token at a time. Without a cache, every new token would recompute keys and values for the whole text again. The KV cache stores them, so each step only processes the newest token. The price is memory, which grows with layers, heads, context length and batch size, and is often what limits long contexts on a GPU.
architecture
Mixture of Experts: big brain, small bill
An MoE layer replaces the single MLP with many expert MLPs plus a router. Each token visits only its top-k experts, so a model can hold far more parameters than it uses per token. Mixtral 8x7B stores about 47B parameters but activates about 13B per token. Experts do not neatly specialize by topic in real models; the labels below are a teaching simplification.
| numbers | code | biology | grammar | names | punctuation | verbs | general | |
|---|---|---|---|---|---|---|---|---|
| Alice | . | . | . | . | ✓ | . | . | ✓ |
| edits | . | . | . | . | . | . | ✓ | ✓ |
| the | . | . | . | ✓ | . | . | . | ✓ |
| TP53 | . | . | ✓ | . | . | . | . | ✓ |
| gene | . | . | ✓ | . | . | . | . | ✓ |
| with | . | . | . | ✓ | . | . | . | ✓ |
| CRISPR | . | . | ✓ | . | . | . | . | × |
| , | . | . | . | . | . | ✓ | . | × |
| then | . | ✓ | . | . | . | . | . | × |
| runs | . | . | . | . | . | . | ✓ | × |
| . | ✓ | . | . | . | . | . | × | |
| ( | . | ✓ | . | . | . | ✓ | . | . |
| 42 | ✓ | . | . | . | . | . | . | × |
| ) | . | ✓ | . | . | . | ✓ | . | . |
| in | . | . | . | ✓ | . | . | . | × |
| Python | . | ✓ | . | . | ✓ | . | . | . |
| . | . | . | . | . | . | ✓ | . | × |
Green ✓ = processed, yellow × = overflowed. Real routers add a load-balancing loss during training so no expert gets all the work.
Check yourself
Tick each one you could explain to a friend. If you can't yet, scroll back up and replay the lab.