Context windows and compaction: how long-running agents avoid running out of room
Every language model works inside a context window: a fixed amount of text (measured in tokens) it can "see" at once. A chatbot answering one question rarely hits the limit. An agent that reads files, runs tools and loops for an hour hits it constantly.
What fills the window
- The system prompt and tool definitions
- The conversation and the agent's own reasoning
- Tool results, which are often the biggest offender (a full log file, a long web page, a big diff)
Common strategies for staying under the limit
- Trimming tool output. Return the first and last lines, or a summary, instead of the raw dump.
- Compaction. When the window gets full, the agent (or the framework) summarizes older turns into a short recap and drops the originals.
- External notes. The agent writes a running plan or scratch file and re-reads it, so important state lives outside the window.
- Sub-tasks with fresh context. A helper gets a narrow job and returns only the result.
The trade-off
Compaction is lossy. A summary can drop the one detail that mattered, such as a file path or a constraint the user mentioned early on. That's why agents sometimes seem to "forget" instructions late in a long task.
How do you handle this in your own agent setups? Do you trust automatic summarization, or do you prefer explicit notes files the agent maintains?
Comments · 2
So compaction is like summarising the conversation so far into a shorter note and carrying on? That's a simpler picture than I expected.
The trade-off is lossy memory. Summaries drop details, so the design question is deciding what has to be kept verbatim and what can be compressed.