Advanced

Context Windows and Model Limits

Budget prompts, conversation history, retrieved evidence, and output within an LLM context window. Learn why a larger window is not a guarantee of better answers.

12–15 min Updated Suggest a fix

Quick answer

A context window is a token budget for the model request. Budget instructions, the current question, history, retrieved passages, tool content, and output capacity according to the selected model and provider; fitting more text does not guarantee the model will use every detail reliably.

  • Token counts depend on the tokenizer and exact request content.
  • Reserve output capacity and count tool schemas and results as well as visible chat text.
  • Trim duplicate or low-relevance context, preserve critical constraints, and make omissions observable.
A hypothetical request budget
Instructions       600
Current question   250
History           1450
Retrieved text    3500
Tools               650
Output reserve      900
----------------------
Total             7350  (350 above a 7000-token limit)

Learning objective

Plan a request that fits without silently dropping the information the model needs.

Prerequisites: know what a language model predicts, how text is tokenised, and what an instruction prompt contains. If those ideas are new, start with What is Generative AI?, Tokenisation and Word Embeddings, and LLMs — Pretraining, RLHF, and Scaling Laws.

A context window is a request’s working set

A model processes tokenised input as context while producing an output. In a multi-turn application, the request may include instructions, the current question, selected conversation history, retrieved passages, and tool definitions or results. The model does not automatically remember text that your application did not send; some APIs offer managed conversation state, so inspect what that API includes on each turn.

A context-window limit is measured in tokens, not words or characters. Token counts vary with the tokenizer and the text: code, punctuation, uncommon names, and different languages can consume different numbers of tokens. Use the model or provider’s documented counting method when you need an exact budget.

Budget all parts of the request

Treat the following numbers as a planning example, not as a tokenizer estimate. Assume this model’s documented combined request and output limit is 7,000 tokens. The application’s planned request totals 7,350 tokens, so it is 350 over budget.

Hypothetical per-request token budget
Request componentPlanned tokens
System and developer instructions600
Current user message250
Recent conversation history1,450
Retrieved reference passages3,500
Tool definitions and results650
Reserved response budget900
Total including reserved output7,350

One possible adjustment is to retrieve fewer, more relevant passages: reducing that component by 600 gives 6,750 planned tokens, leaving 250 tokens below the hypothetical limit. In a real application, keep additional headroom because estimates, tool output, formatting, and provider accounting can differ.

A reliable budgeting workflow

  1. Choose the model first. Limits and token accounting are model-specific.
  2. Reserve output capacity. Do not spend the entire allowance on input if the answer needs room.
  3. Count the actual request. Include instructions, history, retrieved content, tool schemas, and tool results.
  4. Trim by value, not by position alone. Remove duplicate history and low-relevance evidence; keep the user’s current task and required constraints.
  5. Make loss visible. If history or retrieval must be shortened, record what was omitted or summarised rather than silently dropping content.
  6. Evaluate answer quality. Test with relevant evidence in different positions and check whether the response uses it correctly.

Diagnose common failures

The request is rejected before generationInspect the selected model, exact request payload, and provider error. Count all included fields and the requested output allowance.
The answer ignores a fact that was suppliedCheck that the fact actually reached the model after application-side trimming. Then reduce noise, improve retrieval, and test placement rather than assuming a larger window solves it.
Follow-up turns lose earlier constraintsCheck whether the application resends those constraints or uses an API-managed conversation. Do not assume that a browser UI’s visible transcript is automatically model context.
Cost or latency rises unexpectedlyMeasure tokens by request component. Long history, repeated tool schemas, and oversized retrieval can grow input work on every turn.

Practice: repair an over-budget request

Your request has an 8,000-token combined limit and currently uses 8,450 tokens, including a 1,000-token response reserve. The user’s latest question and a required safety instruction must remain. You have 3,200 tokens of retrieved documents, of which only two passages directly answer the question.

  1. Keep the reserve and the required instruction.
  2. Remove duplicate conversation turns before summarising the current question.
  3. Prefer the two relevant passages over unrelated retrieved text.
  4. Recount the actual request, leave headroom, and test that the answer still uses the needed evidence.

Check your result: trimming at least 451 tokens gets the planned request below 8,000, but a production request should leave more than one token of headroom. The exact amount depends on the provider’s accounting and request structure.

Key takeaways

  • The context window is a token budget for the model request, not persistent memory.
  • Budget instructions, user input, history, retrieved evidence, tools, and output together according to the chosen API’s rules.
  • Fitting more text does not guarantee better recall; retrieval quality and evaluation still matter.
  • When trimming, protect the current task and constraints, keep useful evidence, and make omissions observable.

Quick check

What should you do first when a request exceeds the selected model’s context budget?

HELP US IMPROVE

How was this lesson?

Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.

Choose your feedback

Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.

Share

Discussion

0

Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.

Continue with GitHub
Loading...