Ask any team that has shipped an AI feature what surprised them most, and "the context window" will be high on the list. The demo works perfectly with a two-page document. Then a customer uploads a 400-page contract, or a support chat runs for three hours, and the system starts forgetting things, slowing down or throwing errors.
The context window is the single most important constraint in LLM application design. This article explains what it is, how it fills up, what happens at the edges and the patterns experienced teams use to work within it.
What a context window is
A context window is the maximum number of tokens a model can process in a single request. It includes everything:
- the system prompt
- tool definitions, if you use tool calling
- the full conversation history you send
- any documents or retrieved text you paste in
- the model's own output, including any reasoning or thinking tokens
If the total exceeds the limit, the request fails or something has to be cut.
The most important thing to understand is that an LLM has no memory between requests. When a chatbot seems to remember what you said an hour ago, it is because the application resends that history every time. The context window is the model's entire working memory for that one call.
How big are context windows today
Context windows have grown dramatically. Early GPT-3 era models handled around 2,000 to 4,000 tokens. Today, mainstream models from Anthropic, OpenAI, Google and xAI commonly offer windows in the hundreds of thousands of tokens, and several current models, including recent Claude models, support around one million tokens.
One million tokens is roughly 750,000 English words, several long novels, or a medium-sized codebase. Output is limited separately; a model might accept a million tokens of input but produce at most a fraction of that in one response.
Because these numbers change with every model release, do not hard-code them from memory. Many APIs expose limits programmatically. Anthropic's Models API, for example, returns the maximum input and output token values for each model, so your code can adapt when you upgrade.
Measuring how much of the window you use
Developers often underestimate token usage. A few rules of thumb:
- English prose is about 0.75 words per token.
- Source code is denser in tokens than prose because of symbols, indentation and identifiers.
- Many non-English languages, including Hindi, use more tokens per word.
- JSON with long key names adds up quickly.
- Tool definitions are sent with every request and can quietly consume thousands of tokens.
Most providers offer a token-counting endpoint or library. Use it during development to log the size of real prompts. Teams are often surprised to find that the system prompt plus tool schemas take up more space than the user's actual question.
Why bigger is not automatically better
A large context window is a capability, not a design strategy. Filling it has real costs.
Cost
You pay for every input token on every request. If your chat app resends a 200,000-token history each turn, a 20-turn conversation processes millions of tokens. Prompt caching reduces this significantly when the beginning of the prompt stays stable, but it does not make it free.
Latency
More input means more computation before the first output token appears. Very long prompts can add noticeable seconds to response time.
Attention quality
Models are generally good at finding information anywhere in long contexts, and providers test this heavily. But in real use, performance can drop when the relevant detail is a small piece buried among large amounts of irrelevant text, especially when the task requires combining several scattered facts. Research on long-context models has repeatedly found that position and noise matter.
The practical lesson: put the most relevant material in, leave the irrelevant material out, and place key instructions where the model will clearly see them.
What happens when you hit the limit
Different systems handle overflow differently:
- The API rejects the request. You get an error saying the prompt is too long. Your code must handle this.
- The output is cut short. If input plus requested output exceeds the limit, or the output hits the max-tokens setting, the response stops mid-sentence. Check the stop reason field in the response; a value indicating the max token limit means the answer is incomplete.
- The application truncates silently. Some chat frameworks drop the oldest messages without telling anyone. This is where "the bot forgot what I said" complaints come from.
Silent truncation is the most dangerous, because nothing looks broken.
1. Sliding window of recent messages
Keep only the last N turns of conversation. Simple and cheap, but the model loses early context, such as the user's name or the original goal.
2. Summarise and compress
When the conversation grows past a threshold, ask the model to summarise older turns into a short paragraph and replace them with that summary. Keep the most recent turns verbatim.
A good summary preserves:
- the user's goal
- decisions already made
- important facts such as names, numbers and constraints
- open questions
Many agent frameworks, including Claude Code, have a compaction feature that does exactly this when a session gets long.
3. Retrieval augmented generation
Instead of pasting an entire knowledge base into the prompt, store documents in a searchable index and retrieve only the passages relevant to the current question. This is the core idea of RAG, which we cover in depth in a separate article on building RAG from scratch.
RAG keeps prompts small, makes answers easier to verify and lets your knowledge base grow far beyond any context window.
4. Chunking and map-reduce for long documents
For tasks that genuinely need the whole document, such as summarising a 1,000-page report, split the document into chunks, process each chunk separately (the map step), then combine the partial results (the reduce step).
This scales indefinitely and can run in parallel. The trade-off is that relationships across chunks can be missed, so overlap chunks slightly and include a short running summary.
5. Structured memory outside the model
For long-running assistants, store important facts in a database: user preferences, project details, previous decisions. Retrieve and inject only the facts relevant to the current request. This mirrors how a human uses notes instead of memorising everything.
6. Trim tool definitions
If an agent has 50 tools, every request carries 50 schemas. Consider loading tools dynamically based on the task, shortening descriptions, or grouping related actions into a single tool with a parameter.
A simple token budget
A practical approach is to set an explicit budget for each part of the prompt. For a 200,000-token model, you might allocate:
- system prompt and instructions, about 3,000 tokens
- tool definitions, about 5,000 tokens
- retrieved documents, up to 40,000 tokens
- conversation history, up to 30,000 tokens
- reserved for output, 16,000 tokens
Code enforces each budget. When retrieved documents exceed their share, keep the highest-ranked ones. When history exceeds its share, summarise.
Here is a minimal Python sketch of the history-trimming logic:
def trim_history(messages, count_tokens, budget):
kept, total = [], 0
for msg in reversed(messages):
size = count_tokens(msg)
if total + size > budget:
break
kept.append(msg)
total += size
return list(reversed(kept))
In a real system you would replace the dropped messages with a summary rather than discarding them completely.
Context window and prompt caching work together
If you send the same long prefix many times, such as a big system prompt, a set of tools or a reference document, prompt caching lets the provider reuse the processed prefix. With Claude, cached reads are billed at a small fraction of the normal input price, and latency drops as well.
To benefit, keep the stable parts at the start of the prompt and the changing parts, such as the latest user message, at the end. Our article on prompt caching in Claude explains the details.
Common mistakes
- Sending the whole conversation forever. Costs climb with every turn.
- Ignoring the stop reason. Truncated answers get shown to users as if complete.
- Assuming the model read everything equally well. Test with your own long documents.
- Hard-coding limits. Model upgrades change them.
- Forgetting that output counts. Leave room for the answer.
- Not counting tool schemas. They are part of every request.
Testing long-context behaviour
Before launch, build a few test cases that stress context:
1. A long document with a key fact near the middle. Ask a question that needs that fact.
2. A long conversation where the user states a constraint early and the final request depends on it.
3. A prompt that approaches your configured budget, to confirm trimming works.
4. A request whose answer would exceed max tokens, to confirm you detect truncation.
Run these tests whenever you change models or prompts.
Key takeaways
- The context window is the model's entire working memory for one request.
- Everything counts: system prompt, tools, history, documents and output.
- Bigger windows help, but cost, latency and focus still matter.
- Use summarisation, retrieval, chunking and external memory to scale beyond the window.
- Budget tokens deliberately and test the edges.
Context limits are not going away, even as windows grow. Teams that design for them from day one ship AI features that stay fast, affordable and reliable as usage grows.
Related articles on MindStick
- How Large Language Models Actually Work, From Tokens to Answers
- Retrieval Augmented Generation From Scratch, A Practical Architecture
- Prompt Caching in Claude and How It Cuts Cost
- Handling Long Documents When You Hit Context Limits
Context window sizes, pricing and API features differ between models and change frequently. Check your provider's current documentation before designing around specific numbers.
Leave a Comment