LLM Context Window Explained: Tokens, Limits and How to Fit More, Measured
An LLM context window is the total number of tokens a model can read and write in one request. Learn what fills it, how words turn into tokens, how to budget input and output, and how to trim chat history safely, with counts we measured.
- The LLM context window is a token limit shared by everything: system prompt, history, documents and the answer.
- Tokens are not words. Our English prose used 1.14 tokens per word, Python code 2.22, and Hindi text 1.41 with one tokenizer but about 4.6 with an older one.
- Chat history grows every turn. Trim the oldest messages but always keep the system prompt; naive cutting dropped it in our test.
- A large window is not a free pass. Long prompts cost more, run slower, and can bury the detail that matters.
You paste a long contract into a chatbot and it answers as if the last ten pages never existed. Or a support bot slowly “forgets” the rules you gave it. Both come down to the LLM context window: the fixed number of tokens a large language model can work with in one request. Knowing how it fills up helps you build AI features that stay accurate and affordable.
We measured the numbers below with OpenAI’s open-source tiktoken library, version 0.14.0, using its o200k_base and older cl100k_base encodings on Python 3.13. Other model families use other tokenizers, so treat our counts as realistic examples, not exact figures for every model. For the basics of tokens, see our LLM tokens guide.
What is a context window?
A model does not remember earlier requests on its own. Each time you call it, you send text, and the model reads all of it before generating a reply. The context window is the maximum size of that combined text, measured in tokens. If the window is 128,000 tokens, the input and the output together must fit inside it. Our guide to what a large language model is explains why models work this way.
Window sizes range from a few thousand tokens on small models to hundreds of thousands, or more than a million, on large ones. Sizes change with each release, so check your provider’s documentation for the exact limit and for any separate cap on output tokens.
What fills the context window
- System prompt: instructions, rules and persona.
- Conversation history: every earlier user and assistant message you resend.
- Retrieved or pasted content: documents, search results, code files.
- Tool definitions and results: function schemas and the data they return.
- The answer itself: output tokens come out of the same budget.
That last point surprises many people. A prompt that uses almost the whole window leaves no room for a reply, so the answer is cut short.
Words vs tokens: what we measured
We repeated three short texts 20 times each and counted words and tokens:
| Text | Words | o200k tokens | Tokens per word | cl100k tokens |
|---|---|---|---|---|
| English prose | 740 | 841 | 1.14 | 841 |
| Python code | 180 | 400 | 2.22 | 400 |
| Hindi text | 300 | 422 | 1.41 | 1,381 |
Three lessons stand out. Plain English is close to one token per word. Code uses roughly twice as many tokens per “word”, because symbols, indentation and short names each take tokens. And language matters hugely: the same Hindi text took 422 tokens with the newer tokenizer and 1,381 with the older one, more than three times as many. A window that holds a long English document may hold far less in another language.
import tiktoken
enc = tiktoken.get_encoding("o200k_base")
print(len(enc.encode("Your text here")))
Tip: never estimate tokens by dividing characters by four for production limits. Count with the tokenizer for your model, then keep a safety margin.
Budgeting a request
We planned a request with a 600-token system prompt, 4,200 tokens of chat history and 25,000 tokens of documents, then checked it against three window sizes, reserving 2,000 tokens for the answer:
window 8000: input 29800 tokens, left -21800, room for 2,000-token answer: False
window 32000: input 29800 tokens, left 2200, room for 2,000-token answer: True
window 128000: input 29800 tokens, left 98200, room for 2,000-token answer: True
The 8K window could not take the request at all. The 32K window fit, but with only 2,200 tokens spare, so one more document would squeeze the answer. Write this check into your code before every call: count the parts, subtract a reserved output budget, and decide what to cut if it does not fit.
What happens when you go over
Behaviour depends on the API and app. Some APIs reject the request with an error. Some chat apps silently drop the oldest messages. Some truncate documents. And if the input fits but leaves too little room, the answer stops mid-sentence when it hits the output limit. None of these are obvious to users, which is why the model can seem to “forget” or ignore part of a document.
Chat history grows every turn
Chat apps resend the whole conversation each turn so the model can see it. At about 350 tokens per turn:
chat of 10 turns x 350 tokens = 3500 tokens
chat of 50 turns x 350 tokens = 17500 tokens
chat of 200 turns x 350 tokens = 70000 tokens
A long support chat can quietly reach tens of thousands of tokens, and every new message pays for all the old ones again.
Trimming history without losing the rules
We built a 31-message chat of 1,455 tokens and trimmed it to an 800-token limit in two ways. The smart way kept the system prompt, then added messages from newest to oldest until the budget was full. The naive way just kept the last 800 tokens of the joined text.
history: 31 messages, 1455 tokens -> kept 17 (783 tokens), system kept: True, oldest kept: 'message 14'
naive tail-truncation keeps system prompt: False
The naive version dropped the system prompt, which held the instruction “Always answer in English”. That is exactly how a bot loses its rules in long chats. Better strategies, roughly in order of effort:
- Always pin the system prompt and the latest user message.
- Drop or summarise the oldest turns first.
- Replace old turns with a short running summary written by the model.
- Store facts outside the chat and retrieve only what the current question needs.
Is a bigger context window always better?
No. A large window lets you include more, but research such as the 2023 “Lost in the Middle” study by Liu and colleagues found that models used information at the start and end of long inputs more reliably than information in the middle. We did not test that ourselves. In practice, sending only the relevant parts often beats sending everything, and it reduces hallucinations caused by distracting or conflicting text. Put key instructions near the start and restate the actual question at the end.
Chunking and retrieval (RAG)
Retrieval-augmented generation keeps documents outside the prompt. You split them into chunks, find the chunks related to a question, and send only those. We split a 4,201-token document into chunks of at most 500 tokens with a 50-token overlap and got 10 chunks. The overlap keeps sentences that cross a boundary readable in at least one chunk.
def chunks(text, size, overlap):
ids = enc.encode(text)
step = size - overlap
return [enc.decode(ids[i:i + size]) for i in range(0, max(1, len(ids) - overlap), step)]
Chunks are matched to questions using embeddings stored in a vector index. Our what is RAG guide walks through the full pipeline.
Cost and speed
Most APIs charge per input and output token, and longer prompts take longer to process. A 100,000-token prompt sent 1,000 times a day is 100,000,000 input tokens daily, even if the answers are short. Trimming history, retrieving instead of pasting, and caching repeated prompt prefixes where the provider supports it can cut cost and latency sharply. Check current prices on your provider’s pricing page, as they change often.
Testing context limits in your app
- Test with the longest realistic input, not a short sample.
- Test long chats: does the bot still follow the system prompt after 50 turns?
- Test non-English input, which may use far more tokens.
- Assert that your code reserves output tokens and handles over-limit errors gracefully.
- Check that trimming never removes safety rules, which also matters for prompt injection defences.
Final thoughts
The LLM context window is a shared token budget, not a memory. Count tokens with the right tokenizer, reserve space for the answer, pin your system prompt when trimming history, and retrieve relevant chunks rather than pasting everything. Do that and even a modest window goes a long way, while a huge one stays fast and affordable. Clear prompts help too, as our prompt engineering guide shows.
Frequently asked questions
What is a context window in an LLM?
It is the maximum number of tokens a model can handle in one request, covering the system prompt, conversation history, added documents and the generated answer together.
How many words fit in a context window?
It depends on the text and tokenizer. Our English prose used about 1.14 tokens per word, so 100,000 tokens is roughly 88,000 words, but code and many other languages use more tokens per word.
What happens when you exceed the context window?
The API may reject the request, the app may drop older messages or truncate documents, or the answer may be cut off because no output tokens are left.
Does the output count toward the context window?
Yes. Input and output share the same window, so always reserve enough tokens for the reply. Many APIs also have a separate maximum for output tokens.
Is a bigger context window better than RAG?
Not always. Large windows cost more and models can miss details in the middle of long inputs. Retrieving only relevant chunks is often cheaper and more accurate.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.