Discuss your project

Guide

Context window management for long-running agents

Context window management: what fills the window, what to compress, what to move to memory, and the four actions that silently invalidate everything you cached.

By · Updated · 4 min read

A context window is a budget with a hard limit and a soft cost. Every token in it is paid for on every turn, and every token competes for the model's attention with the tokens that matter.

Most agents do not fail because the window is too small. They fail because it is full of the wrong things.

Discuss your project

What fills the window

The system prompt. Instructions, persona, rules, brand guardrails. Stable, and usually worth its size.

Tool definitions. The schema for every tool the model can call. This grows quietly and is often the largest block after the system prompt.

Memory. Facts loaded into every turn. Useful when small, expensive when it becomes a dumping ground.

Conversation history. Every message and every response. The part that grows without bound.

Tool results. The raw output of every call. The most common cause of a context blowout.

Retrieved documents. Passages from a search or a knowledge base. Necessary, but usually too much of them.

The budget

A rough allocation for a working agent:

BlockShare of windowNotes
System prompt and rules10 to 15%Stable, cacheable
Tool definitions10 to 20%Prune aggressively
Memory5 to 10%Facts only, not history
Retrieved context20 to 30%The relevant passage, not the library
Conversation and tool resultsThe restThe part you manage dynamically

The numbers are a starting point. The principle is that the stable blocks should be large enough to be useful and small enough to leave room for the work.

What to compress, what to move, what to drop

Compress the history. Keep the decisions, the current task, and the open questions. Drop the exploration that led nowhere. One compression, late, with a clear rule.

Move facts to memory. A preference, a definition, a standing rule belongs in memory. The transcript of how you learned it does not.

Drop the tool result after it is used. A 5,000-token JSON response that produced one number does not need to stay in the window. Summarize it to the number and the source.

Prune the tool list. Every tool the model can see is tokens on every turn. If a workflow uses four tools, do not load twenty.

Retrieve less. Five relevant passages beat fifty. A reranker that picks three is cheaper and usually more accurate than a similarity search that returns twenty.

The four cache invalidators

Context management and caching pull in opposite directions. Compression saves tokens and invalidates the cache. The four events that rewrite the prefix are:

  • Switching model mid-session.
  • Reloading configuration.
  • Adding a skill or tool after turn one.
  • Context compression.

One compression per long session is free. Five is a miss pump. If a session must add skills, load them at the start so the prefix stabilizes once.

The long-session pattern

For work that genuinely needs a long session:

  1. Load the system prompt, skills, and tools at turn one.
  2. Keep the memory block small and stable.
  3. Retrieve per task, not per session. Put the passage in the message, not the system block.
  4. Summarize tool results to the fields you need.
  5. Compress once, at a defined threshold, with a rule for what survives.
  6. Start a new session for a new task.

The rule that saves the most money: a new task gets a new session. Continuity is not free, and most of it is unnecessary.

Where context management fails

The window is treated as storage. Everything is kept "just in case." The model pays for it on every turn and the attention dilutes.

Memory becomes a dump. Five hundred facts loaded every turn, most of them stale. Prune monthly.

Tool results are never summarized. The raw output stays, and the context grows with every call.

Compression is ad hoc. Someone compresses when the model starts failing, which is the worst time. Define the threshold and the rule in advance.

The tool list is never pruned. Twenty tools, four used, sixteen adding noise.

How to tell the context is the problem

Three symptoms:

  • The model forgets an instruction given earlier in the session.
  • The quality drops as the session gets longer, even on the same task.
  • The cost per turn rises while the work stays the same.

All three point to a window full of the wrong things. The fix is rarely a bigger window. It is a smaller, better-curated one.

Questions owners ask

No. A larger window costs more per turn and dilutes attention. Use the smallest window that holds the task.

Once, when the session approaches the threshold you set. Not on a timer, and not repeatedly.

It can. Keep the decisions, the current task, and the open questions. Drop the exploration.

Stable facts and preferences. Not task progress, not transcripts, not temporary state.

Yes, by starting a new session for each task and keeping the retrieved context small. That is usually better than compressing a bloated session.

Start with the process that costs you the most time

Tell me which process you want to improve. A name, an email, and a short description of the process are enough to start a useful conversation.

Discuss your project