A long conversation grows past the context window turn by turn, and when the total passes the limit the oldest turns are cut, silently. The fix is to trim deliberately: count the history, drop the oldest turns, and keep a short summary of what was dropped, so the model keeps the recent turns and the gist of the old ones.
Prerequisites
- The chat history with token counts per turn, or a way to count them.
- The context window size and the room needed for the next reply.
- A note of which turns carry facts the conversation still needs.
Steps
Step 1: Count the history
Count the tokens in each turn, including the system prompt. Add them up and compare the total with the window minus the room for the reply. If the total is over, trimming is needed.
Step 2: Drop the oldest turns
Drop the oldest turns first, keeping the most recent ones. Drop whole turns, never half a turn, so the model sees complete exchanges. Stop when the remaining history plus room for the reply fits the window.
Step 3: Keep a short summary
Write one or two sentences summarising the dropped turns: the facts decided, the names used, the task. Add that summary as a short system note, so the dropped facts are not lost entirely.
Worked example: Maple Corner bakery menu planning chat
The fictional Maple Corner bakery has a menu-planning chat with 7,400 tokens of history and an 8,192-token window, needing 800 tokens for the next reply.
| Turns kept | Tokens | Room for reply | Fits 8192 |
|---|---|---|---|
| All turns | 7400 | 800 | No: 8200 total |
| Drop oldest 2 turns | 5500 | 800 | Yes: 6300 total |
| Plus 1-sentence summary | 5650 | 800 | Yes: 6450 total |
Dropping the 2 oldest turns, 1,900 tokens, leaves 5,500 tokens of history, and the 150-token summary brings it to 5,650, leaving room for the 800-token reply.
Expected result. The recent turns stay in context, the dropped turns survive as a short summary, and the total fits the window with room for the reply. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- A summary can lose detail; a fact that matters later should be kept as a full turn, not summarised.
- Trimming is a workaround, not a fix; a conversation that grows forever needs a different design.
- This guide does not cover prompt caching, which can reduce the cost of long histories.
