g↗genai/ learner
How GenAI works / Lesson 06

The context window

A model can only work with a bounded amount of context at a time. What happens when a conversation grows? How can an application choose what to include?

Concept + interactive labBeginner friendly

What the model can see.

For a generation, the application sends a sequence of tokens to the model. This can include instructions, conversation turns, retrieved documents, and the user’s latest request. The context window is the model’s limit on the sequence it can process for that request, typically counted in tokens.

Generated tokens also occupy space in the sequence. Exact accounting and limits vary by model and service, so check the specific model’s documentation when building an application.

Key idea: a long chat in the interface is not proof that every earlier message is present in the model’s current context.
An application selects recent messages, a summary, and a retrieved note for a bounded context window, reserving space for the answer. Model weights and external memory are shown separately, with a limited desk as an analogy.
The application assembles the request from information the model can use now.

Make room in a toy window.

Adjust the capacity. This example reserves room for a new answer, keeps the newest message, then includes older turns in order while they fit. It is one possible application strategy, not a rule every model or chat app follows.

01 / Fit the conversationSIMULATION

8 tokens reserved for a response · 16 available for history · 8 unused

Earlier instruction

Explain things with simple examples.

Outside window · 8 toy tokens
Earlier user

What is a token?

Outside window · 7 toy tokens
Earlier assistant

A token is a piece of text.

Outside window · 9 toy tokens
Latest user

Can you explain context windows?

Included · 8 toy tokens

Counts are invented for this exercise. This policy keeps the latest turn and a contiguous suffix of older turns, so the oldest item drops first regardless of its role. It does not prioritize instructions. Real applications may preserve instructions, summarize, reject, or select content differently.

Who chooses what stays?

The application assembles the request. It might send a recent slice of conversation, summarize older turns, retrieve relevant facts, or ask the user to start a fresh conversation. Some systems use other policies. Simply exceeding a limit does not guarantee an API will automatically remove old messages; an application or service may reject the request instead.

Truncation can lose an important constraint. A summary can help preserve useful information but can also omit details or introduce errors. Retrieved passages add relevant material but consume context too. Inspect the assembled request when debugging surprising behavior.

Context, weights, and external memory.

Context is the information supplied in the current request. Model weights are parameters learned during training; they are not a transcript of the current chat. External memory is information stored outside the model by an application, such as a saved preference or document, and must be selected and supplied when needed.

Example: a stored preference is not automatically visible to the model. The app has to retrieve it and include it in the request, subject to its own rules and available space.

Plan a budget.

A practical request budget includes instructions, user and assistant messages, retrieved material, tool results, and space for the response. Token counts depend on the actual tokenizer. A larger window can hold more material, but it does not by itself guarantee that every detail will be used correctly.

available input ≈ window limit − reserved output
request = instructions + history + retrieved material + latest message

This is a planning model, not a universal billing formula. Follow the model provider’s counting rules and test with representative requests.

✎ Try it yourselfKnowledge check · Practice only

Check your understanding.

An earlier detail is no longer in the current request. What can you conclude?

Take this with you.

The context window bounds what can be processed at once. The app decides what to send, while weights and external storage serve different roles. When information disappears, inspect both the selection strategy and the token budget.

Review: How a model predicts the next token →