Resources/Model training/Tokens and context

Concept

Context window

The maximum number of tokens a model can take into account at once, how that space is shared, and how it differs from the per-example limit that applies during training.

Chapter 23 min read4 references

What it is

The context window is the model's working memory for a single request: the maximum number of tokens it can take into account at once. It is shared by everything — the system prompt, every earlier turn of the conversation, any documents placed in the prompt, the new message, and the answer the model is writing. Anything that does not fit is simply not seen.

Windows have grown quickly, from a couple of thousand tokens in early models to hundreds of thousands, and around a million in some current ones. The size is fixed by how the model was built and trained; fine-tuning does not change it.

ONE REQUESTcontext windowsystemearlier turnsnew messageanswerfreeTRAINING RECORD · BEFOREsysturn 1turn 2turn 3targetAFTER TRIMMINGsysturn 2turn 3targetper-example limitoldest dropped
Top: one request. The system prompt, the history, the new message and the answer all share one window. Bottom: a training record over the per-example limit loses its oldest exchange, keeping the system prompt, the latest context and the target.

How it works

A transformer reads its input in parallel and, at every layer, lets each token look back at every earlier token through a mechanism called attention. That is what allows an answer to depend on a detail from far back in the conversation. It is also why long contexts are expensive: in standard attention the computation grows with the square of the length, and the memory used to keep the conversation in view while generating grows with every token.

The answer counts too. If a model has a 128,000-token window and the input is 126,000 tokens long, only 2,000 tokens are left for the reply. Most APIs also cap the length of the output separately.

Bigger is not the same as better

A model can accept a long input without making good use of all of it. Studies of long contexts find that models use information at the start and end of the input more reliably than information buried in the middle, and that many models advertising very long windows lose accuracy well before their stated limit. For training and for use alike, a short, relevant context usually does better than a long, padded one.

The per-example training limit

Training adds a second limit: the per-example token limit, the longest single record a provider will train on. It is often smaller than the model's context window, because training on a long record needs far more memory than answering one. A record over the limit has to be shortened or left out.

Fitting a long support conversation
  1. A record has a 300-token system prompt, 14 earlier exchanges of about 600 tokens each, and a final question plus target reply of 400 tokens: 300 + 8,400 + 400 = 9,100 tokens.
  2. The provider's per-example limit for this model is 8,192 tokens, so the record is 908 tokens too long.
  3. Trimming removes the oldest exchanges first. Dropping two of them (about 1,200 tokens) brings the record to roughly 7,900 tokens.
  4. The system prompt, the most recent twelve exchanges and the target reply are kept, so the model still learns the reply in the situation that produced it.

Common mistakes

  • Assuming that a model with a large context window will train on equally long records.
  • Forgetting that the answer has to fit in the window alongside the input.
  • Filling the prompt with every available document on the theory that more context is always better.
  • Dropping long conversations outright when trimming the oldest turns would have kept a useful target.

References and further reading

External sources open in a new tab.

  1. 01Lost in the Middle: How Language Models Use Long ContextsLiu et al., 2023 · arXiv · arxiv.orgShows that information in the middle of a long input is used least reliably.
  2. 02RULER: What's the Real Context Size of Your Long-Context Language Models?Hsieh et al., 2024 · arXiv · arxiv.orgMeasures how far below their advertised windows models stay reliable.
  3. 03Attention Is All You NeedVaswani et al., 2017 · arXiv · arxiv.orgThe transformer paper, where attention over the whole context comes from.
  4. 04Context windowsClaude Platform Docs · platform.claude.comA provider's explanation of how input and output share the window.

Turn weeks of expert work into minutes

See how Scalata fits your team, your data, and your controls.