Context Compression for AI Products: Managing Conversation Memory Beyond the Window
TL;DR
Every AI product handling multi-turn conversations hits the same wall: the context window fills up. Context compression is the set of techniques you use to decide what stays, what gets summarized, and what gets dropped. The five main approaches are sliding window truncation, recursive summarization, selective memory retention, retrieval-augmented context, and hybrid layered architectures. Each trades off differently on coherence, latency, and cost. This guide covers the mechanics of each, when to use them, and what the design decisions look like for real AI products.
The AI PM Minute
One tactic to make you a sharper AI PM, twice a week. 60 seconds to read. Free.
No fluff. Unsubscribe anytime.
Why the Context Window Is Not Enough
Even with modern models supporting 128K, 200K, or 1M token windows, production AI products hit context limits all the time. Three forces cause this: conversation length (a support chatbot session can easily run 50 to 100 turns), cost (long contexts multiply per-token charges, especially at scale), and performance (attention quality degrades as the window fills, particularly in the middle of a long context).
The naive solution is to pass the entire conversation history every turn. That works fine for short sessions. It fails at scale. A 90-turn customer support session at 200 tokens per turn is 18,000 tokens of history before you have added the system prompt, the retrieved documents, or the user's current question. At that point, you are either hitting the window ceiling, paying for tokens that dilute the model's focus, or both.
Cost spiral
Every new turn appends to the previous turns, so token count grows linearly with conversation length. A session that averages 300 tokens per round costs 60x more at turn 60 than at turn 1, before any model response.
Quality degradation
Research shows that details buried in the middle of long contexts receive less attention than content at the start or end. This is the 'lost in the middle' problem. A context window of 128K tokens does not give you uniform recall across those 128K tokens.
Latency growth
Attention scales quadratically with sequence length. A 2x longer context does not take 2x longer; it takes roughly 4x longer to process. At sufficient length this becomes user-visible latency even on fast hardware.
Architecture inflexibility
Products that pass raw conversation history everywhere cannot easily be deployed across different models. If you switch from a 128K to a 32K model for cost reasons, your sessions break. Context compression decouples product behavior from window size.
Context compression solves all four of these. The tradeoff is engineering complexity and the risk of losing information the model needed. Getting the balance right is a product decision as much as an engineering one.
Technique 1: Sliding Window Truncation
The simplest compression strategy: keep the most recent N turns and discard everything older. When the buffer fills, the oldest turn drops off the back. The model always sees the last N exchanges, the system prompt, and the current message.
When it works
Short-horizon tasks where only the immediate context matters: coding assistants, single-document Q and A, transactional support bots. If the user is asking about the same document or topic throughout, early turns rarely matter.
Where it breaks
Any product where early conversation matters. If a user gave their name, stated a preference, or described a constraint in turn 1, that information vanishes at turn N+1. Relationship-oriented products lose the relationship.
Implementation cost
Very low. Sliding window is a buffer operation: keep a list of messages, pop from the front when you exceed a turn count or token count threshold. No model calls, no extra infrastructure.
Product implications
Tell users explicitly when context resets. Many products surface a 'starting new session' notice rather than silently losing history. Transparency reduces user confusion when the model 'forgets.'
Technique 2: Recursive Summarization
Instead of dropping old turns, you summarize them. When the conversation buffer fills, a background call asks the model to condense the oldest block of turns into a compact summary. That summary replaces the full text, freeing tokens. Future turns are appended to the summary plus recent raw history. As the new block fills, it gets summarized and merged with the previous summary.
Compaction trigger
What happens: Define a threshold: for example, when the token count of raw turns exceeds 4,000 tokens. At that point, summarize the oldest 2,000 tokens into a 300-token summary. Exact numbers depend on your model's context size, your quality bar, and your cost tolerance.
PM implication: The trigger threshold is a product parameter. A higher threshold preserves more detail but costs more. A lower threshold compresses more aggressively and can drop nuance. Tune it against user feedback and quality evals.
Summary quality
What happens: Summarization adds a model call and therefore latency and cost. Use a smaller, faster model for summarization (e.g., Haiku-class rather than Opus-class). Instruct it to preserve named entities, stated constraints, and open questions. What you instruct it to preserve is what it will preserve.
PM implication: Write an explicit summarization prompt, not a generic 'summarize this conversation.' Specify what matters for your use case: user preferences, decisions made, action items outstanding, constraints stated.
Degradation risk
What happens: Each summarization round can drop information. A detail compressed in round 2 is gone by round 3. Products with high fidelity requirements (medical, legal, financial) need auditable history alongside the compressed context.
PM implication: Treat the compressed context as the working representation for inference, not the system of record. Store the raw conversation separately. This also gives you a debugging artifact when users say 'you forgot what I told you.'
Go Deeper in the AI PM Masterclass
The masterclass covers context window strategy, memory design, and the technical decisions that separate great AI products from expensive ones. Taught live by a Salesforce Sr. Director PM.
Technique 3: Selective Memory Retention
Rather than summarizing everything, you extract and persist structured facts from the conversation: entities mentioned, preferences stated, decisions made, open questions. These facts are stored outside the context window and injected back as a compact memory block at the start of each new turn.
Memory extraction
After each turn (or periodically), run an extraction pass to identify facts worth storing: 'User is based in Berlin, budget is 50K, needs GDPR compliance.' Store these as structured key-value pairs or short sentences in a persistent store.
Memory injection
At the start of each request, prepend a memory block: 'What I know about this user: [extracted facts].' This gives the model stable facts without consuming the full conversation history.
Memory scope
Define what gets remembered: user preferences, stated constraints, session goals. Define what does not: casual remarks, intermediate reasoning steps, dead ends. Over-permissive memory fills quickly and injects noise.
Memory decay
Facts become stale. A price stated in turn 5 may be outdated by turn 50. Implement expiry: memories older than a session boundary, or explicitly superseded by the user, should not persist. Add a 'forget this' affordance to your UX.
The privacy angle
Persistent memories are user data. They must be surfaced on request, editable, and deletable under GDPR and similar regulations. Any product that retains memories needs a UI for the user to view and manage what has been stored. Build that interface before launch, not after a complaint.
Technique 4: Retrieval-Augmented Context
Instead of storing conversation history in-context, you store it in a vector database and retrieve only the relevant parts at query time. The current turn is embedded and used to search the history for semantically similar past exchanges, which are retrieved and injected alongside the current message.
Best fit
Long-running agents with potentially infinite history, documentation assistants, and applications where any past exchange might be relevant to any future query. Customer support products that track an entire account relationship over months.
Infrastructure cost
You need a vector store, an embedding pipeline for every new turn, and a retrieval step at every inference call. This is 2 to 3x the infrastructure complexity of pure in-context approaches. Only worth it at sufficient scale or session length.
Retrieval quality
The retrieved context is only as good as your retrieval. Semantic similarity is not the same as relevance to the current task. Retrieval can surface misleading context. Test your retrieval quality with the same rigor you test your model outputs.
Latency impact
Retrieval adds a database round-trip before each inference call. For synchronous APIs this is an additional 50 to 200ms. For products where latency matters, batch embedding and async retrieval pipelines are necessary.
Choosing Your Compression Strategy: A Decision Framework
Most production AI products end up using a layered approach: sliding window for the immediate session, summarization for older turns, selective memory for persistent facts, and retrieval for long-horizon agents. The right combination depends on your session length profile, fidelity requirements, and infrastructure maturity.
Single-session assistant (support, coding, writing)
Sliding window plus summarization is usually sufficient. Store raw history for debugging. Keep the compressed context under 8K tokens unless the task requires more.
Multi-session user relationship (personal assistant, CRM copilot)
Add selective memory on top of session summarization. Extract named entities, preferences, and stated goals at session end. Inject them as a user profile at session start.
Long-horizon agent (project management, months-long research)
Retrieval-augmented context for history plus selective memory for persistent facts. The agent should know what it has done before retrieving it, not just search blindly.
High-fidelity regulated use case (legal, medical, financial)
Keep the raw transcript as the system of record regardless of compression technique. The compressed context is for inference only. Auditability requires the full record.
Start with the simplest technique that covers your P90 session length. Add complexity only when real user behavior in production shows you need it. Building a full retrieval pipeline for a product where 95% of sessions end in 10 turns is over-engineering.
Turn Technical Knowledge Into Product Decisions
The AI PM Masterclass teaches you to reason about memory architecture, context window tradeoffs, and the technical decisions that determine what your AI product can actually do. Cohort starts January 4, 2027.
Related Articles
Before you go: get the AI PM Minute
One tactic to make you a sharper AI PM, twice a week. 60 seconds to read. Free.
No fluff. Unsubscribe anytime.