New Tool Radar

AI Agents & Automation

How to Fix an Agent That Fails When a Long Chat Overflows Its Context Window

Trim or summarise a long agent chat so it fits the context window again instead of failing on the next message.

Schematic diagram with three steps: Count the messages, Summarise the early turns, then Test the next message.

Every model has a context window, and a long chat eventually fills it: the next message fails because there is no room left. The fix is not a bigger window alone, because the same chat will fill that too. The fix is to keep less history: trim the oldest turns, or summarise them into a short note the agent keeps.

Before you start

You need a chat that fails with a context error, and the message count at failure. Note which early details still matter, because those must survive in the summary.

Count the messages and find what can go

Count the messages in the failing chat and note the window size. The oldest turns are usually small talk or finished tasks: those can be dropped. The details that still matter, such as an order number or a budget, go into a short summary the agent keeps instead of the full history.

Summarise the early turns and test the next message

Replace the early turns with a short summary, for example three lines naming the topic, the budget and the open items. Send the next message and check the agent answers from the summary. If it still fails, the summary is too long or the window setting is too small.

Worked example: A trimmed chat for a fictional bookshop agent

Maple Books is a fictional bookshop. Its agent chat overflowed after twenty turns. The table shows the trim.

Part Before After
Turns 1 to 14 Full history Summary: wants a bicycle repair book under 30 dollars
Turns 15 to 20 Full history Kept in full
Next message Failed Answered from the summary

Expected result (UNTESTED): after the trim, the next message is answered, and the agent still knows the topic and the budget. We checked the LangGraph checkpointers documentation, which describes threads and state, but we did not run the trim on this machine.

Limits

A summary loses detail, so a trimmed chat can forget something the summary left out. Write the summary by hand for the first few trims, and check what was lost before you automate it. A bigger context window only delays the same failure.

Sources

See all