Compressing Context
By Theo Luan - July 21, 2025 - 4 minute read
Research
Share
By Theo Luan - July 21, 2025 - 4 minute read
Research
Share
LLMs attend only to the tokens in their current prompt. Because every model enforces a finite context window, extended conversations and multi-step workflows eventually exceed that limit. Our strategy for retaining, selecting, and compressing prior turns is a major lever on inference quality, latency, and cost.
Factory maintains a lightweight, persistent conversation state: a rolling summary of the information that actually matters. We persist anchored summaries of earlier turns and, when compression is needed, summarize only the newly dropped span and merge it into the persisted summary.
A simple way to stay within an LLM's context window is to compress the conversation on-the-fly with a summarization model.
Whenever we need to make an inference call for the top-level agent:
Though seemingly straightforward, this method has significant limitations in practice:
Rather than regenerating the entire summary per request, Factory systematically maintains a persistent summary, updating it incrementally whenever we truncate old messages. Each summary update is anchored to a specific message (we will call these anchor messages), and captures the conversation up to that message.
Our iterative approach uses two main thresholds:
For the sake of simplicity, we will reason only over the conversation tokens. In practice, everything else that must go into the prompt each turn (system prompts, tool schemas, metadata, and any reserved output budget) must be factored in.
We represent the conversation as an ordered sequence of messages [m₁, m₂, …, mₙ]. We maintain anchor points aj marking messages KaTeX can only parse string typed expression that correspond to persisted summaries KaTeX can only parse string typed expression.
We also define KaTeX can only parse string typed expression to be the maximum token size of any given summary S, enforced by our summarize and update functions.
The two thresholds create a classic tradeoff between performance and quality, with some additional complexity around compression frequency.
(KaTeX can only parse string typed expression): Higher compression thresholds preserve more context but impose linear cost scaling:
(KaTeX can only parse string typed expression): The gap between KaTeX can only parse string typed expression and KaTeX can only parse string typed expression controls how often compression occurs, creating a secondary tradeoff:
The optimal configuration depends heavily on the shape of your task. Debugging sessions benefit from higher thresholds due to intricate state dependencies, while simple Q&A can operate effectively with more aggressive compression.
Cutting context too aggressively can backfire. Once key artifacts are summarized away, the agent must re-fetch them, adding extra inference calls and latency. In workflows that revisit the same information (e.g., iterative code review, implementations within complex systems), those round-trips can outweigh the token savings.
Our aim is to minimize tokens per task, not per request. Ideally, we keep just enough context to avoid repeated work while still respecting the model's effective limits.
Certain information is obviously more important to retain. "Important" varies by context and domain.
For Factory's synchronous chat-based coding sessions, for example, we must preserve:
The compression strategy we've outlined is fundamentally reactive. Our agent scaffolding mechanically shrinks history based on token thresholds. While necessary and effective, this approach doesn't scale with advancing model capabilities.
Consider an agent that has just completed a complex debugging session. As soon as the error is resolved, much of the intermediate trial-and-error becomes noise for future turns. Rather than waiting to hit a token threshold, the agent should proactively compress its work.
The future lies in proactive memory management, where agents intelligently choose when and what to compress. This takes several forms:
These capabilities already exist in modern AI systems. The key is recognizing them as part of a broader memory strategy. As models improve at self-reflection and planning, we expect proactive curation to become the norm - shifting from "compress when forced" to "compress when optimal."
Start building