Skip to content
ML Academy

Core day 42 of 60 · Week 9 · Part II · orientation depth

LLM Architecture

A passenger chatbot is asked "Which gate does ZX104 leave from?" and answers "Gate A7", calmly and in flawless English. The gate system says B12. What exactly produced "A7", which settings could have changed the answer, and which could not?

By the end: You describe generation as a loop over next-token probabilities, state what temperature, top-k and top-p change and what they cannot, and explain why a fluent answer is not evidence that the answer is supported.

At a glance

A language model repeatedly scores every next token and samples one; decoding settings reshape that distribution but never add a missing fact.

  • Next-token loop

    Score all tokens, sample one, append, repeat until a stop token.

  • Tokens

    Subword pieces: an IBAN or a SKU may be several tokens; counts differ by language.

  • Temperature and top-p

    Sharpen or flatten, then truncate the distribution; no new knowledge.

  • Context window

    Prompt plus output must fit; a fitting prompt is not read evenly.

  • Fluent is not true

    Supply facts through the prompt or a tool and verify identifiers in code.

Recall · 5 min

No reviews due? Answer from memory. (1) What did TF-IDF fail to do when a message used a word the labelled data never contained (Day 41)? (2) In self-attention, what do queries and keys decide (Day 39)? (3) Why does a softmax output sum to one, and what does dividing the inputs by a number above one do to it?

Open due reviews

Sign in to record this block.

Concept · 10 min

A language model does one thing: given the Tokens so far, it scores every possible next token. A sampler picks one, appends it and the loop repeats. "Gate A7" appears because gate-like tokens are likely after "departs from gate", not because anything was looked up, so a wrong answer reads exactly as fluently as a right one.

Temperature and top-p change which token is drawn from a fixed distribution; they never change what the model knows

Predict first

In the invented-paper prompt you lower the temperature from 1.0 to 0.2. What happens to the share of draws that are an invented name?

Text description of the figure

A bar chart of a fixed, hand-written toy distribution over candidate next tokens (not a real model), with two prompts: a capital-city fact where the supported answer is the most likely token, and an invented paper title with no supported author. Controls set temperature, top-k, top-p, the number of seeded draws and the seed. Outline bars show the base probabilities and filled bars the reshaped ones; readouts give the probability of the supported answer, of fluent but unsupported tokens and of hedges, plus entropy and the drawn tallies. The gate example appears only in the application code.

Four ideas to hold on to Overview
  • Tokens in

    Text becomes integer ids from a fixed subword vocabulary.

  • Scores out

    One logit per vocabulary token at the last position.

  • Sampling reshapes

    Temperature and top-p change which token is drawn, not what is known.

  • No lookup

    Nothing in the loop checks a fact; verification lives outside the model.

Generation is one loop: score every token, reshape, draw one, append Loop
until a stoptoken or thelength limit1Tokens so farprompt ids, then everygenerated id2Transformer blocksfixed weights, maskedself-attention3Logitsone score per vocabularytoken4Softmax at T, top-psharpen or flatten, thencut the tail5One tokensampled, or the top oneif greedyforward passlast positiondivide by Tdrawappend
  1. Tokens so farprompt ids, then every generated idforward pass ↓
  2. Transformer blocksfixed weights, masked self-attentionlast position ↓
  3. Logitsone score per vocabulary tokendivide by T ↓
  4. Softmax at T, top-psharpen or flatten, then cut the taildraw ↓
  5. One tokensampled, or the top one if greedyappend ↓
  6. ↺ back to 1 · Tokens so far

Steps 1 to 3 are the model, with weights fixed after pretraining. Steps 4 and 5 are the decoder: it only reshapes and samples the distribution the model produced. Nothing in the loop looks a fact up.

The generation loop, one token at a time Illustration
Step 1… departs from gate ▸a70.33c30.20b120.18a20.11d90.10e50.07

Gate tokens get the share each gate has after "gate" in the training text; A7 wins with 0.33. Flight ZX104 plays no part.

Step 2… from gate a7 ▸at0.65.0.35

"at" is appended (0.65) and the model is asked again with the longer text.

Step 3… gate a7 at ▸14:300.2010:150.1812:050.1818:200.1816:500.1409:400.12

A near-tie: greedy takes 14:30 (0.20) over 10:15 (0.18). Sampling would often pick another, equally fluent value.

Step 4… a7 at 14:30 ▸.1.00

After "." the stop token has probability 1.00, so the loop ends.

Smallest possible language model: bigram counts from 400 seeded synthetic announcements. Greedy decoding of "flight zx104 departs from gate" gives "flight zx104 departs from gate a7 at 14:30 ." (the true gate in this story is B12). A real model conditions on the whole context, but it also emits the likeliest continuation, not a looked-up fact.

A bigram model counted from synthetic announcements, decoded greedily. Each step scores every next token, takes one and appends it.

Temperature and top-p as small multiples: same scores, six settings Illustration
T = 0.20.84A70.11C30.04B12D9theunknown

No gate in context. Entropy 0.79 bits; P(B12) 0.04; top-p 0.8 keeps 1 token, P(B12) after it 0.00.

T = 1.00.36A70.24C30.20B120.12D90.04the0.04unknown

No gate in context. Entropy 2.23 bits; P(B12) 0.20; top-p 0.8 keeps 4 tokens, P(B12) after it 0.22.

T = 2.00.26A70.22C30.19B120.15D90.09the0.08unknown

No gate in context. Entropy 2.47 bits; P(B12) 0.19; top-p 0.8 keeps 4 tokens, P(B12) after it 0.24.

T = 0.2A7C31.00B12D9theunknown

Gate B12 in context. Entropy 0.00 bits; P(B12) 1.00; top-p 0.8 keeps 1 token, P(B12) after it 1.00.

T = 1.00.08A70.05C30.83B12D9theunknown

Gate B12 in context. Entropy 0.97 bits; P(B12) 0.83; top-p 0.8 keeps 1 token, P(B12) after it 1.00.

T = 2.00.16A70.13C30.52B120.09D90.05the0.05unknown

Gate B12 in context. Entropy 2.05 bits; P(B12) 0.52; top-p 0.8 keeps 3 tokens, P(B12) after it 0.65.

  • A7, the model’s favourite (wrong)
  • B12, the gate in the gate system
  • hollow: cut by top-p 0.8

Hand-set toy scores from the application code (not a real model); probabilities computed exactly with the next-token-sampler widget’s code.

Top row without the gate in context, bottom row with it. Lower temperature sharpens whatever the model already favours.

Where a fact can come from Comparison
CriterionWeightslearned in pretrainingPrompttext you place in contextTool callmodel requests a lookup
FreshnessFrozen at a training cutoffAs fresh as the text you passLive
Private or live dataNever: an account balance or parcel status is in no training setYes, if you fetch and insert itYes, through an allowed function
Can be checkedNo source to point toYes: the answer can cite the lineYes: compare output with the tool result
CostFree at inferenceTokens, latency, window spaceA round trip and a contract to maintain (Day 47)
  • Loop: tokenize -> embed -> masked transformer blocks (Day 39) -> logits -> Temperature (sampling) and top-p -> sample -> append.
  • The weights are fixed after Pretraining; generation is inference only, and decoding settings only reshape the computed distribution.
  • The Context window holds prompt plus output; a KV cache keeps earlier keys and values and grows with length.
  • An answer supported by neither context nor world is a Hallucination; the fix is evidence (prompt, tool) plus verification.

Assumptions

  • The description fits decoder-only models in general; real products add system prompts, tools and post-training that change what is sampled.

  • The toy distribution below is hand-set and tiny; real vocabularies have tens of thousands of tokens.

  • Probability is not truthfulness. It reflects patterns in training text and the prompt, not a check against reality.

Sign in to record this block.

Application and workplace decision · 10 min

  • A toy sampler stands in for the chatbot's last step. Scores after "Flight ZX104 departs from gate" are hand-set (toy, not a real model): A7 is most likely, the true gate B12 is third.
  • A second score vector adds "ZX104: gate B12" to the context, which raises B12.
  • The code draws 5,000 samples for four settings with and without context.
  • Predict first: without context, which setting gives the most correct answers, and does any make the bot reliable?

Python

import numpy as np

tokens = np.array(["A7", "C3", "B12", "D9", "the", "unknown"])  # toy candidates after "Flight ZX104 departs from gate"
no_context = np.array([2.6, 2.2, 2.0, 1.5, 0.5, 0.3])  # hand-set toy scores, not from a real model
with_context = np.array([2.6, 2.2, 5.0, 1.5, 0.5, 0.3])  # same prompt plus "ZX104: gate B12" in the context
truth = "B12"  # the value in the gate system

def next_token_probs(logits, temperature=1.0, top_p=1.0):
    p = np.exp(logits / temperature)
    p /= p.sum()
    order = np.argsort(-p)
    n = min(np.searchsorted(np.cumsum(p[order]), top_p) + 1, len(p))  # smallest set reaching mass top_p
    q = np.zeros_like(p)
    q[order[:n]] = p[order[:n]]
    return q / q.sum(), n

rng = np.random.default_rng(42)
for label, logits in [("no context", no_context), ("with context", with_context)]:
    for t, tp in [(0.2, 1.0), (1.0, 1.0), (1.0, 0.8), (2.0, 1.0)]:
        q, n = next_token_probs(logits, t, tp)
        draws = rng.choice(tokens, size=5000, p=q)
        h = abs(-(q[q > 0] * np.log2(q[q > 0])).sum())
        print(f"{label:<12} T={t:<3} top_p={tp:<3} kept={n} entropy={h:.2f} bits  "
              f"P(top)={q.max():.2f} top={tokens[q.argmax()]:<3} correct={np.mean(draws == truth):.2f}")

This code is executed in the repository's checks (pnpm lesson:py / lesson:sql). It is not run on this site; copy it into your notebook.

Sign in to record this block.

Mini-assessment · 8 min

Your first answer is your evidence. You can re-answer later in practice mode; that is labelled practice.

Question 1 · easy

At each step of generation, what does a decoder-only language model compute?
Options

How confident are you? (optional, used for calibration)

Question 2 · medium

A fare-rules assistant uses a model with a 4,096-token context window. The prompt holds 3,000 tokens of policy text and 500 tokens of question, and the answer may need up to 800 tokens. By how many tokens does the request exceed the window? Enter a negative number if it fits.
tokens

How confident are you? (optional, used for calibration)

Question 3 · hard

A chatbot gives the wrong gate for ZX104. Someone sets temperature to 0 and retests: the same wrong gate appears 50 times out of 50. The gate is not in the prompt. What is the right conclusion and next step?
Options

How confident are you? (optional, used for calibration)

Question 4 · medium

Use the hand-set toy scores from the gate example without context (not from a real model): A7 2.6, C3 2.2, B12 2.0, D9 1.5, "the" 0.5 and "unknown" 0.3. With temperature 0.5 and no top-k or top-p truncation, what probability does the sampler give to A7? Answer as a decimal.
probability

How confident are you? (optional, used for calibration)

Question 5 · hard

A fare-rules assistant uses a model whose advertised window is 128,000 tokens. The prompt holds 95,000 tokens of policy text, a 400-token question and a limit of 800 tokens of answer, so it fits with room to spare. The one clause that answers the question sits near the middle of the text, and the answers often quote a different, similar clause. What is the most defensible next step?
Options

How confident are you? (optional, used for calibration)

Sign in to complete lessons and keep your progress.

Technical depth · optional · 27 min in total

Open only what you need. Nothing here is required to complete the day; the assessment covers the core above.

The model, defined, and what is inside it4 minThe autoregressive factorisation every decoder-only model computes, the stack from embeddings to logits, and a rough parameter count.
The model, defined: a product of next-token distributions Formula
pθ(x1,…,xN)=∏t=1Npθ(xt∣x<t),pθ(⋅∣x<t)=softmax(WU ht)p_\theta(x_1,\dots,x_N) = \prod_{t=1}^{N} p_\theta(x_t \mid x_{<t}), \qquad p_\theta(\cdot \mid x_{<t}) = \mathrm{softmax}(W_U\, h_t)
xtx_t
token id at position t, from a fixed vocabulary of size V
pθ(xt∣x<t)p_\theta(x_t \mid x_{<t})
the distribution the network outputs at step t, given only earlier tokens (the causal mask, Day 39)
hth_t
the last block's vector at position t, d numbers
WUW_U
unembedding matrix, V x d: one row per vocabulary token, so W_U h_t is the vector of V logits
∏\prod
the chain rule of probability: exact for any sequence, no independence assumption; the network only parameterises each factor

Generation samples the factors one at a time, left to right. Training (Day 43) maximises the same product on real text. Nothing in either step consults a fact base.

Inside a decoder-only model, bottom to top Stack
  1. Token embedding + positionEach id becomes a vector; position information is added or rotated in.
  2. Masked self-attentionEach position reads only earlier positions (Day 39).
  3. Feed-forward networkA per-position MLP, classically 4 times wider than the model; gated variants differ.
  4. Residual + normalisationAround each sub-layer; repeated for L blocks.
  5. UnembeddingThe last position's vector times the vocabulary matrix gives the logits.
Rough parameter count of a decoder stack Formula
N≈12 L d2+VdN \approx 12\, L\, d^{2} + V d
12 L d212\, L\, d^{2}
L blocks, each about 4d^2 attention plus 8d^2 feed-forward weights (width 4d)
LL
number of blocks (layers)
dd
model width
VdV d
embedding table, V = vocabulary size

L=32, d=4,096 gives about 6.4 billion block weights: an order-of-magnitude check, not a spec; gated MLPs, grouped-query attention and mixtures of experts change it. Each generated token costs about 2N floating-point operations (one multiply and one add per weight; Kaplan 2020), so decoding cost scales with model size times answer length.

Sampling, derived and computed by hand5 minThe temperature formula pushed through four logits on paper, top-p on the same numbers, and the four common decoders.
Temperature-scaled softmax Formula
pi=exp⁡(zi/T)∑jexp⁡(zj/T)p_i = \frac{\exp(z_i / T)}{\sum_j \exp(z_j / T)}
ziz_i
logit of token i at the last position
T<1T < 1
sharpens; T approaching 0 is greedy decoding
T>1T > 1
flattens; unlikely tokens are drawn more often

The ordering of tokens never changes with T; only how much mass the top ones get.

Four logits through temperature, by hand (delivery-chat toy, synthetic) Table
Token after "Your order will arrive"Logit zexp(z), T=1p at T=1p at T=0.5p at T=2
tomorrow2.07.3890.610.840.43
Friday1.02.7180.220.110.26
soon0.51.6490.140.040.21
never-1.00.3680.030.0020.10
sum (divide by it)-12.1241.001.001.00

Hand-set logits for an e-commerce delivery chat, not a real model. Divide z by T, exponentiate, divide by the sum; checked in numpy. Cooling to T=0.5 moves "tomorrow" from 0.61 to 0.84, real date or not.

Top-p 0.9 on the same four tokens, by hand Sequence
  1. Sort

    At T=1 the sorted probabilities are 0.61, 0.22, 0.14, 0.03.

  2. Accumulate

    Cumulative mass: 0.61, then 0.83, then 0.97: the first value at or above 0.9.

  3. Truncate

    Keep the smallest prefix reaching 0.9: three tokens; 'never' (0.03) is cut.

  4. Renormalise

    Divide the kept 0.61, 0.22, 0.14 by 0.97: final 0.63, 0.23, 0.14.

  5. Read it

    Top-p deleted only the tail. The favourite is unchanged; no new token can ever enter.

Same toy logits as the worked table, checked in numpy. Top-k would cut at a fixed count instead of a fixed mass.

Decoding strategies Table
StrategyWhat it doesTypical useWatch out for
GreedyAlways take the top tokenExtraction, classificationRepetition on long text
TemperatureRescale logits before softmaxLow for facts, higher for draftsNot a truth dial
Top-k (Fan 2018)Keep the k most likely tokensCap the tailFixed k ignores how peaked p is
Top-p (Holtzman 2020)Keep the smallest set with mass pDrafting, around 0.9-0.95 by conventionValues are conventions to test
Decoding defaults in Hugging Face Transformers (as read 2026-10-12) Table
SettingDocumented defaultExpert note
do_sampleoff: greedy decodingTemperature and top-p do nothing until sampling is on
temperature1.0 if not setA model's generation_config.json can override it
top_k50 if not setSilently truncates the tail when sampling; set it explicitly
top_p1.0 if not set (no truncation)Applied after top-k when both are set

Hosted APIs use their own defaults and parameter names. Record the exact settings with every evaluation run.

Properties of decoding: what it can and cannot change4 minRanking, log-odds, entropy and the two limits of temperature, each checked on the worked logits, then the same facts on the application's gate scores.
Properties of temperature, each checked on the four delivery-chat logits Table
PropertyWhy it holdsOn the worked logits
Ranking never changesz/T keeps the order of z for any T > 0, and exp is increasingtomorrow > Friday > soon > never at T = 0.5, 1 and 2
Log-odds scale by 1/Tlog(p_i / p_j) = (z_i - z_j) / Ttomorrow vs Friday: odds e^1 = 2.72 at T = 1, e^2 = 7.39 at T = 0.5
Entropy rises with TStandard result for softmax(z/T) when the logits are not all equal0.78 bits at T = 0.5, 1.46 at T = 1, 1.82 at T = 2
T to 0 is greedyAll mass moves to the largest logitp(tomorrow) 0.61, then 0.84, then 1 in the limit
T to infinity is uniformz/T goes to 0 for every token2.00 bits = log2(4) in the limit, whatever the logits said
Truncation only deletesTop-k and top-p zero the tail and renormaliseNo setting moves a token up the ranking; a cut token gets exactly 0

Entropy values computed in numpy. The first and last rows are the lesson in two lines: decoding re-weights the model's own ranking and never adds an option it did not score.

Lower temperature makes the top token more certain, right or wrong (toy scores) Chart
00.250.50.751123T 0.2: A7 0.84, B12 0.04T 1.0: A7 0.36, B12 0.20A7, wrong gate, no context — Temperature T: 0.1, Probability: 0.98A7, wrong gate, no context — Temperature T: 0.2, Probability: 0.841A7, wrong gate, no context — Temperature T: 0.3, Probability: 0.701A7, wrong gate, no context — Temperature T: 0.7, Probability: 0.438A7, wrong gate, no context — Temperature T: 1, Probability: 0.36A7, wrong gate, no context — Temperature T: 1.5, Probability: 0.296A7, wrong gate, no context — Temperature T: 2, Probability: 0.263A7, wrong gate, no context — Temperature T: 3, Probability: 0.23A7, wrong gate, no co…B12, true gate, no context — Temperature T: 0.1, Probability: 0.002B12, true gate, no context — Temperature T: 0.2, Probability: 0.042B12, true gate, no context — Temperature T: 0.3, Probability: 0.095B12, true gate, no context — Temperature T: 0.7, Probability: 0.186B12, true gate, no context — Temperature T: 1, Probability: 0.198B12, true gate, no context — Temperature T: 1.5, Probability: 0.198B12, true gate, no context — Temperature T: 2, Probability: 0.195B12, true gate, no context — Temperature T: 3, Probability: 0.188B12, true gate, no co…B12 with the gate in context — Temperature T: 0.1, Probability: 1B12 with the gate in context — Temperature T: 0.2, Probability: 1B12 with the gate in context — Temperature T: 0.3, Probability: 1B12 with the gate in context — Temperature T: 0.7, Probability: 0.943B12 with the gate in context — Temperature T: 1, Probability: 0.832B12 with the gate in context — Temperature T: 1.5, Probability: 0.646B12 with the gate in context — Temperature T: 2, Probability: 0.52B12 with the gate in context — Temperature T: 3, Probability: 0.387B12 with the gate in …Temperature TProbability
  • A7, wrong gate, no context
  • B12, true gate, no context
  • B12 with the gate in context

Exact softmax probabilities of the application's hand-set scores. Without evidence, cooling the sampler makes the wrong gate near-certain; with evidence, it makes the right gate certain.

Share of correct gates in 5,000 draws (application output) Chart
  • No context, T=0.20.04 share correct
  • No context, T=1.00.2 share correct
  • No context, top-p 0.80.22 share correct
  • No context, T=2.00.2 share correct
  • Context, T=0.21 share correct
  • Context, T=1.00.84 share correct
  • Context, top-p 0.81 share correct
  • Context, T=2.00.52 share correct

No setting rescues the no-context case. Once the evidence is in context, low temperature and truncation remove the residual wrong draws.

Tokens and the vocabulary3 minHow byte-pair encoding builds the vocabulary and why token counts differ by language and by identifier.
How a subword vocabulary is built (byte-pair encoding) Sequence
  1. Start small

    Begin with single characters or bytes as the vocabulary.

  2. Count pairs

    Count adjacent symbol pairs across the training text.

  3. Merge

    Add the most frequent pair as a new symbol.

  4. Repeat

    Stop at the target vocabulary size.

  5. Result

    Common words are one token; rare words and codes split into pieces.

  6. Consequence

    An IBAN, SKU or ICD-10 code becomes several tokens, each a separate guess: verify identifiers in code.

Sennrich et al. (2016). Budgets, prices and window limits are counted in tokens, so count with the deployed model's own tokenizer: text from languages that were rare in its corpus needs more tokens for the same content (Petrov et al. 2023).

Budgeting the context window3 minWhat shares the window and why a prompt that fits can still be read unevenly.
A context budget for an insurance claims assistant (hypothetical) Chart
  • System prompt
  • Retrieved passages
  • Tool output
  • User question
  • Maximum answeroutput shares the window
  • Window

Illustrative values: they show the pattern, not measured results.

Everything left of the window bar is summed against it (7,800 of 8,192 here). Count with the deployed model's own tokenizer.

KV-cache arithmeticadvanced3 minMemory per token and per sequence, and why long contexts reduce serving throughput.
KV-cache memory per token Formula
Mtoken=2⋅L⋅nkv⋅dhead⋅bM_{token} = 2 \cdot L \cdot n_{kv} \cdot d_{head} \cdot b
22
one key and one value
LL
number of layers
nkvn_{kv}
key-value heads (fewer than query heads with grouped-query attention)
dheadd_{head}
dimension per head
bb
bytes per number (2 for 16-bit)
KV cache by hand: 128 KiB per token becomes 100 GiB per server Sequence
  1. Per token

    2 x 32 layers x 8 KV heads x 128 dims x 2 bytes = 131,072 bytes = 128 KiB.

  2. Per sequence

    Linear in length: 32,768 tokens x 128 KiB = 4 GiB of cache, before weights and activations.

  3. Per server

    Times concurrency: 100 requests of 8,192 tokens = 100 x 1 GiB = 100 GiB. This is what caps batch size.

  4. Fewer KV heads

    Grouped-query attention shares each KV head among several query heads (Ainslie 2023): 8 instead of 32 cuts it 4x.

  5. Fewer bits

    An 8-bit cache halves it again (64 KiB per token); test accuracy at your own lengths before relying on it.

Hypothetical configuration (32 layers, 32 query heads of 128 dims, 16-bit numbers), checked in Python.

KV cache against context length, and what it does to batch size Illustration
KV cache for one sequence035701k8k32k128kcontext length in tokens (log scale)GiBmodel weights 12.0 GiB32 KV heads, 1,024 tokens: 0.532 KV heads, 2,048 tokens: 1.032 KV heads, 4,096 tokens: 2.032 KV heads, 8,192 tokens: 4.032 KV heads, 16,384 tokens: 8.032 KV heads, 32,768 tokens: 1632 KV heads, 65,536 tokens: 3232 KV heads, 131,072 tokens: 6432 KV heads8 KV heads, 1,024 tokens: 0.18 KV heads, 2,048 tokens: 0.38 KV heads, 4,096 tokens: 0.58 KV heads, 8,192 tokens: 1.08 KV heads, 16,384 tokens: 2.08 KV heads, 32,768 tokens: 4.08 KV heads, 65,536 tokens: 8.08 KV heads, 131,072 tokens: 168 KV heads1 KV head, 1,024 tokens: 0.01 KV head, 2,048 tokens: 0.01 KV head, 4,096 tokens: 0.11 KV head, 8,192 tokens: 0.11 KV head, 16,384 tokens: 0.31 KV head, 32,768 tokens: 0.51 KV head, 65,536 tokens: 1.01 KV head, 131,072 tokens: 2.01 KV headcache passes the weights at 24k tokens

One sequence. Memory grows linearly with length: 128 KiB per token with 8 KV heads, so 4 GiB at 32k tokens. With 32 KV heads a single 32k request needs more than the 12.0 GiB of weights.

Sequences that fit (80 GiB card)1101001000100001k8k32k128kcontext length in tokens (log scale)sequences32 KV heads, 1,024 tokens: 13632 KV heads, 2,048 tokens: 6832 KV heads, 4,096 tokens: 3432 KV heads, 8,192 tokens: 1732 KV heads, 16,384 tokens: 8.032 KV heads, 32,768 tokens: 4.032 KV heads, 65,536 tokens: 2.032 KV heads, 131,072 tokens: 1.032 KV heads8 KV heads, 1,024 tokens: 5448 KV heads, 2,048 tokens: 2728 KV heads, 4,096 tokens: 1368 KV heads, 8,192 tokens: 688 KV heads, 16,384 tokens: 348 KV heads, 32,768 tokens: 178 KV heads, 65,536 tokens: 8.08 KV heads, 131,072 tokens: 4.08 KV heads1 KV head, 1,024 tokens: 43521 KV head, 2,048 tokens: 21761 KV head, 4,096 tokens: 10881 KV head, 8,192 tokens: 5441 KV head, 16,384 tokens: 2721 KV head, 32,768 tokens: 1361 KV head, 65,536 tokens: 681 KV head, 131,072 tokens: 341 KV head

Concurrent sequences that fit in the 68 GiB left on a 80 GiB card after the weights. With 8 KV heads: 136 at 4k tokens, 17 at 32k, 4 at 128k. Long contexts trade away batch size.

  • 32 KV heads (multi-head): 512 KiB/token
  • 8 KV heads (grouped-query): 128 KiB/token
  • 1 KV head (multi-query): 16 KiB/token

Arithmetic on a hypothetical configuration (32 layers, head dimension 128, 16-bit cache, 6.4e9 block weights, an 80 GiB card); activations, fragmentation and framework overhead are ignored, so real capacity is lower.

Linear growth per sequence; fewer key-value heads (grouped-query, multi-query attention) cut it by the head ratio.

The two phases of serving a request Comparison
CriterionPrefillread the promptDecodewrite the answer
What runsAll prompt tokens in parallel through every layerOne token per step, reusing the KV cache
Grows withPrompt length (attention part grows quadratically)Answer length times model size
Usually limited byComputeMemory bandwidth: weights and cache are read every step
LeverShorter prompts; caching a shared prefixBatching requests; smaller cache per token

A practitioner model of serving cost, consistent with the paged-attention paper's description of the cache (Kwon et al. 2023). Measure latency per phase.

Diagnosing hallucination3 minA decision path from a wrong, fluent answer to the control that fixes it.
Diagnosing a wrong, fluent answer Diagram
Wrong fluentanswerFact in contextor tool?Supply itprompt, retrieval ortoolWas it used?position,…Shorten or reorderthe context; test atreal lengthsVerify in codeagainst the system ofrecordnoyesnothenthen
  1. Wrong fluent answer
  2. Fact in context or tool?
    • no: to Supply it
  3. Was it used?position, length, conflicts
    • no: to Shorten or reorder
  4. Supply itprompt, retrieval or tool
    • then: to Verify in code
  5. Shorten or reorderthe context; test at real lengths
  6. Verify in codeagainst the system of record

Both branches end at a check in code: no setting inside the loop verifies a fact. Why models guess at all is evolving research; Kalai et al. (2025, preprint) argue that training and scoring reward guessing over abstaining.

Four ways the token loop misleads Overview
  • Confident wrong facts

    Plausible tokens without support; same tone as right ones.

  • Stale knowledge

    Weights stop at a cutoff; prices, tariffs and policies move on.

  • Lost in a long prompt

    Fitting the window does not mean every clause is used.

  • Repetition

    Greedy decoding on long text loops (Holtzman 2020).

Interview nuances and common misreadingsadvanced2 minFive statements that sound right in an interview and what a staff-level answer says instead.
Common misreadings, and the staff-level correction Overview
  • Temperature 0 means truth

    It means the top token every time; hosted systems may not even be bitwise repeatable.

  • Probability is confidence

    Token probabilities are uncalibrated for facts until checked on labelled data.

  • Window size is memory

    The window is per request; nothing persists between calls unless you resend it.

  • Longer context is free

    It costs prefill compute, KV memory and batch size, and may be used unevenly.

  • Tokens are words

    Codes, numbers and rarer languages take more tokens per word.

Memory anchor

A model emits likely tokens, not verified facts: fix support with context, then verify.

Useful for

  • Explaining to stakeholders why a confident answer needs a source, and which control (context, tool, verification) fixes what.

  • Budgeting prompts: counting prompt plus output tokens, and expecting memory and latency to grow with context length.

  • Choosing decoding settings per task: low temperature for extraction and classification, higher for drafting variants.

Not suitable for

  • Predicting how a specific hosted model behaves; products differ in tokenizer, post-training, system prompt and sampling defaults, so measure on your own cases.

  • Treating token probabilities as calibrated confidence in a fact without a calibration check on labelled data (Day 45).

Traps

Using temperature as a truth dial

A bank's card-support team lowers temperature to cut invented fee amounts. The answers become repeatable, including the wrong ones, and the change is credited with a safety it does not provide.

How to avoid it

Treat temperature as a variance control. Fix unsupported answers with context, retrieval or tools, and verify identifiers and numbers against a system of record.

Counting only the prompt against the window

A telecom contract-summary bot passes a 7,500-token contract to an 8,192-token model. Output tokens, system prompts and tool results share the same window, so the request fails or the summary is cut mid-sentence.

How to avoid it

Budget prompt, retrieved text, tool output and maximum answer length together, and count tokens with the deployed model's own tokenizer.

Senior reasoning

A senior separates what the model computed from what the product guaranteed. They ask where the fact came from: the weights (frozen at a cutoff), the prompt, or a tool. They pick decoding settings per task, never to cure unsupported output, budget the window and cache, and put a verifier between the model and the customer for identifiers, amounts and dates: an account number in banking, a dosage in a clinic's patient line, a delivery date in logistics. They also test at the lengths and languages of real traffic rather than demo prompts.

Interview question

A hospital's appointment-line assistant gave a patient the wrong clinic opening hours, and a stakeholder says "the model hallucinated, can we just turn the temperature down?" How do you respond?

Recall cards scheduled after completion

  • What do temperature, top-k and top-p change, and what can they not change?
  • What does the context window have to hold, and what is the KV cache for?

Reviewed on a 1 · 3 · 7 · 14 · 30-day schedule; a miss returns the card to 1 day.

References

Optional sections

Deep dive · 10 min

Parameter count, roughly. For a standard decoder block with model width dd and a feed-forward width of 4d4d, attention holds about 4d24d^2 weights and the feed-forward network about 8d28d^2, so LL blocks hold about 12 L d212\,L\,d^2. A hypothetical configuration with L=32L=32 and d=4096d=4096 gives about 6.4 billion weights, plus the embedding table (V⋅dV\cdot d). Real models vary (gated feed-forward layers, grouped-query attention, mixture of experts), so treat this as an order-of-magnitude check, not a spec.

KV cache arithmetic (approximate, hypothetical configuration). Per token the cache stores keys and values for every layer: 2×L×nkv×dhead×2\times L\times n_{kv}\times d_{head}\times bytes. With L=32L=32, 8 key-value heads, dhead=128d_{head}=128 and 2-byte numbers, that is 131,072 bytes (128 KiB) per token, so a single 32,768-token sequence needs about 4 GiB before weights and activations. Serving many requests multiplies it, which is why long contexts reduce batch size.

What pretraining optimises. Cross-entropy on the next token over a very large corpus. Loss falls smoothly with model size, data and compute (Kaplan 2020), but low average loss says nothing about any single fact. Rare facts tend to fail first (insight-0504), and facts after the training cutoff are absent.

Sampling in practice. Greedy decoding is deterministic given identical numerics but tends to repeat itself on long text. Sampling with temperature around 0.7-1.0 and top-p around 0.9-0.95 is a common drafting setting; these values are conventions to test, not constants. For extraction, classification and tool calls, use low temperature and validate the output format (Day 47).

Retrieval project link (weekend studio). In the Sunday recall block of the retrieval milestone you are asked why a fluent answer is not evidence and what temperature and top-p change. Use today's script as the worked example: the answer is that they reshape a distribution, so the evidence has to come from the retrieved chunk.

Interview practice · 10 min

A logistics company's chatbot states a wrong delivery date in a confident, polished sentence. Explain to the product owner, without maths, why that happens. Then say what you would build so it happens less.

Content version 7 · last verified 2026-10-18 · depends on numpy