Context Window
A context window is the amount of tokenized information an LLM can consider in a request. Learn what fills that window, how the model uses it, and how to design applications that keep the right information in view.
By the end of this lesson, you will be able to:
- Explain what an LLM context window represents.
- Identify the different sources of context in a real application.
- Distinguish context capacity from model memory and training data.
- Explain why the position and quality of information can affect answers.
- Build a simple Python context-budgeting experiment.
What Is a Context Window?
The context window is the token capacity available for the information a model processes around a request. It is the model's working input-and-generation space for that interaction.
Key point: A context window is about what is available to the model for the current interaction. It is not the same as the model's training dataset or a permanent memory of everything you have ever said.
What Goes Into the Window?
A production application often builds the context from several sources before calling the model.
Instructions
Rules, role definitions, formatting requirements, and application behavior.
History
Earlier user and assistant messages that are still useful.
Retrieved data
Relevant documents, database records, or RAG chunks.
Tools
Search results, API responses, calculations, and other tool output.
Context Window vs Memory
These ideas are easy to confuse. A context window is the information available to the model in a particular request. Memory is an application-level mechanism that can store information and selectively add it to later requests.
What the model sees now
- Current instructions
- Selected conversation history
- Retrieved information
- Current user request
What your system stores
- User preferences
- Long-term facts
- Past summaries
- Records retrieved when relevant
Why Context Quality Matters
Having more tokens available does not automatically produce a better answer. The model still has to use the information in the context to produce the response.
A large context full of unrelated text can be less useful than a smaller context containing the right evidence.
Does Position Matter?
Information is not equally easy to use simply because it appears somewhere inside a long context. In long prompts, important evidence can become harder for an application to manage, especially when many competing passages are included.
Practical lesson: retrieve and organize the most relevant evidence instead of assuming that adding more text will improve the answer.
Context Window in a Chat Application
Consider a support assistant. A user asks, βCan I return this product?β The application may construct a request from a policy, recent conversation, product information, and the user's current question.
The application does not need the entire company database in the context window. It needs the pieces relevant to this question.
Measure the Context Before Sending It
For a real application, build the context first, count or estimate its tokens with the appropriate tokenizer, and reserve room for the expected output.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("gpt2")
system = "You are a helpful support assistant."
policy = "Returns are accepted within 30 days with proof of purchase."
question = "Can I return this item?"
context = f"{system}\n{policy}\n{question}"
token_count = len(tokenizer.encode(context, add_special_tokens=False))
print("Context tokens:", token_count)Important: The tokenizer above is a teaching example. For exact production budgeting, use the tokenizer or token-counting method appropriate for the model you are calling.
What If the Context Is Too Large?
When the assembled request approaches or exceeds the model's applicable capacity, the application needs a context-management strategy.
Context Window and RAG
RAG is one of the most useful ways to manage a large knowledge base. The knowledge base can be much larger than the model's context window because retrieval selects a smaller working set for each question.
Real Application: Company Policy Assistant
Suppose an employee asks, βHow many days of parental leave are available?β A good assistant can retrieve the current leave policy, keep only the relevant conversation turns, add the user's question, and reserve output space.
This is a hypothetical budget for learning. The actual capacity and tokenizer behavior depend on the model and API you use.
Build a Context Budget
Create three strings for instructions, retrieved evidence, and a user question. Count their combined tokens and compare the result with a hypothetical capacity.
def fits_context(capacity, input_tokens, output_budget):
planned = input_tokens + output_budget
return planned <= capacity, capacity - planned
fits, remaining = fits_context(8000, 5200, 1200)
print("Fits:", fits)
print("Headroom:", remaining)Test yourself
Which statement best describes a context window?
30-Second Recap
- A context window is the token capacity available to a model for a request.
- Instructions, history, retrieved documents, tool results, and the current question can all consume context.
- A context window is not the same as model training data or application memory.
- More context is not automatically better; relevant and focused context is usually more useful.
- RAG, summarization, reranking, and task splitting help applications manage large information sources.