4.2.10 · D3Tokenization & Language Modeling

Worked examples — Context window and sequence length

3,096 words14 min readBack to topic

Before we start, two quantities everything below uses:


The scenario matrix

Every question this topic can ask lives in one of these cells. The Example column links straight to the worked example that fills that cell.

Cell Case class The question it tests Example
A (fits, room to spare) Does it fit? How much slack? Ex 1
B (fits exactly, degenerate boundary) What happens at the exact edge? Ex 2
C (overflow) How many chunks? How much to cut? Ex 3
D (empty / zero input) Limiting case: no tokens at all Ex 4
E Prompt + output share the window Budget split; generation overflow Ex 5
F Cost scaling (the law) Non-doubling ratios, fractional scale Ex 6
G Real-world word problem Convert words/chars/code → tokens Ex 7
H Exam twist (combine cells) Multi-turn chat eating the window Ex 8

We'll use English ≈ 1.3 tokens/word (the parent's rule) unless told otherwise.


Figure — Context window and sequence length

Read the figure as our example map. The white box is the same fixed window () in every row — the dashed white line on the right is the ceiling. The coloured fill is how many tokens you actually feed:

  • Blue row (Cell A / Ex 1): the memo fills only part of the box — white space remains, matching the ~38% used in Ex 1.
  • Yellow row (Cell B / Ex 2): the fill reaches the dashed line exactly — , zero slack, exactly Ex 2's boundary.
  • Red row (Cell C / Ex 3): the fill spills past the dashed line (hatched red) — the overflow that forces the 4 chunks of Ex 3.
  • Green row (Cell D / Ex 4): the box is empty — , the limit case of Ex 4.

Every other example (5–8) is a combination of these four fill states, so keep this picture in mind: you are always asking "where does the fill land relative to the dashed ceiling?"


Cell A — fits with room to spare


Cell B — the exact boundary (degenerate)


Cell C — overflow, needs chunking


Cell D — the zero / empty limit


Cell E — prompt + output share one window


Cell F — the cost law, non-obvious ratios


Cell G — real-world: mixed content


Cell H — exam twist: multi-turn chat


Recap: which rounding, which law?

Recall One line per cell — forecast before revealing
  • Cell A/B/C decision rule ::: check ; slack .
  • Cell C chunk count ::: (round UP to cover everything).
  • Cell D limit ::: compute , full window free.
  • Cell E output room ::: ; reply truncates there.
  • Cell F cost ratio ::: square the length ratio: .
  • Cell G mixed content ::: tokenize each type at its own rate, then add.
  • Cell H rounds that fit ::: (round DOWN to fit).

Flashcards

To chunk a text of tokens into a window of , how many chunks?
— ceiling, so no text is left out.
A model has prompt = 5000 tokens, ; you ask for a 4000-token reply. How many tokens actually come back?
; the reply is truncated (808 tokens lost).
If the context window grows , how much does attention compute grow?
— square the length ratio.
What is the attention compute when ?
Zero — the attention matrix is empty ().
What does mean in the cost law ?
The model width / embedding dimension — the length of each token's vector; it's the cost of one query–key dot product.
Chat resends history: system 500 tok + 500 tok/round, . How many full rounds fit?
rounds.
Ceiling vs floor — when each?
Ceiling to cover a document in chunks; floor to count rounds/replies that fit inside the window.