Sub topic 2
Context Windows and What Happens When You Exceed Them
This chapter assumes you can call a chat-style LLM API, know the roles (system, user, assistant), and have a general idea of what a token is. You should also know from the previous chapter that each request must carry the whole conversation.
What the context window is
The context window is the maximum number of tokens a model can handle in one request. It covers everything the model reads and writes for that call. A token is a piece of text, often a short word or part of a longer word.
Two numbers matter in practice:
- The input is everything you send: the system prompt, the history, tool definitions, and the newest user message.
- The output is the reply the model generates.
Most providers count input and output against the same window. Some also set a separate cap on output length. Check your model's documentation for both limits, because they differ between models and change over time.
Why conversations outgrow the window
Because the history is resent every turn, each request is larger than the last. The input grows with every exchange, and the total tokens you pay for across a conversation grow faster than the conversation itself.
Long replies, pasted documents, and tool results fill the window much faster than short chat messages. One large file pasted into a chat can use more space than fifty ordinary turns.
What happens when you exceed it
The behavior depends on the provider and the API you use. There are three common outcomes.
| Outcome | What you see | Risk |
|---|---|---|
| Hard error | The API rejects the request, often with a message that the prompt is too long | Easy to detect, but the user sees a failure |
| Silent truncation by the service | The request succeeds, but some input is dropped | The model may lose the system prompt or early facts without any warning |
| Output cut short | The reply stops mid-sentence because the window or output cap ran out | Broken answers, unfinished JSON, or incomplete tool calls |
The third case is easy to miss. If your input nearly fills the window, little space remains for the reply. A request can be valid and still produce a truncated answer. Check the stop reason or finish reason in the response. A value indicating a length limit means the reply was cut off.
Do not rely on the provider to trim for you. Silent truncation may remove the wrong content, and you will not know what the model saw. Your application should decide what to keep.
Detecting the problem early
Treat the window as a budget you check before every request, not an error you handle afterwards. A minimal check looks like this:
def fits(input_tokens, window, reserved_for_output):
# True if the input leaves enough room for the reply.
return input_tokens + reserved_for_output <= window
window = 8000
reserved_for_output = 1000
for input_tokens in (3000, 6900, 7200):
status = "ok" if fits(input_tokens, window, reserved_for_output) else "must shrink"
print(input_tokens, status)This prints 3000 ok, 6900 ok, and 7200 must shrink. The check uses a token count, which later chapters cover in detail: you will see how to get exact counts or estimates, and how to turn this check into a proper input budget.
Your options when the history is too long
When the check fails, you have a small set of choices. Later chapters cover each in depth.
- Truncate: drop the oldest messages and keep the newest.
- Summarize: replace older messages with a shorter summary written by a model.
- Combine both: keep a summary plus recent messages word for word.
- Move history out of the prompt and retrieve it when needed.
Each choice trades something away. Truncation loses old facts completely. Summaries keep the gist but can drop or distort details. The rest of this tutorial shows how to pick and test a strategy.
Exercise
A request has 7,500 input tokens, the model's window is 8,000 tokens, and your application allows replies of up to 1,200 tokens. Does the request fit? What can go wrong if you send it anyway?
Show answer
It does not fit. 7,500 + 1,200 = 8,700, which is more than 8,000. Depending on the provider, the request may be rejected, the input may be silently trimmed, or the reply may stop partway through because only about 500 tokens remain. Shrink the input to at most 6,800 tokens before sending.
Exercise
Why is it risky to let the provider silently truncate an over-long prompt instead of trimming it yourself?
Show answer
You do not control what is removed. The service might drop the system prompt or important early facts, and nothing tells you the model never saw them, so answers degrade without an obvious error.