LESSON 17 Β· TOKENS & CONTEXT

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.

15 min readβ€’Beginnerβ€’Generative AI

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.
1

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.

Simple idea:Imagine giving an analyst a folder before asking a question. The context window is the size of the folder. Everything you put inside competes for that available space.
One requestContext window
System instructions
Conversation
Retrieved context
Tool results
Current question
Generated output

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.

2

What Goes Into the Window?

A production application often builds the context from several sources before calling the model.

1

Instructions

Rules, role definitions, formatting requirements, and application behavior.

2

History

Earlier user and assistant messages that are still useful.

3

Retrieved data

Relevant documents, database records, or RAG chunks.

4

Tools

Search results, API responses, calculations, and other tool output.

Build contextselect useful information
β†’
Tokenizeconvert text to tokens
β†’
Modelprocess the request
β†’
Generateproduce the answer
3

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.

CONTEXT WINDOW

What the model sees now

  • Current instructions
  • Selected conversation history
  • Retrieved information
  • Current user request
APPLICATION MEMORY

What your system stores

  • User preferences
  • Long-term facts
  • Past summaries
  • Records retrieved when relevant
Think of it this way:Memory can be a library. The context window is the small selection of books you put on the desk for today's question.
4

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.

RelevantInformation directly helps answer the question.
ClearInstructions and documents are easy to interpret.
FocusedUnnecessary material is kept out of the request.
ConsistentConflicting instructions and facts are minimized.

A large context full of unrelated text can be less useful than a smaller context containing the right evidence.

5

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.

BeginningImportant evidenceMiddleMore documentsEnd

Practical lesson: retrieve and organize the most relevant evidence instead of assuming that adding more text will improve the answer.

6

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.

PolicyReturn rules
+
HistoryRecent turns
+
ProductOrder details
+
QuestionCurrent request
β†’
LLMGrounded answer

The application does not need the entire company database in the context window. It needs the pieces relevant to this question.

7

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.

</> Python
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.

8

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.

1. SummarizeCompress older conversation into useful facts.
2. RetrieveBring in only documents relevant to the current question.
3. RerankPrioritize the strongest evidence before sending it.
4. SplitBreak a large task into several smaller model calls.
9

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.

Knowledge base100,000+ chunks
β†’
Retrieverselect relevant evidence
β†’
Context windowsmall working set
β†’
LLManswer from context
Key idea:The context window is the working set, not the entire knowledge base.
10

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.

Instructions1,000tokens
Recent history1,500tokens
Policy evidence2,000tokens
Answer budget800tokens

This is a hypothetical budget for learning. The actual capacity and tokenizer behavior depend on the model and API you use.

PRACTICE

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.

βœ“ assemble contextβœ“ count tokensβœ“ reserve outputβœ“ check headroom
QUICK QUIZ

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.