Skip to main content
DesignKey Studio
Development
July 31, 2026
9 min read
Written byDaniel Killyevo

Memory and Context for Production AI Agents

AI agent memory management is the silent killer of production agents. The three layers, the patterns that work, and when to build vs use a framework.

ai-agentsmemorymcpclaudearchitecture

The agent that worked perfectly in your team's Slack demo will die in production for one of three reasons, and "the model is not smart enough" is almost never one of them. The most common reason, by a wide margin, is that ai agent memory management was never designed. The agent was given a context window, told to figure it out, and shipped. By week two it is forgetting what the user asked in the previous turn, hallucinating user preferences, or quietly burning $0.40 per session in cached context that nobody reads.

If you are building agents that ship to customers, memory is not an optional feature. It is the architecture. Get it wrong and you will spend more time debugging memory drift than you spend on the actual product. Get it right and you will spend less on inference and ship faster.

The TL;DR

  • Production agents need three distinct memory layers: short-term (the conversation), task-state (what we are trying to do right now), and long-term (what we know about the user and the world).
  • The four patterns that work in 2026: explicit memory contracts, summarization at threshold, vector retrieval for facts, structured KV for state.
  • The single biggest 2026 efficiency unlock is Anthropic's code-execution-with-MCP pattern, which cut one canonical Drive-to-Salesforce workload from 150k tokens to 2k - a 98.7% reduction.
  • Anthropic's Managed Agents added memory in April 2026 via mounted /mnt/memory/ directories, which is now the cleanest hosted pattern.
  • Build your own memory layer when latency, privacy, or cost economics demand it. Use a framework when you can.
  • Memory drift is a silent killer. Instrument it, set TTLs, and decommission stale state on a schedule.

What "memory" actually means in an agent

The word gets used to mean four different things and most production failures trace back to teams confusing them. The working 2026 vocabulary:

  • Context window. The literal tokens fed to the model on a single inference call. Bounded, expensive, and overstuffed by default.
  • Conversation memory (short-term). The recent message history. Lives in the context window or summarized just outside it.
  • Task state (working memory). The variables, intermediate results, and tool outputs the agent needs while a task is in progress. Plans, tool call results, retrieval hits, partial outputs.
  • Long-term memory. Facts about the user, the system, prior interactions, and learned preferences that should survive across sessions.

These four are not interchangeable. Putting long-term memory in the context window is how you get $5 sessions. Putting task state in long-term storage is how you get an agent that thinks last Tuesday's draft is still in flight. Each layer has its own storage, its own retrieval pattern, and its own lifecycle.

The three layers, in production

The pattern that survives contact with real workloads:

Layer 1: Short-term (conversation memory)

The recent N turns plus a summary of everything older. Lives in the context window or one Redis hop away. Two things matter: the cutoff (when do you summarize), and the summary quality (does the summary preserve what matters).

The default 2026 pattern: keep the last 10-20 raw turns, summarize older history into a structured block (decisions made, entities mentioned, open questions), and store the full transcript out-of-band for audit. When the conversation hits ~50% of the model's context window, re-summarize the older block more aggressively.

Layer 2: Task state (working memory)

The hardest layer to get right. This is the agent's scratch pad while it is working: the plan, the sub-task results, the tool outputs, the retrieval hits. Most production failures live here.

The bad pattern: dump everything into the context window and hope. With a 200k context, this works for the first three turns and degrades silently after that.

The good pattern: an explicit task-state object the agent reads from and writes to. Structured fields (current_step, plan, completed_steps, tool_results), bounded size (drop old tool results after they have been summarized into a decision), explicit handoff between sub-agents. LangGraph's checkpointing model is the canonical implementation; for non-LangGraph stacks, a simple Postgres JSONB row indexed by session_id works fine.

Layer 3: Long-term memory

Facts about the user, the system, and prior interactions that survive sessions. Two storage shapes win in 2026:

  • Structured KV for things you can name (user preferences, account settings, known facts). Postgres or Redis. Read by ID, not by similarity.
  • Vector retrieval for things you cannot name (prior conversations, documents, examples). pgvector in Postgres if you are starting; Pinecone, Qdrant, or Weaviate if you outgrow it. The vector database market grew from $1.73B in 2024 to a projected $10.6B by 2032, and the production patterns matured along with it.

The mistake to avoid: using vector retrieval for things you can name. Looking up user.tier should be a SQL query, not a similarity search. Reserve vectors for unstructured retrieval where exact match does not exist.

We covered the production-ready vector pattern in Semantic Search With pgvector and the broader retrieval architecture in Multi-Tenant SaaS Architecture Explained.

The four patterns that work

After two years of agent production data, four memory patterns have separated themselves from the noise.

Pattern 1: Explicit memory contracts

The agent does not "decide" what to remember. The system tells it explicitly. A memory contract is a small schema that defines, for each task type, what gets written to long-term memory at the end of the session.

interface MemoryContract {
  taskType: 'support_ticket' | 'sales_call' | 'code_review';
  writes: Array<{
    field: string;
    extractFrom: 'final_response' | 'tool_output' | 'plan';
    ttl?: number;
  }>;
  summaryPromptKey: string;
}

This solves the "agent remembers everything and nothing useful" problem. Every workflow has a known shape; the contract enforces it. No contract, no write.

Pattern 2: Summarization at threshold

When conversation memory or task state crosses a token threshold, summarize aggressively. Two thresholds usually work: a soft threshold (50% of context) where you summarize the oldest 30%, and a hard threshold (80%) where you summarize everything except the last 10 turns and the active task state.

The summarization prompt matters more than the model. A bad summary loses the entity references the agent needs to keep working. Test summarization on real conversation snippets and check that downstream turns still resolve pronouns correctly.

Pattern 3: Vector retrieval for unstructured recall

When the agent needs to recall "have we talked about this before?" or "what is the company's policy on X?", vector retrieval is the right tool. The production pattern in 2026:

Skip vector retrieval if the data is small (under a few thousand items) and structured. Use SQL.

Pattern 4: Structured KV for state

For things like "what is this user's plan tier" or "what is the current step in this workflow", a typed KV store beats vector retrieval and beats dumping into the context window. Postgres JSONB plus Drizzle or Prisma is the boring 2026 default. Read on demand, write at decision points.

The Anthropic code-execution-with-MCP pattern

The single biggest 2026 efficiency unlock. The naive way to give an agent tools is to inject every tool description into the context window. For an agent with 50 MCP tools each averaging 3000 tokens of schema and example, that is 150k tokens before the user has said a word.

The code-execution-with-MCP pattern inverts this. Instead of injecting tool descriptions, the agent runs in a sandbox with MCP tools mounted as files on a filesystem. The agent writes code that calls the tools it needs, when it needs them. Tool descriptions are only loaded when the agent reads the file.

Anthropic's canonical example: a Drive-to-Salesforce workflow that previously consumed 150k tokens of context now consumes 2k. A 98.7% token reduction with no quality loss. (Anthropic Engineering)

The implications for production memory architecture:

  • Tools become memory, not context. The agent looks up tools the same way it looks up facts.
  • Long-running task state lives in files in the sandbox, not in the context window.
  • Sub-agents communicate via filesystem reads, which are cheap, not via prompt injection, which is expensive.

If you are building anything with more than 5-10 tools, this is the pattern to start with. The cost economics are not subtle.

When Anthropic's Managed Agents are the right answer

In April 2026, Anthropic added memory to Managed Agents. Each memory store is a workspace-scoped directory mounted as /mnt/memory/ inside the agent's container, persistent across sessions, scoped per agent.

This is the cleanest hosted pattern in 2026 for teams who do not want to build memory infrastructure themselves. You define the agent (model, prompt, tools, MCP servers), Anthropic handles the runtime, the sandboxing, the session state, and now the memory layer.

When this is the right call:

  • You are early in production and want to ship without building a memory service.
  • Your data residency requirements allow Anthropic-hosted state.
  • You can live with the abstraction (you do not get full SQL-level access to memory contents; it is a filesystem).

When to roll your own instead:

  • You need sub-100ms memory reads (the network hop costs you).
  • You have strict data residency or compliance requirements that disallow third-party persistence.
  • Your memory model needs strong consistency across regions or strong transactional guarantees.

For most teams shipping their first production agent, Managed Agents is the right starting point. Move to a custom memory service only when you have a specific reason to.

When to build vs use a framework

The honest version of this decision in 2026:

Use a framework when...Build your own when...
Memory needs are standard (chat history + facts)Memory has unusual structure (graphs, time-series, multi-modal)
Latency budget is generous (200ms+)Latency budget is tight (< 100ms)
You have one or two agentsYou have a fleet of agents sharing state
Your team has not shipped agents beforeYou have shipped agents before and know your access patterns
You want to ship in weeks, not monthsYou have engineering capacity and a long roadmap

The frameworks worth considering: LangGraph for stateful workflows with checkpointing, the Anthropic Claude Agent SDK plus Managed Agents for hosted simplicity, Mem0 for cross-session user memory specifically. For most teams starting in 2026, "LangGraph for the orchestration plus Postgres + pgvector for the memory" is the boring default that works.

Where memory fails in production

The recurring failure modes we have seen across AI Integration engagements:

  • Stale long-term memory. A user changes their preference, the old preference is still in vector store, retrieval keeps returning the wrong fact. The fix: TTLs on every memory write, plus a write-through invalidation pattern when known facts change.
  • Context bleed across sessions. A multi-tenant agent leaks context between tenants because the session key was not part of the memory query. The fix: every memory read and write must scope by tenant ID, enforced at the query layer, not at the prompt.
  • Summarization hallucination. The summary of older turns drifts from what was actually said, and the agent makes decisions based on the drift. The fix: keep the raw transcript out-of-band, and run an eval that compares decisions made on the summary vs decisions made on the full transcript.
  • Token spend without ceiling. An agent with bad memory hygiene runs at 80% context window for every turn. The fix: per-session token ceilings, alerts when a session crosses a threshold, and observability on average tokens per turn.

We unpacked the broader observability pattern in Testing AI Features With Golden Sets and the human-in-the-loop guardrails in Human-in-the-Loop Architecture.

Where to start

If you are building your first production agent and trying to design the memory layer, here is the order:

  1. Decide your three layers explicitly. Write down what is short-term, what is task-state, what is long-term. No agent should ship without this on paper.
  2. Pick a framework or commit to building. For most teams in 2026, LangGraph plus Postgres plus pgvector is the boring default. Anthropic Managed Agents if you want it hosted.
  3. Define memory contracts per task type. What gets written, when, with what TTL. Without this, your long-term memory becomes a junk drawer.
  4. Use code-execution-with-MCP for tools. If you have more than five tools, the pattern pays for itself within the first week of production traffic.
  5. Instrument from day one. Tokens per turn, memory hit rate, summarization quality, drift checks. You cannot debug what you cannot see.

For the bigger picture on agents in production, AI Agents for Business: What Works in 2026 covers the orchestration patterns and where agents deliver. For the engineering team economics, The Economics of an AI-Augmented Engineering Team covers what it costs to staff this work.

If you are designing or auditing an agent's memory architecture, that is the kind of work we do as part of our AI Integration and API Integration services. The first audit is free, and we will tell you straight when "your memory layer is the bottleneck" is the actual answer.

Want a second opinion on your agent's memory architecture? Contact us for a free 30-minute consultation.

Share this article

DK
Daniel Killyevo

Founder & Technical Lead

Daniel Killyevo started Design Key with a vision to empower businesses with cutting-edge technology and tailor-made solutions. After years of experience in the tech industry, Daniel recognized the gap between clients' needs and available services. This realization led to the creation of Design Key, an agency that would bridge the divide and help clients achieve their goals with better-designed products. Daniel is an accomplished technical leader with a Master's degree in Computer Science from Poltava National Technical University (2005-2011). Born to a Ukrainian mother and Tanzanian father in Tanzania and raised in Ukraine, he brings a unique global perspective to his work. With more than 15 years of experience in software development and product design, Daniel has successfully delivered more than 50 web and mobile applications. He began his career as a software developer and went on to work with prominent companies such as Ciklum, Corrigo (Terminix), and JustEat, helping build more than 40 prototypes and MVPs for startups. His expertise includes architecting complex cloud-based software solutions, API and data integrations, and building and scaling tech teams. As a seasoned entrepreneur, Daniel has gained invaluable experience working on personal startups and establishing two software agencies.

How Custom Software Solves Business Problems thumbnail
Next Article

How Custom Software Solves Business Problems

Contact Us

Need Development Expertise?

Our team of expert developers can help you build robust, scalable solutions.

How does it work?

1

Our solution expert will analyze your requirements and get back to you within 1 business day.

2

If necessary, we can sign a mutual NDA and discuss the project in more detail during a call.

3

You'll receive an initial estimate and our suggestions for your project within 3-5 business days.