Chapter 2: Tokens and the Context Window

Learning Objectives

Pre-Reading Check — Tokenization Fundamentals

1. What does a language model actually read as its input?

2. How does byte-pair encoding (BPE) build a model's vocabulary?

3. Which rule of thumb best approximates token counts for standard English prose?

1. Tokenization Fundamentals

Key Points

A language model does not read letters or words — it reads sequences of integers. The tokenizer converts human text into those integers (and back), and the integer-mapped pieces it produces are tokens. Tokenization is the very first step in every request: text is split into tokens (whole words, word fragments, single characters, or bytes) and each is mapped to an ID in a fixed vocabulary.

Whole-word vocabularies would be enormous and still fail on typos, brand names, code, and emoji; raw characters make sequences painfully long. Modern models take a middle path — subword tokenization — and the dominant algorithm is byte-pair encoding. BPE starts from the smallest units and repeats one move: find the most frequent adjacent pair and merge it into a new unit, adding one vocabulary entry each time, until the vocabulary hits a target size. The effect is compression.

OpenAI's tiktoken implements byte-level BPE: because it operates on raw bytes, it is universal — any text produces a valid encoding, with no "unknown character." The catch: non-ASCII characters often span multiple bytes and therefore multiple tokens, so non-English or symbol-dense text tends to cost more tokens than the same meaning in plain English.

flowchart LR A["Raw text: 'the tokenizing'"] --> B{"Split into subword tokens"} B --> C["Common word 'the' passes through whole"] B --> D["Rare word 'tokenizing' fragments: token + iz + ing"] C --> E["Map each token to a vocabulary ID"] D --> E E --> F["Integer sequence: e.g. 279, 4037, 449, 288"] F --> G["Model reads the integers"]
Animation slot: BPE tokenization pipeline (raw text → subword split → vocab IDs → integers)

Tokens vs. Words vs. Characters

Because a token is neither a word nor a character, engineers estimate with rough ratios. The exact ratio depends on content: conversational text runs lower, while technical writing and especially source code push counts higher (punctuation, rare identifiers, and symbols fragment into many tokens). Vocabulary size also shifts the math: GPT-4/GPT-3.5-Turbo use cl100k_base (~100K vocab), while GPT-4o uses o200k_base (~200K vocab), so the same sentence may tokenize to fewer tokens under the larger vocabulary.

Unit of measureRough English-prose equivalence
1 token~0.75 words
1 word~1.33 tokens
1 token~4 characters
100 tokens~75 words
1,000 words~1,333 tokens

Counting Tokens Before You Send

Estimating is fine for a napkin sketch; production systems count exactly. Counting matters because tokens determine two things you cannot guess about: billing (you pay per token) and the context window (a hard maximum per request). The essential move is to select the encoding that matches your target model — counting with the wrong encoding gives the wrong answer.

Animation slot: live token counter comparing cl100k_base vs o200k_base
Post-Reading Check — Tokenization Fundamentals

1. What does a language model actually read as its input?

2. How does byte-pair encoding (BPE) build a model's vocabulary?

3. Which rule of thumb best approximates token counts for standard English prose?

Pre-Reading Check — The Context Window

4. What does the context window include?

5. On a 128K-token window, your prompt already uses 120K tokens. What happens?

6. A model advertises a 200K-token context window. How should you treat that number?

2. The Context Window

Key Points

If a token is the unit, the context window is the container — the total number of tokens a model can consider in a single request. It covers everything: system prompt, few-shot examples, tool definitions, user input, conversation history, retrieved documents, and the model's response. It is one shared token budget.

The single most overlooked fact: the window is shared between input and output. If your model has a 128K window and your prompt uses 120K, the model can generate only ~8K in response. The parameter max output tokens (often max_tokens) is how you explicitly reserve part of the window for the reply. Fail to reserve enough and generation is simply cut off when the window fills — the assistant stops mid-sentence, mid-list, or mid-JSON.

graph TD A["Context window: fixed total, e.g. 128K tokens"] --> B["Input tokens: prompt + history + tools + retrieved docs"] A --> C["Output tokens: the model's generated reply"] B --> D{"Do input + output fit together?"} C --> D D -->|"Yes: output space reserved"| E["Complete answer"] D -->|"No: input fills the window"| F["Generation cut off mid-sentence"]
Animation slot: the "one truck" metaphor — input fills a fixed bar, output stays roped off

Model Context Limits Across Providers

Window sizeGood forTypical use in an assistant
8K / 32KSmall tasks and short documentsSingle-turn Q&A, short chats, quick tool calls
128KMost real-world structured inputs after preprocessingMulti-turn conversations with retrieved memory and tools
1M (emerging 2025)Entire books, massive datasets, multi-document workflowsDeep research assistants, whole-codebase reasoning

For a hyper-personalized assistant, the 128K tier is the sweet spot: it holds a rich system prompt, a page of user memory, tool definitions, and a healthy stretch of history, with room reserved for a substantial reply. But the headline number is not what you build against — models advertising 200K windows show measurable quality degradation around 130K tokens. There are two costs, neither of which crashes: money (every token is billed, and agents make many calls carrying full context) and quality (examined next). The advertised window is a ceiling, not a target.

Post-Reading Check — The Context Window

4. What does the context window include?

5. On a 128K-token window, your prompt already uses 120K tokens. What happens?

6. A model advertises a 200K-token context window. How should you treat that number?

Pre-Reading Check — Building a Token Budget

7. When building a token budget, what should you allocate first?

8. Among the input zones (system prompt, tools, memory/retrieval, conversation history), which one grows without bound as a session continues?

9. Why leave "headroom" and set an effective budget below the advertised window?

3. Building a Token Budget

Key Points

Knowing the window is shared, the engineer's job is allocation — deciding in advance how many tokens each part may consume. Budgeting starts from the end: reserve the response first. If your assistant sometimes produces a 1,500-token summary, reserve at least that plus a margin as max output tokens, and everything else fits in what remains.

With output reserved, distribute the rest across the input zones. Because history is the zone that grows, it is the one you actively manage — via summarization (compress older turns), priority-based eviction (drop the oldest/least relevant first), and staged loading (bring detailed guidance into context only when a matching task appears). A budget summing to exactly 100% will overflow, so leave headroom and set your effective window below the maximum.

Worked Example: A Budget on a 128K Model

Advertised window: 128,000 tokens. Effective operating budget: 110,000 tokens (~18K top-level headroom against quality degradation). Allocate within 110K:

ZoneAllocation (tokens)Notes
Reserved output (max output tokens)4,000Roped off first; enough for a long, structured reply
System prompt1,500Fixed instructions, persona, rules
Tool / function definitions3,000Schemas for several actions
Long-term memory + retrieved docs8,000User profile, preferences, RAG results
Conversation history90,000Managed via summarization + oldest-first eviction
Safety margin (headroom)3,500Absorbs variance in user input and tool returns
Total110,000Fits inside the 110K effective budget

Note the two-layer safety design: the ~18K gap between the 128K window and the 110K effective budget guards against quality degradation, while the 3,500-token margin inside the budget guards against input variance. The response is reserved before anything else; the growing zone is the one you manage; you never spend to the ceiling.

graph TD A["Advertised window: 128,000 tokens"] --> B["Top-level headroom: ~18,000 tokens against quality degradation"] A --> C["Effective operating budget: 110,000 tokens"] C --> D["Reserved output: 4,000 (roped off first)"] C --> E["System prompt: 1,500"] C --> F["Tool / function definitions: 3,000"] C --> G["Long-term memory + retrieved docs: 8,000"] C --> H["Conversation history: 90,000 (actively managed)"] C --> I["Safety margin: 3,500 for input variance"]
Animation slot: interactive token-budget allocator with overflow warning
Post-Reading Check — Building a Token Budget

7. When building a token budget, what should you allocate first?

8. Among the input zones (system prompt, tools, memory/retrieval, conversation history), which one grows without bound as a session continues?

9. Why leave "headroom" and set an effective budget below the advertised window?

Pre-Reading Check — Context Quality Effects

10. What does the "lost in the middle" phenomenon describe?

11. What shape does model accuracy follow as relevant information moves from the start to the end of the context?

12. Given the U-shaped attention bias, how should you arrange retrieved documents by relevance?

4. Context Quality Effects

Key Points

A larger window does not guarantee better quality; how you order and place content matters just as much as whether it fits. The best-documented failure is lost in the middle: models support million-token windows yet struggle to use information located in the middle of long contexts — a direct problem for RAG systems that depend on the model reading retrieved documents.

The foundational study is Liu et al. (2024), "Lost in the Middle," in TACL. Testing multi-document QA and key-value retrieval, they repositioned the answer-containing document among distractors and measured a 30%+ accuracy drop when it moved from position 1 to position 10 in a 20-document context — showing models do not robustly use information across all positions.

The pattern is a U-shaped performance curve: highest accuracy when relevant info is at the beginning (primacy) or end (recency), sagging in the center. The mechanism — shown by MIT and Google Cloud AI researchers — is a U-shaped attention bias: start and end tokens receive disproportionately strong attention, the middle receives less.

flowchart TD A["Relevant info at START of context"] --> B["High accuracy (primacy bias)"] C["Relevant info in MIDDLE of context"] --> D["Accuracy trough: 30%+ drop (Liu et al., 2024)"] E["Relevant info at END of context"] --> F["High accuracy (recency bias)"] B -.-> D D -.-> F B --> G["Design rule: place best content at the edges"] F --> G
Animation slot: draggable U-shaped accuracy-vs-position curve

Signal-to-Noise in a Full Window

Because performance is best at the edges and worst in the middle, the most effective approach places the highest-ranked content at the beginning and end, leaving lower-ranked material in the middle. With ten ranked documents, you do not concatenate top-to-bottom — you interleave so your two best sit at the two edges. Every irrelevant token is noise: it dilutes the signal and pushes important content into the low-attention middle. For a personalized assistant, retrieving more memories can make answers worse if it buries the single most relevant fact. Curate ruthlessly, rank carefully, place your best at the edges.

Animation slot: edge-placement vs ranked-order arrangement of retrieved documents
Post-Reading Check — Context Quality Effects

10. What does the "lost in the middle" phenomenon describe?

11. What shape does model accuracy follow as relevant information moves from the start to the end of the context?

12. Given the U-shaped attention bias, how should you arrange retrieved documents by relevance?

Your Progress

Answer Explanations