What is a context window in an LLM?

A context window is the maximum amount of text, measured in tokens, that a large language model can work with in a single request. It covers the system prompt, history, documents, tool definitions and the model's own output.

Also called context length, context sizeUpdated

A large language model has no memory between requests. Everything it should take into account has to be sent each time: the instructions, the conversation so far, any documents, the tool definitions, and room for its reply. The context window is the upper limit on all of that, counted in tokens rather than words or characters.

If a request is too big, the API rejects it or the application has to drop something. Chat apps usually drop older messages quietly, which is why a long conversation can "forget" what you said at the start.

What counts towards the window

  • the system prompt and instructions
  • the conversation history
  • retrieved passages, pasted documents and images
  • tool or function definitions, and the results of tool calls
  • the model's output, including hidden reasoning tokens on reasoning models

Providers usually also set a separate, lower maximum for output tokens. Window sizes differ between models and change with new releases, so check the vendor's documentation for the exact model you use.

Example: budgeting a support assistant's request

Assume a model with an illustrative 32,000-token window. A support assistant builds each request from these parts:

system prompt and rules                     1,800 tokens
tool definitions (6 tools)                  1,400
conversation history (last 10 turns)        3,200
retrieved help articles (8 chunks x 600)    4,800
user's new message                             90
reserved for the answer                     1,500
----------------------------------------------------
total                                      12,790 tokens

The request fits comfortably. Then a customer pastes a 40-page contract of about 20,000 words. At roughly 0.75 English words per token, that is close to 27,000 tokens, and the request no longer fits. The application has to choose: summarise the contract in parts first, retrieve only the relevant clauses, or trim the history.

Why a bigger window isn't the whole answer

  • Cost and latency grow with input. You pay for every input token on every turn, and long prompts take longer to process. Prompt caching lowers the cost of a repeated prefix.
  • Models use long contexts unevenly. The "Lost in the Middle" study by Liu and colleagues (published in TACL in 2024) found that models did best when the relevant information was at the start or end of the input, and noticeably worse when it sat in the middle.
  • More text means more noise. Irrelevant passages can distract the model and contribute to hallucinations.

This is why retrieval-augmented generation stays useful even with large windows. Sending fewer, better passages is often cheaper and more accurate than sending everything.

Managing the context window

  • Count tokens before sending, with the provider's tokeniser or token-counting endpoint.
  • Trim or summarise history. Keep recent turns in full and replace older ones with a summary.
  • Retrieve rather than paste. Index long documents and send only the relevant chunks.
  • Place key instructions and evidence at the start or end of the prompt.
  • Watch agent runs. Every tool result an AI agent receives is added to its context, so long runs need pruning or summarising.

Our guide to taking an LLM feature from demo to production covers trimming context to control cost and latency.

Any idea in mind? Let's make it real.

Tell us what you're building. You'll hear back from an engineer, not a sales team, usually within one working day.

Book a 30-min call