Somewhere between week two and month two of running an agent in production, the same scene plays out. The agent is eight tool calls deep into a task, and it contradicts a decision it made four steps ago. It re-fetches a record it already retrieved. It ignores a constraint the user stated at the top of the session. The model did not change. The prompts did not change. The agent simply stopped holding its context together.1
The instinct is to blame the model. The accurate diagnosis is a memory architecture problem. Most production agent failures are not model failures, they are failures of how the agent stores, retrieves, and evicts what it knows. The fix is not a smarter model. It is deciding what belongs in the context window, what belongs in a persistent store, and who decides when each gets cleared.
The context window behaves like RAM, not storage
The core mistake teams make is treating the context window as if it were a database. It is not. It behaves like RAM, and when you build against it as though it were durable storage, you get failures that look like model problems but are actually structural.1
Three properties of the context window make it unsuitable as a long-term store. First, it is volatile. Everything in it disappears when the session ends, including the preference stated at turn one, the constraint set at turn three, and the decision made at turn seven. Second, it degrades under load well before it fills. The lost-in-the-middle effect documented by Liu et al. in 2023 shows that models retrieve from the beginning and end of context reliably, while information buried in the middle gets measurably less attention.1 Third, it is expensive per access. Every LLM call re-processes the entire window, so a 50,000-token context costs no less than a 10,000-token one just because only the last 500 tokens changed.1
Anthropic makes the same point in its guidance on context engineering: context is a finite resource with diminishing marginal returns. Every token the model must attend to draws on a limited attention budget, and the transformer architecture creates n-squared pairwise relationships between tokens, so as the window grows, the model's grip on any one relationship thins. Anthropic and others call this degradation context rot, and it emerges across every model, not just the weak ones.2
The framing that clears this up comes from an April 2026 study by Yeran Gamage, which ran 4,416 trials at six conversation depths. The finding: omission constraints decay while commission constraints persist. A constraint an agent followed correctly ten turns ago is not being violated because the model changed. The attention weight on that constraint dropped below the threshold needed to enforce it. That is a memory problem, not a model problem.1
Two memory layers, one routing question
If the context window is RAM, then an agent needs a second layer that behaves like storage. Every piece of information an agent encounters belongs in one of two places, and the split follows directly from the properties above.
Working memory holds what the agent needs to complete the current task and nothing else. It lives in the context window because it must be immediately accessible on every call. It holds the current task, compressed tool results, the active reasoning trace, and any mid-task instructions. It does not hold stable preferences, hard constraints, or anything that must survive the session.1
Persistent memory holds what must survive session boundaries. It lives outside the context window in a vector store, a structured database, or a dedicated memory service, because it needs to survive process termination and scale beyond what any context window can hold. It holds exact-value preferences, hard constraints that must never be violated, identity facts, and long-horizon context.1
The routing question that separates the two is simple: would this still be relevant in thirty days? If yes, it belongs in persistent memory. If no, the next question is whether it must survive this session. Task state with a time-to-live goes to a task store, and session-only modifiers stay in working memory. The classification happens at ingestion time, not at retrieval time, so the system never has to guess later.1

The four failure modes when the layers get mixed
Most production agent failures trace back to four specific ways the two layers get conflated. Naming them makes the fix obvious.
Token bloat and cost explosion. When every tool result and intermediate step is appended to the context window without eviction, cost per call grows linearly with session length. A session that starts at 2,000 tokens can balloon past 25,000 as the conversation progresses. Mem0's 2026 LoCoMo benchmark work quantifies the alternative: the full-context baseline scored 72.9% accuracy on roughly 26,000 tokens with a 17-second p95 latency, while a memory-managed pipeline scored 91.6% on under 7,000 tokens with a 1.44-second p95 latency. That is an 18.7-point accuracy lift, a 4x token reduction, and a 91% latency cut from memory management alone, not a better model.1
Preference dilution and safety rule violations. When stable preferences and hard constraints live in conversation history rather than being pinned to the system prompt, they decay like everything else. Gamage's study puts a number on it: a constraint set at turn three that has not been exercised by turn sixteen has a 33% compliance rate, versus 73% at turn five. The fix is constraint pinning, retrieving hard constraints from persistent memory on every call and injecting them at the top of the system prompt, where burial is structurally impossible.1
Contradictory behavior mid-session. When working memory accumulates without management, the agent can hold contradictory signals at once. A user who says "keep the report concise" at turn three may get a verbose document at turn eight, because the instruction is buried under thousands of tokens of content that arrived after it. This is passive decay, not an attack; the agent simply attends to recent content more reliably than early content as the session grows.1
Inconsistent behavior across sessions. When preferences are stored as raw conversation history rather than extracted to a persistent store, exact values vanish when compression fires. "User wants 2700K warm lighting after 8pm" becomes "user has lighting preferences," and the agent is correctly general and usefully precise at different points in the same session depending on whether the preference predates the last compression pass.1

Context engineering is the craft of deciding what belongs where
The term that has replaced prompt engineering for this work is context engineering. Karpathy calls it the delicate art and science of filling the context window with just the right information for the next step.3 Anthropic frames it as the natural progression of prompt engineering: instead of tuning the words of a prompt, you curate the entire context state, system instructions, tools, external data, and message history, at each step of an agent's trajectory.2
LangChain's team groups the strategies into four buckets that are worth memorizing: write, select, compress, and isolate. Writing is saving context outside the window so the agent can use it later, like a scratchpad or a memory file. Selecting is pulling context back in at the right moment, like a memory search. Compressing is retaining only the tokens needed to perform the task, like compaction. Isolating is splitting context across sub-agents so each works from a clean window. Cognition goes further and calls context engineering effectively the number one job of engineers building AI agents.3 This is the same reason multi-agent setups fail in predictable structural ways when state is not deliberately distributed; we covered those failure modes separately in Multi-Agent Failure Is Structural, and memory management is the other half of that story.
For long-horizon tasks, Anthropic describes three techniques that handle work spanning more tokens than the window can hold. Compaction takes a conversation nearing the limit, summarizes it, and starts a new window with the summary, preserving architectural decisions and unresolved bugs while discarding redundant tool output. Structured note-taking, which Anthropic calls agentic memory, has the agent write notes to a file like a NOTES.md or a to-do list that get pulled back in later; Claude Code's memory tool and the CLAUDE.md convention are the file-based version of this. Sub-agent architectures give each specialized sub-agent a clean context window, let it explore for tens of thousands of tokens, and return only a distilled 1,000 to 2,000 token summary to the lead agent.2

Five patterns that get it right in production
The design patterns that fix these failures are framework-agnostic. They apply whether the agent is built on LangGraph, CrewAI, or a custom stack.1
Hard constraint pinning retrieves constraints from persistent memory on every call and injects them at the top of the system prompt, before any conversation history, so position cannot affect them. Tool result summarization compresses a raw 2,000-token API response to a 100-token fact summary before it enters working memory, so ten tool calls cost roughly 1,000 tokens instead of 20,000. Active modifier re-injection keeps mid-task instructions in a working memory object and re-injects them on every subsequent call until the task finishes, rather than leaving them in history to compete for attention. Session-close extraction runs one pass when a session ends that writes stable preferences to persistent memory and discards everything else, the only point where working observations graduate to durable storage. Structured compression with external memory pairs within-session compression for narrative continuity with an external persistent store that preserves exact values and hard constraints that must survive compression passes.1

Red Hat's architecture work maps these onto a full system and adds the coordination layer. Memory write-back happens after inference, when new memories are written to long-term memory. The hierarchy has agent-scoped memory private to a single agent and shared memory that spans a team or enterprise, enabling both personalization and collective learning. A maintenance process, which some tools call dreaming, continuously deduplicates, evicts stale memories, and forms new associations. Agent memory stays distinct from the enterprise source-of-truth databases, which agent operations never modify.4
What to measure, and where this lands
Testing agent memory requires separating within-session from cross-session failures, because they have different causes and different fixes. Within a session, measure constraint adherence by turn depth against a baseline like Gamage's 73% at turn five falling to 33% at turn sixteen, with pinning expected to hold above 90% at all depths. Measure exact value accuracy, because an agent can remember the category of a preference while losing its specific value. And track the working memory token footprint over a twenty-turn session; a flat or slowly growing line means healthy eviction, an exponential one means accumulation without management. Across sessions, measure preference persistence, whether a preference stated in session one is applied in session two without restating it.1
None of this is theory. In our own delivery pipeline we run a fleet of agents that each treat every session as stateless and the shared vault as the persistent memory layer, and we have felt exactly the failure this article describes: a worker that quietly dropped an instruction set when its context was compressed mid-run, producing output that was structurally complete and subtly wrong. The fix was not a better model, it was moving the durable constraints out of the rolling conversation and into the persistent layer where compression could not reach them. That is the whole argument, and we live it every day.
For a client team the takeaway is concrete. Stop debugging agents as though they were forgetting because they are dumb. Check whether the context window is being used as storage, pin the hard constraints where they cannot decay, summarize tool results before they enter working memory, extract stable facts at session close, and measure the token footprint as a health signal. Do that and the agent that seemed to lose its mind at turn sixteen will hold a straight line to turn two hundred. If you are running agents on local models where the context budget is tighter still, the serving patterns in Local LLM Infrastructure are the load-bearing base that makes a clean memory layer possible.
Sources
Sources
-
Mem0, "Memory vs Context Window for LLM and AI Agents" (May 11, 2026). mem0.ai ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15
-
Anthropic, "Effective context engineering for AI agents" (Sep 29, 2025). anthropic.com ↩ ↩2 ↩3
-
LangChain, "Context Engineering" (Jul 2, 2025). langchain.com ↩ ↩2
-
Red Hat Emerging Technologies, "From context to dreams: architecting memory for AI agents" (Jun 1, 2026). next.redhat.com ↩



