Sub topic 3
Tokens and context windows: will it fit?
This chapter assumes you know basic Python, have sent prompts to an LLM through an API or chat interface, and have heard of tokens. Here you will learn to check whether a document fits in a model's context window before you send it.
Tokens, not words
A model reads and writes tokens, which are pieces of text such as whole words, word fragments, or punctuation. English prose averages roughly 0.75 words per token, or about 4 characters per token. That ratio changes with the language, the tokenizer, and the content. Code, numbers, and non-English text usually split into more tokens per word.
Each model family has its own tokenizer. The same paragraph can have different token counts in different models, so count with the tokenizer that matches the model you call.
The context window
The context window is the maximum number of tokens a model can handle in one request. It covers everything in the request and the reply together:
- the system prompt and your instructions
- the document you want summarised
- any examples you include
- the model's output
Many providers also set a separate cap on output tokens, which is smaller than the window.
Counting tokens
For OpenAI-style models, the tiktoken library counts tokens locally. Other providers publish their own tokenizers or offer a token-counting endpoint. The function below returns a count you can pass to the chunking and summarising code later in this tutorial.
import tiktoken
def count_tokens(text, encoding_name="cl100k_base"):
encoding = tiktoken.get_encoding(encoding_name)
return len(encoding.encode(text))
sample = "Context windows limit how much text a model can read at once."
print(count_tokens(sample))If you cannot use the exact tokenizer, estimate with len(text) / 4 and add a safety margin of 10 to 20 percent. An estimate is fine for deciding whether to chunk. It is not fine when you are close to the limit.
A fit check
Subtract the fixed costs from the window to see how much room the document has. Reserve space for the output, because a request that leaves no room for the reply is cut off or rejected.
def fits(document, prompt, count_tokens, window, output_reserve, margin=0.1):
used = count_tokens(prompt) + count_tokens(document)
budget = int((window - output_reserve) * (1 - margin))
return used <= budget, used, budget
ok, used, budget = fits(
document="..." * 1000,
prompt="Summarise the following text in a short paragraph.",
count_tokens=count_tokens,
window=8000,
output_reserve=500,
)
print(ok, used, budget)For example, with an 8,000-token window, a 500-token output reserve, and a 10 percent margin, the budget is 6,750 tokens. A 7,000-token document fails the check even though 7,000 is below 8,000.
Common errors
- Counting only the document. The prompt, system message, and output also use the window.
- Using the wrong tokenizer. A count from one model family can be off by a noticeable amount for another.
- Forgetting the output cap. Some models limit output well below the window size, so a long summary may be cut off even when the input fits.
- Treating the window as a quality guarantee. A document that fits can still be summarised poorly. The next chapters cover this.
What to do when it does not fit
If the check fails, you have three options: pick a model with a larger window, shorten the input by removing boilerplate, or split the document into chunks and combine partial results. The rest of this tutorial covers the third option.
Exercise
A model has a 16,000-token window. Your prompt is 300 tokens, you reserve 1,000 tokens for the output, and you use a 10 percent safety margin on the remaining space. What is the largest document, in tokens, you can send in one call?
Show answer
The budget is (16,000 - 1,000) x 0.9 = 13,500 tokens. Subtract the 300-token prompt to get 13,200 tokens for the document.
Exercise
Why can a document that is below the context window size still fail to fit?
Show answer
The window must hold the prompt, the document, and the model's output together. If the document plus the instructions and the reserved output exceed the window, the request does not fit.