GGiriBioAI

/ 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.

By the end you can
  • 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.

about 15 min hover ? for help

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.

Live lab · Fit a conversation into the window
when it overflows:
used 925 answer reserve 500
  1. systemYou are a helpful lab assistant.150 tok
  2. userHi! I'm planning a CRISPR knockout experiment.dropped
  3. assistantGreat, which gene and cell line?dropped
  4. userGene TP53 in HEK293. Our project code is BLUE-42.dropped
  5. assistantHere is a guide RNA design checklist…dropped
  6. userHow many guides should I test?dropped
  7. assistantUsually 3 to 4 per gene, because…dropped
  8. userCan you draft the transfection protocol?dropped
  9. assistantDay 1: seed cells… Day 2: transfect…dropped
  10. userAnd the validation by sequencing?45 tok
  11. assistantUse Sanger + TIDE analysis, steps…700 tok
  12. userWhat was our project code again?30 tok
model's answer to the last question

"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.

KV cache
Stored keys and values of earlier tokens, so each new token only computes its own.
grouped-query attention
Several query heads share one key/value head, shrinking the KV cache (used by Llama 3).

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.

Live lab · KV cache calculator
2 (K and V) × 32 layers × 8 KV heads × 128 dims × 8,192 tokens × 1 × 2 bytes
1.1 GB
128 KB per token
tokens processed per generation step (prompt of 8, then 16 new tokens)
without cache248 total
with KV cache23 total

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.

Live lab · Route tokens to experts
top-k experts per token:
numberscodebiologygrammarnamespunctuationverbsgeneral
Alice....✓..✓
edits......✓✓
the...✓...✓
TP53..✓....✓
gene..✓....✓
with...✓...✓
CRISPR..✓....×
,.....✓.×
then.✓.....×
runs......✓×
print.✓.....×
(.✓...✓..
42✓......×
).✓...✓..
in...✓...×
Python.✓..✓...
......✓.×
numbers
1/6
code
5/6
biology
3/6
grammar
3/6
names
2/6
punctuation
4/6
verbs
2/6
general
6/6
routed slots: 26
dropped (expert full): 8
active share of experts per token: 2/8 = 25%

Green ✓ = processed, yellow × = overflowed. Real routers add a load-balancing loss during training so no expert gets all the work.

lost in the middle
Models recall facts at the start and end of a long context better than those in the middle.
mixture of experts
Many expert MLPs per layer; a router sends each token to only a few, so compute stays low.
router / gate
The small network in an MoE layer that scores experts for each token.

Check yourself

Tick each one you could explain to a friend. If you can't yet, scroll back up and replay the lab.

Finished exploring?
Progress is saved only in this browser.