New Tool Radar

AI Concepts

How to Handle a Context Window Overflow by Truncating or Summarizing Instead of Failing Silently

Decide in advance what to drop when a request overflows, truncate the oldest turns or summarize the middle, and never silently cut the instruction.

Schematic with three steps: Set the overflow rule before the conversation; Watch the token count as the conversation; Apply the rule and verify the task.

A request that exceeds the context window will be rejected, so a long conversation needs a plan for overflow before it happens. The two standard answers are truncation, dropping the oldest turns, and summarization, compressing the middle of the conversation into a short summary and keeping the recent turns whole. Claude long-context guidance helps either way: put long-form data at the top of the prompt, structure documents with XML tags, and ask the model to ground its responses in quotes from the documents before acting.

What you need

  • The context limit for your model
  • A running token count for the conversation
  • A rule for what may be dropped and what must be kept

Step 1: Set the overflow rule before the conversation grows

Decide now what happens at the limit: drop the oldest turns, summarize the middle, or stop and ask the user. Never silently cut the system instruction or the most recent user message, since those carry the task.

Step 2: Watch the token count as the conversation grows

Count the tokens on every turn, including tool definitions and cached prefixes. When the count approaches the limit, act early: a summary written at 80 percent of the limit is better than an emergency cut at 100 percent.

Step 3: Apply the rule and verify the task survives

Truncate the oldest turns or replace the middle with a summary, then check the model can still answer a question about the earlier content. If it cannot, the summary lost something essential and needs more detail.

Worked example: synthetic overflow handling for 4 conversations

The table below is a synthetic example. It shows the expected result of handling overflow; no conversation was run.

Conversation Tokens Action
Short chat 2,000 No action
Long chat 90% of limit Summarize the middle
Very long chat Over limit Drop oldest turns
Long chat, key instruction 95% of limit Summarize, keep instruction

Expected result (UNTESTED). the short chat needs nothing, the long chats are summarized or truncated, and the key instruction is always kept. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Truncation loses information permanently: a dropped turn cannot be recovered from the summary.
  • Summarization costs a model call and can itself introduce errors, so check the summary against the original.
  • Prompt caching can soften the cost: cached prefixes are reused instead of reprocessed.
  • A hard cap on output tokens stops generation at the cap, so leave room for the answer as well as the input.

Sources

See all