The context window

Treat the context window as a budget with competing claims on it, and decide what to drop before the provider decides for you.

04of 24
≈ 13Min read
5Parts

One number, shared by everything

The window covers the input and the output. A 200 000-token window with a 190 000-token prompt leaves you 10 000 tokens of answer, and many APIs will simply refuse rather than truncate. max_tokens is reserved out of the same total.

So context is not "how much can I send". It is an allocation problem between five claimants, and the interesting engineering is deciding what gives way when they do not all fit.

text
┌──────────────── context window ────────────────┐
│ system │ tools │ retrieved │ history │ question │ ← reply │
└────────────────────────────────────────────────┘
        everything above is input, and the reply comes out of the same budget

The five claims, and what each is worth

History is the one that grows on its own, which is why every chat application needs a strategy for it before it needs anything else.

ClaimTypical sizeDrop it?
System prompt100–2 000Never — it is the behaviour
Tool definitions50–200 per toolYes: send only the tools this step could need
Retrieved contextUsually the largestYes: fewer, better chunks — the retrieval lessons
Conversation historyGrows without limitYes: the main thing to manage
The current questionSmallNever

The four strategies for history

Pin and slide is the pragmatic default: the first exchange usually contains the task, and the last few contain the state. What sits between them is mostly working out. Summarise only when a conversation is genuinely long, and remember that a summary of a summary loses more each time.

StrategyHowLoses
Sliding windowKeep the last N messagesAnything said early — including the user’s actual goal
SummariseReplace old turns with a summaryDetail, and costs a model call; the summary can drift
RetrieveStore the history and fetch the relevant partsNothing in principle; adds a retrieval step and its failure modes
Pin and slideKeep the first turns and the last, drop the middleThe middle — usually the cheapest thing to lose

Longer windows did not end this

Which is the real lesson of this course’s retrieval half: the goal is not to fit everything in. It is to put the right few thousand tokens in, and leave the rest out.

  • Cost is linear in input. A million-token prompt on every turn is a bill, not a design.
  • Latency is linear in input. Time to first token grows with the prompt, on every single call.
  • Attention is not uniform. Models reliably use the beginning and the end of a long context better than the middle — the "lost in the middle" result, and it has held across model generations.
  • Irrelevant context actively hurts. Filling the window with plausible-but-wrong material measurably lowers accuracy. More context is not more information.

Budget it explicitly

Write the allocation down, count against it before the call, and fail loudly when something exceeds its share. An overflow discovered by a 400 from the provider, in production, is the same bug found in the worst possible place.

js
const BUDGET = {
  total: 128000,
  reserveForReply: 4000,
  system: 800,
  tools: 600,
  retrieved: 6000,        // a ceiling, not a target
};

const forHistory = BUDGET.total - BUDGET.reserveForReply
  - BUDGET.system - BUDGET.tools - BUDGET.retrieved;

Practice