Skip to content
MaximumAI
← All articles
context windowtokensarchitecture

Context Window Explained: What It Means in Practice

What an LLM context window is, what counts against it, why bigger isn't automatically better, and practical ways to budget and manage context in real applications.

Model spec sheets love to advertise context windows: 32K, 128K, a million tokens. But what does that number actually mean for the application you’re building? This guide explains the concept, what fills the window, where the practical limits are, and how to plan a context budget.

What is a context window?

The context window is the maximum number of tokens a model can handle in a single request. It is the model’s working memory for that call: everything it can “see” while generating an answer.

Two points trip people up:

  1. It usually covers input and output together. If a model has a 128K-token window and you send 120K tokens of input, there are at most 8K tokens left for the answer — and the provider may impose a separate, smaller cap on output length.
  2. The model has no memory between requests. A chatbot that “remembers” earlier messages does so because the application resends the conversation each time. That history occupies the window — and costs input tokens — on every turn.

What counts against the window

Everything you send, plus everything the model produces:

  • System prompt and instructions
  • Few-shot examples
  • Tool or function definitions
  • Retrieved documents (in RAG systems)
  • Conversation history
  • The user’s current message
  • Attached images or files (converted to tokens)
  • The model’s output, including any reasoning tokens the model generates before the visible answer, where applicable

A rough budget for a document-Q&A request might look like this:

ComponentTokens (example)
System prompt + instructions1,000
Tool definitions500
Retrieved passages6,000
Conversation history2,500
User question150
Reserved for answer1,500
Total11,650

These are illustrative numbers. Measure your own with the provider’s tokenizer.

What happens when you exceed it

Behaviour depends on the API and your code, but typically one of these happens:

  • The API rejects the request with an error saying the input is too long.
  • Your framework or SDK silently truncates something — often the oldest messages — which can remove important instructions or facts.
  • The output gets cut off because the model hit its output cap or the remaining window, usually signalled by a finish/stop reason such as “length”.

Silent truncation is the dangerous one. Always check the stop reason in the response, and log token usage so you can see when requests approach the limit.

Bigger isn’t automatically better

A large window is genuinely useful: you can include whole contracts, long transcripts or large code files without building a retrieval pipeline. But there are trade-offs.

Cost scales with what you send

You pay for every input token on every request. Stuffing 100K tokens of “just in case” context into each call can cost dramatically more than retrieving the 5K tokens that matter. Some providers also price very long requests at a higher tier — check the pricing page for your model.

Latency grows with input size

The model must process the whole input before it produces the first output token. Very long prompts generally mean slower first responses.

Using the context is not the same as fitting it

Research and practitioner experience have repeatedly shown that models can be less reliable at using information buried in the middle of a very long context than information near the beginning or end. Newer models have improved, but the safe assumption is: a fact being present in the context doesn’t guarantee the model will use it correctly. Test retrieval of specific details from long inputs as part of your own evaluation.

Advertised vs effective context

The advertised limit is what the API accepts. The effective limit for your task — where quality stays acceptable — may be lower. You only find it by testing at different lengths.

Practical strategies

1. Write a context budget

Decide upfront how many tokens each component may use: for example, instructions 1K, retrieved documents up to 8K, history up to 4K, output 1.5K. Enforce it in code. A budget turns “the prompt got too long” from a mystery into a configuration value.

2. Retrieve instead of stuffing

For large knowledge bases, retrieval-augmented generation (RAG) selects the most relevant passages for each question. It keeps requests smaller, cheaper and often more accurate. Full-document context still makes sense when the whole document matters, such as summarising a single contract.

3. Manage conversation history

Options, from simplest to most involved:

  • Sliding window: keep only the last N turns.
  • Summarise older turns into a short running summary.
  • Pin essentials: keep the system prompt and key facts fixed; trim everything else first.

4. Put instructions where they’ll be followed

A common pattern for long inputs is to place the documents first and the specific question or task instructions at the end, close to where generation begins. Some providers recommend this explicitly for long-context prompts. Clear delimiters (such as XML-style tags or headings) between documents also help the model keep sources apart.

5. Reserve room for the answer

Set a maximum output length that fits your use case, and make sure input + max output stays under the window with margin.

6. Use caching for repeated prefixes

If many requests share the same long prefix — a big system prompt or a reference document — prompt caching (where your provider supports it) can reduce cost and latency for that repeated part. More on this in how to cut LLM API costs.

A quick fit check

Before choosing a model, compute your worst realistic request:

worst_case = system + tools + max_retrieved + max_history + longest_user_input + max_output
headroom   = context_window − worst_case

If headroom is negative, either the model is ruled out or you need to cut the budget. If it’s positive but thin, expect occasional failures in production as inputs vary. Aim for comfortable headroom rather than an exact fit.

Key takeaways

  • The context window is a per-request limit covering input and (usually) output.
  • Everything you send counts, and history is resent every turn.
  • More context costs more, can slow responses, and doesn’t guarantee better answers.
  • Budget your context explicitly and test quality at realistic lengths.

When comparing models, context window is one column among many. MaximumAI will include a context planner that checks whether your real request sizes fit each model with room to spare.