DevKits
Concept

LLM Context Window Explained — Tokens, Limits & How to Stay Under

A context window is the maximum number of tokens an LLM can consider at once — your entire prompt plus the response it generates must fit inside it. Modern models range from 8k to 1M+ tokens. This guide explains what the context window is, how it differs from the max output limit, what happens when you exceed it, and practical strategies (chunking, summarization, RAG) to stay within it.

Last updated:

Core Concepts

What the context window is
The context window is the model's working memory, measured in tokens. Everything the model reads and writes in a single call — system prompt, conversation history, retrieved documents, the user's message, and the generated completion — has to fit inside it. A 128k-token window holds roughly 96,000 words total. When a model is described as '200k context', that's the combined budget for input and output, not input alone.
Context window vs max output tokens
These are two different limits people often conflate. The context window is the total token budget. max_tokens (or max_completion_tokens) caps only how many tokens the model may generate in the response. If a model has a 128k context window and you fill 120k with input, only ~8k remain for output no matter what you set max_tokens to. Always leave headroom for the completion.
What happens when you exceed it
Go over the context window and the API returns an error (OpenAI: context_length_exceeded; others return a 400-class error). Some chat frameworks silently drop the oldest messages instead, which can make the model 'forget' earlier instructions. Neither is good: an error breaks the request, and silent truncation causes subtle, hard-to-debug behaviour. Counting tokens before sending prevents both.
Strategies to stay under the limit
Three common techniques: (1) Chunking — split long documents into pieces and process them separately. (2) Summarization — compress earlier conversation turns or documents into a shorter summary that preserves key facts. (3) Retrieval-Augmented Generation (RAG) — instead of stuffing everything into the prompt, embed your corpus and retrieve only the few most relevant chunks per query. RAG scales to arbitrarily large knowledge bases while keeping each request small and cheap.

Frequently Asked Questions

Does the context window include the response?

Yes. The context window is the combined budget for input (prompt, history, retrieved context) and output (the generated completion). If you fill most of it with input, little room remains for the response — so always reserve tokens for the output you expect.

What's the difference between context window and max_tokens?

The context window is the total token capacity of the model. max_tokens only limits how long the generated response can be. The response still has to fit in whatever context-window space your input didn't already use, so max_tokens can't exceed the remaining budget.

How do I handle documents bigger than the context window?

Don't put everything in the prompt. Chunk the document, embed the chunks, and use retrieval (RAG) to pull only the most relevant pieces per query. For long conversations, periodically summarize older turns into a compact summary instead of sending the full history.

Try these related tools