An LLM context window is a fixed token budget, not an unlimited memory. This guide explains how tokens are counted, how input and output share capacity, what truncation looks like, and how to size prompts for reliable AI development. It also includes a repeatable budgeting method, comparison framework, worked examples, and a maintenance checklist for model changes.
Overview
A context window is the maximum amount of tokenized information a model can process in one request. Depending on the API, that budget may include system instructions, developer messages, conversation history, user input, retrieved documents, tool results, and the generated response. The exact accounting rules vary, so a model's advertised context limit should be treated as a starting point rather than a guarantee that every character in a prompt is available for output.
Tokens are pieces of text rather than words. Common words may use one token or less, while unusual spellings, source code, markup, long numbers, emojis, and non-English text can use more. Whitespace and punctuation also affect tokenization. As a result, character counts and word counts are useful rough estimates only. Production applications should use the tokenizer associated with the selected model or the provider's token-counting utility.
Context capacity is different from maximum output. A request can fit within the advertised context window and still fail because the requested output would exceed the remaining space. A useful model is:
Available output budget = total context limit − input tokens − reserved overhead
Reserved overhead can include formatting instructions, tool definitions, response schemas, hidden wrappers, or safety and routing metadata. Whether those items are visible to an application depends on the provider.
Context-limit comparison framework
Model limits change frequently, and providers may expose separate limits for different endpoints or model snapshots. Instead of copying an unverified number into application code, record the values from the model's current API documentation:
| Model record | Context limit to verify | Input and output rule to verify | Operational question |
|---|---|---|---|
| Model or snapshot identifier | Maximum tokens per request | Whether output shares the same total budget | Can the model fit the largest expected request? |
| Provider endpoint | Endpoint-specific limit | Separate input, output, or combined caps | Does the SDK enforce a different limit? |
| Open-source deployment | Configured serving limit | Runtime maximum sequence length | Has the operator changed the default? |
| Long-context configuration | Extended-window availability | Any throughput or quality qualification | Does the workload remain accurate at that length? |
This approach is safer than treating a static “best AI models” list as authoritative. When comparing models, capture the model ID, documentation date, tokenizer, context limit, maximum output, pricing units, and truncation behavior in the same record.
How to estimate a prompt token budget
Use this five-step workflow before selecting a model or designing a retrieval pipeline.
- List every input component. Include system and developer instructions, conversation history, user content, retrieved passages, tool definitions, tool results, images or other modality-specific inputs, and formatting schemas.
- Measure representative samples. Count tokens for short, typical, and worst-case requests. Do not measure only a carefully edited example; production inputs often contain pasted logs, tables, URLs, or source code.
- Reserve response space. Set a deliberate output budget for the answer, JSON object, code patch, or tool call. If the response must be complete, leave more room than the average answer requires.
- Add an overhead allowance. Keep a buffer for wrappers, future instruction changes, tool schemas, and small variations in tokenization. The size of this buffer should reflect the volatility of the application rather than a universal percentage.
- Enforce the budget in code. Before sending the request, trim, summarize, rank, or reject content when the measured total approaches the configured threshold.
A practical prompt-sizing template is:
Context limit: [documented model value]
System and developer tokens: [measured value]
Conversation history: [measured value]
User input: [measured value]
Retrieved content: [measured value]
Tools and response schema: [measured value]
Reserved output: [chosen value]
Safety and change buffer: [chosen value]
Estimated total: [sum]
Remaining margin: [context limit - estimated total]
Use the same tokenizer and message format as the production API wherever possible. A local approximation can identify obviously oversized prompts, but it should not be the final authority.
Inputs and assumptions
Token budgeting becomes easier when each input has a defined policy.
- System instructions: Keep stable rules concise and remove duplicated guidance. Long instructions consume capacity on every request.
- Conversation history: Retain turns that affect the current task. Summarize older turns and preserve decisions, constraints, identifiers, and unresolved questions.
- Retrieved documents: Retrieve by relevance and token budget, not by document count alone. Remove repeated headers, navigation text, and passages that do not support the answer.
- Tool definitions: Large schemas can become a material part of the prompt. Expose only tools needed for the current route and keep parameter descriptions precise.
- Output format: Structured output instructions and schemas improve consistency but use tokens. Measure the schema as part of the request rather than treating it as free.
- Multimodal inputs: Images, audio, and files may be accounted for differently from text. Use the provider's modality-specific calculation method instead of converting everything to characters.
Long context also has a quality cost. A model may technically accept a very large request, yet relevant details can become harder to use when they are surrounded by unrelated material. More context is not automatically better context. Retrieval, deduplication, headings, source labels, and clear placement of the task can improve results without increasing the window.
Cost follows a similar pattern. Providers commonly price input and output separately, but units, discounts, cached-input treatment, and billing rules differ. Calculate cost with the current provider formula:
Estimated request cost = input tokens × current input rate + output tokens × current output rate
Use a documented rate and label the estimate with its date. Do not use a remembered price when evaluating a migration or building a budget.
Worked examples
Example 1: A retrieval-augmented support answer
Suppose a support request includes a stable instruction block, a short conversation, five retrieved passages, a response schema, and a reserved answer space. Measure each component separately. If the total input is close to the model's context limit after adding the reserved output, do not simply send the request and hope the API handles it. First rank the passages, remove duplicates, reduce the history to a factual summary, and measure again.
The application should also define a failure path: if the selected passages alone exceed the budget, return a controlled “narrow the question” response or run a second-stage summarization step. This is more reliable than silently dropping the beginning or end of the prompt.
Example 2: A coding assistant with long history
A coding workflow may accumulate source files, compiler logs, previous patches, and user instructions. Keep the current file and relevant error output at full detail, but summarize superseded attempts. Preserve exact line numbers, error messages, test commands, and acceptance criteria. If the response is expected to contain a patch, reserve enough output capacity for the complete patch rather than budgeting only for an explanation.
Example 3: A long document summarizer
For a document larger than the available budget, use a staged workflow: split the document into measured chunks, summarize each chunk with a consistent schema, then combine those summaries in a final pass. The intermediate summaries should retain names, dates, figures, caveats, and source locations. Compare the staged result with a shorter direct prompt during evaluation; a larger context window is useful only if it improves the task's measured outcome.
When to recalculate
Recalculate token budgets whenever you change the model, model snapshot, tokenizer, API endpoint, message format, tool set, response schema, retrieval settings, or pricing plan. Also revisit them when users begin submitting longer content, when a new language or file type is added, or when production logs show truncation, refusals, malformed JSON, or incomplete answers.
Schedule a formal review at least quarterly and after major model or SDK releases. Check the provider's current documentation for context limits, maximum output, tokenizer behavior, truncation rules, API parameters, and billing. Record the review date in configuration or documentation so an old comparison table is not mistaken for a current contract.
Troubleshooting checklist
- Confirm the actual model ID and endpoint used in production.
- Count all message parts, tool schemas, retrieved text, and reserved output.
- Check whether the SDK or provider silently truncates conversation history.
- Determine whether truncation removes the oldest messages, the newest content, or another provider-defined segment.
- Log token counts and request outcomes without storing sensitive content unnecessarily.
- Test near-limit requests with the same tokenizer and message structure as production.
- Set alerts for rising input size, low remaining margin, clipped responses, and schema failures.
- Recheck pricing assumptions before comparing models or forecasting usage.
For related implementation work, pair this budgeting process with an LLM application monitoring checklist and a documented AI model selection matrix. The central practice is simple: measure the complete request, reserve output deliberately, and treat context limits, tokenizer behavior, and prices as versioned inputs that must be reviewed rather than guessed.