Token Budgeting and Context Window Management in Long Conversations
Quick answer
Quick answer: every model has a limited context window, and long conversations eventually exceed it or become expensive. Set a token budget for each part of...
Quick answer: every model has a limited context window, and long conversations eventually exceed it or become expensive. Set a token budget for each part of the prompt, keep the system instructions and key facts pinned, summarize or retrieve older turns instead of resending everything, and trim bulky tool outputs. Always reserve room for the model's reply.
Why Does Context Management Matter?
A chat application resends the conversation history with each request, so the prompt grows with every turn. Cost and latency grow with it, because providers charge by tokens and processing a longer prompt takes longer. When the total exceeds the model's limit, the request fails or older content must be dropped, often at the worst possible moment.
Bigger windows do not make the problem disappear. Very long contexts cost more, respond more slowly, and models do not always use all of the text equally well. Information buried in the middle of a long prompt can be overlooked, so more context is not automatically better. A smaller, well-chosen prompt often produces better answers than a huge dump of history.
Tokens are also not words. Counting characters or words only approximates usage, and different models tokenize differently. Use the tokenizer or token-counting facility that matches your model, and count tokens before sending, not after an error.
Plan for the reply too. The model's output counts against the same window, so a prompt that fills nearly all of it leaves no room for a useful answer. Reserve a fixed output allowance first, and treat the rest as the input budget.
How Do You Allocate a Token Budget?
Treat the window like a budget with fixed and flexible parts. The fixed parts are non-negotiable: the system prompt, tool definitions, and the output allowance. What remains is shared among conversation history, retrieved documents, and the current user message.
- Reserve space for the response, using the maximum output length you allow.
- Keep the system prompt and tool definitions compact, since they are sent on every call.
- Give retrieved documents a cap, and include only the most relevant passages.
- Give conversation history whatever is left, with a minimum so recent turns are never cut.
budget = MODEL_LIMIT - OUTPUT_RESERVE - count(system_prompt) - count(tools)
context = []
for msg in reversed(history): # newest first
cost = count(msg)
if cost > budget:
break
context.insert(0, msg)
budget -= cost
This sliding window keeps the most recent turns and drops the oldest. It is simple and predictable, but it forgets early details that may still matter, such as the user's goal or a stated preference.
How Do You Preserve Important Information?
Combine the window with other techniques. Summarization compresses older turns into a short running summary that you prepend to the recent messages. It saves tokens but can lose details or introduce errors, so keep summaries factual and refresh them periodically from the original messages instead of summarizing a summary repeatedly.
Retrieval offers another route: store past messages or extracted facts in an index, and retrieve the ones relevant to the current question. This works well for long-running assistants, because the prompt contains only what is needed. Keep stable facts, such as user preferences or constraints, in a small pinned memory block that is always included.
Handle tool outputs deliberately. A single search result, database dump, or log excerpt can consume thousands of tokens. Truncate or summarize tool results before adding them, remove them from history once they have been used, and return only the fields the model needs. Monitor token usage per request, and alert when prompts approach the limit.
Test your strategy with long, messy conversations, not only short demos. Replay real transcripts against your trimming logic, and check that the assistant still remembers the user's goal and constraints after many turns.
Frequently Asked Questions
What happens when I exceed the context window?
The request usually fails with an error, or your framework silently truncates content. Count tokens before sending, and trim deliberately.
Should I always use the largest context window?
No. Larger prompts cost more and respond slower, and extra text can distract the model. Include what is relevant.
Is summarizing old messages safe?
It is useful but lossy. Preserve critical facts separately, and regenerate summaries from source messages when accuracy matters.
How do I count tokens accurately?
Use the tokenizer or counting tool that matches your specific model, since tokenization differs between models.
Summary
Plan the context window as a budget, keep essentials pinned, and use summarization and retrieval for older content. Trim tool outputs and reserve space for answers. Check your model documentation for limits and token counting methods.