What Is a Context Window? Limits, Overflow and Long-Context Costs

A context window is the maximum number of tokens a language model can work with in a single request, counting both what you send and what it writes back. If the conversation, documents and instructions you send plus the reply would exceed it, the request is rejected or the reply is cut off.

Context windows are measured in tokens, not words. If you need the conversion, how many tokens a word is covers it.

What counts toward the context window

Anthropic's docs define the context window as "all the text a language model can reference when generating a response, including the response itself", and describe it as the model's working memory, as opposed to the data it was trained on. OpenAI's docs say the same thing in different words: the window is the maximum number of tokens usable in one request, and the total "includes input, output, and reasoning tokens."

In a typical chat API request, all of these count:

  • The system prompt (or developer message).
  • Every earlier message in the conversation that you send back, user and assistant.
  • Documents, images and tool results included in messages.
  • Tool definitions, which are sent as text the model has to read.
  • The new reply, including any hidden reasoning or "thinking" tokens the model generates before answering.

The last point surprises people. A reasoning model can spend thousands of tokens thinking before it writes a short answer, and those tokens take up window space and are billed as output.

How input and output share the window

Think of the window as one budget. Whatever the input uses is no longer available for the output. As a worked example with a 200,000-token window:

Part of the requestTokens
System prompt2,000
Tool definitions3,000
Conversation history (20 turns)45,000
Attached contract (PDF text)120,000
Total input170,000
Room left for reasoning and reply30,000

Each new turn makes this worse. A chat API is stateless: to continue a conversation you send the whole history again, so turn 21 contains turns 1 to 20 plus the new message. Anthropic's docs describe this as "progressive token accumulation", with each response becoming part of the input for the next turn.

Some APIs use a separate budget instead. Google's Gemini docs say that on Gemini 3 and 2.5 models, input (1,048,576 tokens) and output (65,536 tokens) have separate limits, while on Gemini 4 input, thinking and output share a single 1,048,576-token window (both as of October 2026). Check which model you are on.

Context window sizes (as of October 2026)

These figures come from each vendor's documentation on the date above and change often. Treat them as a snapshot, not a reference.

Vendor / modelContext windowOutput limit per request
Anthropic: current Claude models (Opus 5.5, Sonnet 5.5, Haiku 5.5 and others listed in the docs)1M tokens128k tokens
Anthropic: other Claude models, including Sonnet 4.5200k tokensVaries by model
Google: Gemini 41,048,576 tokens shared by input, thinking and output65,536 by default, raisable
Google: Gemini 3 and 2.51,048,576 input tokens65,536 tokens (separate budget)
OpenAI: gpt-4o-2024-08-06 (the example OpenAI's guide uses)128k tokens16,384 tokens
Ollama (local), default setting4k if under 24 GiB VRAM; 32k for 24 to 48 GiB; 256k at 48 GiB or moreConfigurable

The Ollama row is a default, not a model limit. Ollama picks a context size from your GPU memory, because a larger window needs more memory. That trade-off is covered in how much VRAM you need to run an LLM locally.

What happens when you exceed the context window

It depends on the vendor and on whether the input alone or input plus output is too large.

  • Claude API, input too large: a 400 invalid_request_error with the message "prompt is too long", on every model.
  • Claude API, input fits but input plus max_tokens does not: on Claude 4.5 models and newer, the request is accepted, and if generation reaches the limit it stops with stop_reason: "model_context_window_exceeded". Earlier models return a validation error up front.
  • OpenAI: the conversation-state guide warns that tokens generated beyond the limit "may be truncated in API responses", so you get a reply that stops mid-way.
  • Gemini 4: if input plus max_output_tokens exceeds the window, the output cap is reduced to whatever space remains.
  • Chat apps: products such as ChatGPT or claude.ai usually hide the limit by dropping or summarising older turns, which is why a long chat can "forget" details from the beginning.

To avoid surprises, count tokens before sending. Anthropic and Google both offer a free counting endpoint, and OpenAI's tiktoken library counts locally.

Why long contexts degrade quality

Fitting in the window does not mean the model uses everything well. Anthropic's context-window docs say plainly that "as token count grows, accuracy and recall degrade", and call this context rot. Their engineering article explains one reason: in a transformer every token attends to every other token, which "results in n² pairwise relationships for n tokens", so attention is spread thinner as context grows. Models have also seen fewer very long sequences during training than short ones.

Position matters too. The 2023 paper Lost in the Middle (Liu et al.) tested multi-document question answering and found accuracy was generally highest when the relevant information was at the start or end of the input, and dropped when it sat in the middle, including for models built for long contexts. Newer models have improved, but the practical advice has stuck: put the most important material where it is easy to find, and do not pad the window with things the model does not need.

Why long contexts cost more

APIs bill per token, so a request with 150,000 input tokens costs roughly 150 times as much input as one with 1,000. In a chat, the cost grows with every turn because the history is resent each time. Some vendors also charge a higher rate above a size threshold; Anthropic's docs, for example, say that on Claude Haiku 5.5 prompts over 100,000 tokens cost more, while other current Claude models bill 1M-token requests at standard rates (as of October 2026).

Long inputs are also slower, because the model has to process every input token before producing the first output token. Prompt caching reduces both cost and delay for repeated prefixes, though Anthropic's docs point out that cached tokens still occupy the window: caching "changes what you pay for those tokens, not whether they count."

How to stay within the context window

  • Send only what the task needs. Anthropic's engineering guidance is to find "the smallest set of high-signal tokens" that gets the result. Extract the relevant section of a document instead of attaching the whole file.
  • Summarise old turns. Replace the first 30 messages of a chat with a short summary. Anthropic calls this compaction and offers it server-side for newer Claude models.
  • Clear stale tool output. In agent loops, old search results and file listings pile up quickly.
  • Set max_tokens deliberately. Leave enough room for the reply and any reasoning, but not more than you need.

The full set of techniques, with trade-offs, is in how to reduce AI token usage.

Frequently asked questions

Does a bigger context window mean the model remembers more?
Only within one request. The context window is working memory for the current call, not long-term memory. Chat apps create the impression of memory by resending earlier turns each time, and those turns count toward the window again on every request.
Is the context window the same as the maximum output length?
No. The context window is the total budget for one request. Most APIs also set a separate cap on output tokens per response, which is much smaller than the window. Anthropic's docs, for example, list a 1M-token window and a 128k maximum output for current Claude models as of October 2026.
How many pages fit in a context window?
Convert pages to words, then words to tokens. If your pages hold about 500 words each, Google's rule of thumb of 100 tokens per 60 to 80 English words makes one page roughly 625 to 830 tokens. A 128,000-token window would then hold very roughly 150 to 200 pages before any room is left for the answer. Count the actual text if you are close to the limit.
What happens to old messages in a long chat?
In the API, nothing happens automatically on most platforms: you resend the history and eventually hit the limit. Chat interfaces may drop or summarise older turns for you. Anthropic's docs note that chat products such as claude.ai can manage the window on a rolling first-in, first-out basis, and that the API offers server-side compaction to summarise earlier turns.

Sources