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
Context Window Checker →
Check whether your prompt fits a model's context window before you send it. Estimates prompt tokens, reserves room for the response, and shows remaining headroom across GPT-4o, o1, Claude, Gemini, DeepSeek and more. 100% local.
Token Counter →
Count tokens in your prompt for GPT-4, GPT-3.5, Claude, and Gemini and predict API cost before you spend. Free, no signup — all counting runs locally, your prompt stays in your browser.
Words to Tokens →
Convert words to tokens (and tokens to words) for GPT-4o, Claude, and Gemini. Estimate from a word count or paste text for an exact count with OpenAI's tokenizer. 100% local.
Text Chunker →
Split long text into overlapping chunks for embedding and RAG pipelines. Preview recursive, paragraph, sentence, or fixed strategies with live token counts. Runs 100% in your browser.
Prompt Token Breakdown →
Break a prompt into segments and see how many tokens — and how much cost — each part uses. Find the heaviest section (system, few-shot examples, or user input) so you know exactly what to trim. 100% local.
AI Cost Calculator →
Estimate monthly LLM API cost across GPT-4o, Claude 3.5, Gemini, DeepSeek and 100+ models. Compare providers side-by-side with prompt-caching and batch discounts. Free, no signup — prices refreshed daily from the public LiteLLM catalog.