4.2.10 · D5Tokenization & Language Modeling

Question bank — Context window and sequence length

1,374 words6 min readBack to topic

Every reveal line below is Statement ::: Reasoning. The answer side always carries the why, never a bare yes/no.


True or false — justify

A model with a 100k context window remembers everything in the window equally well.
False. Even inside the window there is a lost-in-the-middle effect — tokens near the start and end are attended to far better than those in the middle. Fitting is not the same as attending well.
Sequence length and context window are two names for the same number.
False. Sequence length is the token count of the current input (your per-request choice); the context window is the model's fixed trained ceiling. The rule is .
Doubling the context window doubles the attention cost.
False. Attention compute is , so doubling quadruples the pairwise-comparison cost. Linear intuition fails here.
Attention memory for the weight matrix scales as .
False. The stored attention matrix holds one scalar weight per (query, key) pair, so it is . The extra only appears in the compute of each dot product, plus a separate term for storing Q/K/V.
Context length is measured in words.
False. It is measured in tokens (subword units). "internationalization" is one word but can be 5+ tokens, so a word count badly underestimates . See Tokenization.
If your prompt uses the entire context window, you can still generate a long reply.
False. Prompt and generated output share the same window. If the prompt already fills , there is zero budget left for output and the reply is cut off immediately.
A longer context window always improves answer quality.
False. Beyond guaranteeing more text fits, extra length can add noise and dilute attention (lost-in-the-middle). Quality depends on what you put in the window, not just how much fits.
Positional encoding is optional — attention already knows word order.
False. Raw self-attention is a permutation-invariant weighted sum, so "dog bites man" and "man bites dog" would look identical without positional encoding added in. See Positional Encoding.
RoPE and ALiBi let a model use any length with guaranteed quality.
False. These tricks extend usable length beyond the training window, but quality is only guaranteed up to the length the model was actually trained on; far past it, behaviour degrades.

Spot the error

"To double throughput I'll just halve and process twice the batch — same total cost."
Wrong. Halving cuts attention cost by ~4× (quadratic), so twice the batch at half length is actually cheaper per token, not cost-neutral. The law breaks the linear bookkeeping.
"The KV cache grows quadratically with sequence length, like the attention matrix."
Wrong. The KV Cache stores one key and value vector per token, so it grows linearly, . The quadratic term is the attention-weight matrix, which is recomputed, not the cache.
"Since compute is , using a narrower model (smaller ) fixes the context limit."
Wrong. Shrinking scales cost linearly but the quadratic-in- explosion remains. The context ceiling is driven by the factor, which cannot cancel.
"My essay is 6000 words and , so it fits with room to spare."
Wrong. English averages ~1.3 tokens/word, so 6000 words ≈ 7800 tokens. It barely fits and leaves almost nothing for the output budget. Always estimate in tokens, not words.
"I truncated the document to fit, so no information was lost."
Wrong. Truncation and Chunking discards the tokens that fall outside the window — that content is gone unless you chunk or summarize. Truncation trades completeness for fitting.
"Padding tokens don't matter because they're empty."
Wrong. Padding still occupies positions and, unless masked, still enters the attention computation — wasting compute and potentially leaking attention onto meaningless slots.

Why questions

Why is attention compute but attention memory only ?
There are query–key pairs; each pair's dot product costs multiply-adds (the compute), but the result stored is a single scalar weight (the memory). The lives in the calculation, not in the stored number.
Why does doubling the window quadruple attention cost rather than double it?
Attention compares every token with every token, giving pairs. Doubling makes pairs, so the workload is squared, not merely doubled.
Why must positional information be added — why can't attention just infer order from the sequence?
Self-attention computes an order-independent weighted sum, so it sees a set of tokens, not an ordered list. Without an explicit position signal, all permutations produce the same representation.
Why do older models degrade sharply just past their trained length rather than gracefully?
Classic learned/sinusoidal position tables only have entries up to . Position has no encoding at all, so the model is fed a signal it has never seen — a cliff, not a slope.
Why does generating a long reply eat into the same budget as the prompt?
Each generated token is appended to the running sequence and must be attended to on the next step, so it occupies a real position inside the shared window: .
Why is a KV cache used at all if attention is still overall?
The cache stores past keys/values so each new token only attends against them instead of recomputing every earlier token's K and V. It trades memory to avoid redundant work per step, even though total attention across the sequence stays quadratic.

Edge cases

What is the sequence length of a single-token input, and does attention still run?
: the token attends only to itself, a trivial attention matrix. Attention still "runs" but does nothing informative — there are no other tokens to compare against.
What happens at exactly when you then ask for output?
The window is completely full, so there is zero budget for generation. The model can produce nothing new (or must evict the oldest tokens), which is why you must reserve space for output in advance.
If a document tokenizes to fewer tokens than , is there any downside?
No hard failure — it simply fits with spare budget. You pay attention cost proportional to the actual , not the full , so short inputs are cheaper. Unused window is free.
What happens to a token pushed just outside the window in a sliding-window setup?
It becomes literally invisible — the model has no encoding or attention path to it, so any information it carried is forgotten as if it were never present.
Can two different tokenizers give the same text different sequence lengths?
Yes. Tokenization is tokenizer-specific: the same string may split into more or fewer subword pieces, so (and whether it fits ) depends on which Tokenization scheme you use.
Is it possible to have passed to the model at all?
Not without failure — you must truncate, chunk, or summarize first, because there is no valid position encoding or allocated slot beyond . The requirement is a hard precondition.

Recall One-line summaries to carry away
  • Fitting ≠ attending: lost-in-the-middle is real.
  • is your choice; is the fixed ceiling.
  • Cost is ; weight memory is ; KV cache and Q/K/V are .
  • Prompt + output share one window.
  • Everything is counted in tokens, never words.