Context Window Management
Every agent runs inside a hard limit: the model can only see so many tokens at once, and a loop that appends a thought, a tool call and a result on every pass walks towards that limit from the first step. Context window management is the bookkeeping that keeps a run inside it.
The levers
Four of them, usually combined:
- Measure — count tokens per turn and track how close the transcript is to the ceiling, including the room the response itself needs.
- Trim — cap individual tool results before they land; one untruncated log or whole-file read can swallow a quarter of the window.
- Evict — drop or summarise the oldest turns, the sliding-window approach, and fall back on compaction when a summary has to stand in for the entire early history.
- Offload — keep durable material outside the window, in files or a vector store, and retrieve the specific piece a step needs.
Position matters as much as size
A window that fits is not the same as a window that works. Material in the middle of a long context is attended to least reliably, so the goal, the live constraints and the latest observation are deliberately kept at the edges rather than left wherever they happened to arrive.
What failure looks like
Unmanaged, a run dies with a hard error at the limit, mid-task, having paid for everything it did first. Managed carelessly it survives but degrades — re-reading a file it already read, repeating a step whose record was evicted, dropping a requirement nobody retired on purpose. Runtimes such as LangGraph expose these controls per step, which turns the limit into a design decision instead of a surprise.
Context window management is the discipline of deciding what occupies an agent’s finite prompt budget at each step of a run — the goal, recent history, retrieved memory and tool results — and what gets trimmed, summarised or fetched on demand. It governs how long a loop can run before it hits the limit, and what the agent still knows when it gets there. Mismanaged, an early constraint silently falls out and the plan drifts.
Think of a desk that only fits a few pages at a time. To do the job you need the instructions, your notes from this morning, and whatever file you are working on right now — but the desk fills up, and something has to come off.
Context window management is how software working on a long task decides what stays on that desk. It keeps the original goal and the facts it still needs, moves older detail into storage, and pulls things back when they become relevant again.
Get it right and the work carries on for hours. Get it wrong and the system is busy but working from the wrong page.