Large-language-model applications rarely fail because a prompt is one token too long. They fail because context growth is handled without priorities. Chat history expands, retrieval returns more passages, tool results become verbose, and eventually the application truncates whichever text happens to be easiest to cut.

A safer design treats the context window as a budget with explicit allocations. The goal is not to fill every available token. The goal is to preserve the information that controls behavior while leaving enough room for a complete answer.

Reserve output capacity first

If a model accepts a context window of C tokens, reserve answer space before constructing the input:

input_budget = context_limit - reserved_output - safety_margin

The safety margin covers tokenizer differences, message framing, tool metadata, and small prompt changes. Without it, a request that appears to fit in development can fail after a library adds wrappers or a user sends unusually dense text.

Do not derive the output reservation from whatever space happens to remain. Decide how much answer space the product needs first.

Assign priorities instead of truncating blindly

A useful ordering for many assistants is:

  1. system and security instructions;
  2. the current user request;
  3. evidence required to answer the request;
  4. relevant structured state;
  5. recent conversation history;
  6. older conversational detail and optional examples.

The exact order depends on the product, but it should be deliberate.

Keep control instructions intact

System instructions, schemas, and tool contracts should normally be treated as indivisible. Removing half of a policy paragraph can change its meaning. If control text is too large for the intended model, reduce that control surface during application design rather than clipping it dynamically at runtime.

Preserve the current request

Older conversation is usually less important than the current turn. A new request should not lose half of its instructions because twenty previous messages were retained verbatim.

When the application supports long attachments, separate the current instruction from attached evidence so each can be budgeted independently.

Budget retrieval by usefulness

Retrieval systems often return a fixed number of chunks. That ignores how much context each result consumes.

Prefer a token-aware selection loop:

selected = []
used = 0

for chunk in ranked_chunks:
    cost = token_count(chunk.text)
    if used + cost > evidence_budget:
        continue
    selected.append(chunk)
    used += cost

This is safer than slicing a concatenated evidence string in the middle of a passage, which can remove qualifiers or citations.

Prefer information density over repeated evidence

Five passages that repeat the same fact may be less useful than three passages covering different parts of the question. Combine relevance ranking with deduplication or source diversity when possible.

That improves information density and leaves more room for reasoning and output.

Compress history into state when possible

Free-form summaries are useful, but they can silently discard details. Workflow-style applications often benefit from explicit state:

{
  "deployment_target": "staging",
  "rollout": "canary",
  "rollback_required": true
}

Then include only the fields relevant to the current request. This is easier to inspect and update than repeatedly summarizing a growing transcript.

Make truncation observable

Context management should produce metrics rather than invisible behavior. Useful measurements include:

  • input tokens before trimming;
  • tokens removed per section;
  • retrieved chunks considered and retained;
  • history turns dropped;
  • reserved output tokens;
  • requests approaching the model limit.

These signals reveal whether the application regularly operates at the edge of its context window.

If most retrieved passages are always discarded, increasing the retriever’s top-k value will not help. If history dominates every prompt, explicit state extraction may be more valuable than a larger model.

Avoid character-count approximations

Characters, words, and tokens are related but not interchangeable. Code, JSON, identifiers, and non-English text can tokenize very differently.

Use the tokenizer or token-counting API associated with the deployed model when available. If exact counting is impossible, leave a larger margin and treat the estimate as approximate.

Treat model changes as budget changes

Moving to a model with a larger context window does not remove the need for budgeting. Larger windows can increase latency and cost, and they make it easier to include irrelevant material.

Model upgrades should trigger context regression tests. Feed representative long conversations, retrieval sets, and verbose tool outputs through the prompt builder and verify which sections survive.

Common mistakes

Reserving no answer space

A prompt can fit while leaving too little room for the requested response. Reserve output capacity before filling the input.

Keeping history because it is easy

Verbatim history is simple, but its value usually decays with age. Prefer recent turns plus explicit state needed for continuity.

Cutting evidence at arbitrary boundaries

Keep complete evidence units. If a chunk is too large, rechunk it upstream or summarize it deliberately.

Assuming more context always improves answers

More context can contain more noise. Measure answer quality, latency, and cost instead of treating maximum context length as a target.

A practical budgeting policy

A production policy can follow a deterministic sequence:

  1. reserve output tokens and a safety margin;
  2. add system instructions and the current request;
  3. allocate a bounded evidence budget;
  4. add relevant structured state;
  5. fill remaining capacity with recent history;
  6. record what was omitted.

Context windows are capacity limits, not document stores. Applications become more reliable when they decide what deserves that capacity before the prompt reaches the model.