The interview question

Why does a longer prompt make a transformer expensive, and what can caching reuse?

Trace one attention operation and connect context length to memory, caching, and system design.

01. Start with a weighted combination

Attention computes how strongly a query should use available keys, then combines the associated values. In scaled dot-product attention, query–key dot products are divided by the square root of the key dimension and normalized with softmax. The output is the resulting weighted sum of values.

For an illustrative two-token attention row with normalized weights 0.75 and 0.25 and scalar values 2 and 10, the output is 0.75×2 + 0.25×10 = 4. This is one operation within a larger network, not a complete language model or an explanation of factual reliability.

02. Explain masks and position

A causal mask prevents a token from attending to future tokens during autoregressive prediction. Padding masks prevent artificial padding from participating as content. The model also needs a representation of position because the attention operation by itself does not encode ordinary sequence order.

In a document assistant, simply placing two chunks next to each other does not establish which document is authoritative. Metadata, retrieval quality, and the generation instructions still matter. Attention weights should not be treated as a reliable causal explanation of why an answer was produced.

03. Reason about context cost

For dense attention, the conceptual attention-score matrix has one row and column per token. Doubling sequence length quadruples its number of entries. Efficient implementations can avoid materializing the whole matrix, so distinguish the mathematical operation from a specific implementation’s memory use.

A hypothetical 2,000-token sequence has four million token-pair positions per head, while 4,000 tokens has sixteen million. This helps explain why adding every retrieved document can be expensive even before considering whether irrelevant context hurts the answer.

04. Separate prefill and decoding

Prompt processing computes representations for the supplied context. During autoregressive generation, a key–value cache can reuse prior tokens’ keys and values instead of recomputing them at every step. The cache grows with sequence length and competes for device memory across concurrent requests.

Follow-up: batching raises throughput but users complain about latency. Measure time to first token separately from subsequent token latency, and account for waiting in the batch queue. Prefix reuse requires compatible model state and exact applicable context; authorization and tenant boundaries still belong in the caching design.

Practice the follow-up

Self-review

Explain why a bigger context window does not guarantee better retrieval or a more truthful answer.

Connect this to document question answering.

Sources & further reading

Vaswani et al.: Attention Is All You Need. The interview scenario, calculations, and proposed design choices are illustrative.

Continue your preparation

Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.