Sub topic 6
Context window limits and why they matter
This chapter assumes you can call a model through an HTTP API or SDK, read basic Python, and know what a prompt and a token are. It covers one selection criterion: how much text a model can handle in a single request.
What the context window is
The context window is the maximum number of tokens a model can process in one request. It is shared between your input and the model's output. If a model has a 32,000-token window and your prompt uses 30,000 tokens, the reply can use at most 2,000.
Providers often document the input limit and the output limit separately. Check both. A model may accept a long prompt but cap its reply at a much smaller number.
What counts toward the limit
- The system prompt and any instructions
- The conversation history you resend on each turn
- Retrieved documents or pasted files
- Tool definitions and tool results
- The model's own reply
Chat applications are the usual trap. The API does not remember earlier turns, so you resend the whole history each time. The prompt grows with every turn until it hits the limit.
Why it matters for model choice
The window affects three things you have already met in this tutorial.
- Fit. If your task needs whole contracts or large codebases in one request, a small window rules the model out.
- Cost. Hosted APIs charge per input token, so a long prompt costs more on every call. Resending a 20,000-token document with each question multiplies that cost.
- Memory. For self-hosted models, longer contexts need more memory for the KV cache, which stores intermediate results for every token in the window. Memory use grows with context length on top of the weight memory you estimated earlier.
A larger window is not free quality. Models can handle long inputs less reliably than short ones. Details in the middle of a very long prompt are sometimes missed, so test with your own long inputs.
Checking whether a prompt fits
Count tokens before you send a request. Token counts depend on the model's tokenizer, so use the tool the provider documents. The sketch below uses a rough rule of about four characters per token for English text, which is fine for a first check but not for a hard limit.
def rough_token_count(text: str) -> int:
return len(text) // 4
def fits(prompt: str, window: int, reserved_for_output: int) -> bool:
return rough_token_count(prompt) + reserved_for_output <= window
document = "word " * 20_000 # about 100,000 characters
print(fits(document, window=32_000, reserved_for_output=1_000)) # True
print(fits(document, window=16_000, reserved_for_output=1_000)) # FalseThe second call returns False because roughly 25,000 estimated tokens do not fit in a 16,000-token window.
Options when the input is too long
- Pick a model with a larger window.
- Split the document into chunks and process each separately.
- Retrieve only the relevant passages instead of sending everything.
- Summarize older conversation turns and drop the originals.
Each option has a cost. Larger windows raise price, chunking can lose cross-section context, and retrieval can miss the passage you needed. Measure these on your own examples, as the evaluation chapter describes.
Exercise
Your support bot resends the full chat history on every turn. A hosted model charges per input token. Why does the cost per turn rise over a conversation, and name one way to slow the growth.
Show answer
Each turn's prompt includes all earlier turns, so input tokens, and therefore cost, grow with every message. You can summarize older turns and drop the originals, or keep only the most recent turns plus a short summary.