Sub topic 4
The context window
This chapter assumes you can read basic Python, have called an LLM through a chat or completion API, and know what a token is from earlier chapters.
What the window is
The context window is the maximum number of tokens a model can handle in a single call. Everything the model reads and everything it writes must fit inside it. A model with an 8,000-token window cannot process a 9,000-token prompt, however simple the task is.
The window is a property of the model, not of your account or your prompt. Providers publish it for each model, and it differs a lot. Small windows hold a few thousand tokens. Large ones hold hundreds of thousands or more.
What counts toward it
New users often assume the window only covers the text they typed. It covers everything in the request, plus the reply:
- The system prompt, which sets the model's behavior.
- The full conversation history you send with the call.
- The current user message.
- Any documents, retrieved passages, or tool results you add.
- Tool and function definitions, if you use them.
- The model's own output.
The last item matters most. The model writes its answer into the same window, so a long prompt leaves less room for the reply.
Checking that a request fits
The rule is one inequality: input tokens plus the maximum output tokens must be no larger than the window.
def fits_in_window(input_tokens, max_output_tokens, context_window):
return input_tokens + max_output_tokens <= context_window
print(fits_in_window(2000, 500, 8000)) # True
print(fits_in_window(7800, 500, 8000)) # FalseIn the second call the prompt alone fits, since 7,800 is under 8,000. The call still fails the check, because only 200 tokens remain for an answer that may need 500.
Use the max output setting, not the expected output length. You can't know in advance how long the model will write, so you reserve room for the longest reply you allow. The next chapter covers output limits in detail.
What happens when it doesn't fit
Behavior depends on the provider and the API:
- Many APIs reject the request with an error that states the token count and the limit.
- Some APIs accept the request but stop the reply early when the window fills. The response then reports a stop reason such as a length limit.
- Some chat applications silently drop old messages. The model then behaves as if it never saw them.
Silent truncation is the hardest case to notice. If a model suddenly forgets an instruction from the start of a long chat, check whether that text still fits.
Common errors
Counting only the prompt. A check that ignores the output reserve passes requests that later fail or get cut off.
Using words instead of tokens. Token counts are usually higher than word counts. Count with the tokenizer for your model, or read the usage data the API returns.
Assuming a bigger window is free. You can fill a large window, but you pay for every input token you send. Models can also use information in the middle of a very long prompt less reliably than information at the start or end. A large window gives you room. It does not mean you should use all of it.
Forgetting growth over time. In a chat, the history grows every turn, and the window fills even if each message is short. A later chapter on conversation history covers this.
Summary of the working model
Think of the window as a fixed budget. You spend part of it on input and reserve part of it for output. If the two together exceed the budget, the call fails, the reply is cut short, or the oldest content is dropped.
Exercise
A model has a 16,000-token context window. Your prompt is 15,200 tokens and you set the maximum output to 1,000 tokens. Does the request fit? If not, by how many tokens is it over, and what are two ways to fix it?
Show answer
It does not fit. 15,200 + 1,000 = 16,200, which is 200 tokens over the 16,000 limit. You can fix it by shortening the input by at least 200 tokens (for example, trimming old history or a document), or by lowering the maximum output to 800 tokens or less. Switching to a model with a larger window also works.