DEV Community

Eli
Eli

Posted on • Originally published at aiglimpse.ai

AI Agent Memory: Short-Term, Long-Term, and Vector-Backed Recall

Architecture patterns for persistent multi-session agents, from conversation buffers to semantic search over memory.

AI agents that persist across sessions require memory architectures that balance responsiveness, cost, and recall accuracy. Unlike chatbots that forget after each conversation, production agents need to learn user preferences, retain facts, and avoid repeating questions. This requires three distinct layers: a short-term buffer for immediate context, a long-term store for historical facts, and a retrieval mechanism that finds the right memory without drowning the model in noise. Building this correctly is the difference between an agent that feels aware and one that starts from scratch every session.

Why this matters now

In 2026, multi-session agents are becoming the default interface for customer support, sales, research, and coding assistance. A user expects their AI assistant to remember that they prefer metric units, have a dog named Sophia, or asked about a specific feature last month. Without persistent memory, agents either bloat the context window with irrelevant history or lose state entirely, forcing users to re-explain context. The cost of context is now measured in both dollars and latency: every token retrieved adds to inference time and LLM cost.

At the same time, regulators and users demand transparency. Agents that silently accumulate data in long-term memory create compliance risks (GDPR deletion rights, CCPA opt-outs) and privacy concerns. Teams building agents now must architect memory to be queryable by users, auditable by compliance, and erasable on demand. This is no longer optional; it's table stakes for shipping agents at scale.

The three layers: short-term, long-term, and retrieval

The three layers: short-term, long-term, and retrieval
Photo by Andrey Matveev on Pexels.

A production agent memory system has three distinct functions, each with different performance and storage characteristics. The short-term layer is a fixed-size sliding window of recent messages, held in memory during inference. The long-term layer is a persistent database that stores facts, summaries, and embeddings. The retrieval layer decides what to pull from long-term into short-term for each turn.

Short-term memory is the conversation buffer appended to each agent prompt. For a typical multi-turn agent, this includes the last 5 to 50 messages, depending on token budget. Messages are stored verbatim, with metadata (timestamp, user ID, turn number). The short-term buffer is ephemeral; it's cleared when the session ends or when summarization is triggered. Typical implementation: a Python list or a Redis-backed queue, replaced in full for each API call.

Long-term memory is the agent's persistent knowledge base. It stores three types of artifacts: (1) conversation summaries (e.g., "User prefers async communication, mentioned a deadline of 2026-03-15"), (2) factual state (e.g., "User has account ID 4782, subscription tier: Pro"), and (3) embeddings of meaningful exchanges, indexed for semantic search. Long-term memory is append-only until pruning; it never lives in the prompt itself.

Retrieval is the bridge. At the start of each conversation turn, the agent's current input (user message) is compared against long-term memory. The top-k most relevant memories are pulled and injected into the short-term buffer before the LLM is called. This is the core mechanism of RAG agents: retrieve, then augment, then generate.

Short-term memory: conversation buffers and token budgets

Every agent prompt has a fixed token limit, typically 2000 to 8000 tokens for the context window. Short-term memory (conversation history) must share this budget with the agent's system instructions, tool definitions, and the user's current message. The practical upper limit for conversation history is 30 to 60% of the total window, leaving room for the agent to reason and call tools.

A naive approach is to include the entire session history in every prompt. This works for short sessions but becomes prohibitively expensive. A session with 100 turns at 50 tokens per turn consumes 5000 tokens just for history. If the base context budget is 8000 tokens, the agent has only 3000 tokens left to reason, retrieve memories, and generate a response.

The standard solution is a sliding window: keep only the most recent N messages (e.g., last 20), dropping older messages. This keeps latency and cost constant per turn but breaks continuity. If a user asks "What was I working on last week?", the agent has no access to that conversation unless it was already summarized into long-term memory.

A better approach combines a sliding window with summarization. When the buffer reaches a threshold (e.g., 50 messages), the oldest 30 are summarized and stored in long-term memory, then discarded. The agent retains only the most recent 20. This trades one-time summarization cost (a few extra LLM calls) for ongoing savings: future sessions retrieve the summary, not the raw transcript.

Storage is straightforward: short-term memory lives in application memory (Python dict, Node.js object) or a fast cache (Redis). It must be read and written on every turn, so latency matters more than durability. A typical implementation:

  • Load session state from cache (Redis or in-memory store): 1 to 10 ms.

  • Append user message: 1 ms.

  • Call LLM with short-term buffer: 500 to 2000 ms.

  • Append agent response to buffer: 1 ms.

  • Write updated buffer back to cache: 1 to 10 ms.

Total overhead: negligible compared to LLM latency. The bottleneck is never the buffer.

Long-term memory: storage, summarization, and decay

Long-term memory: storage, summarization, and decay
Photo by Arturo Añez. on Pexels.

Long-term memory must support millions of facts per agent instance without blowing storage budgets. A typical production agent accumulates 100 to 1000 distinct facts per user (preferences, account details, historical decisions, conversation summaries). Across 100,000 users, that's 10 to 100 million records. A single record might be: "User asked for feature request on 2025-11-03: dark mode for charts. Status: under consideration."

Storage architecture depends on retrieval needs. If the agent only needs "retrieve the top 5 most relevant facts," a vector database (Pinecone, Weaviate, Qdrant) is ideal. If the agent needs "find all facts from March 2025 OR facts tagged 'billing'," a hybrid approach (vector DB plus relational DB) is necessary.

Vector-only approach: Convert all facts to embeddings, store in a vector DB with metadata tags. Retrieval is semantic similarity search: embed the user message, find the top-k nearest embeddings, return the associated facts. Pros: simple, scales to billions of vectors, no schema migrations. Cons: filtering (time range, tag, user) requires scanning the entire DB or relying on metadata filters that degrade accuracy.

Hybrid approach: Store facts in PostgreSQL with a pgvector extension or similar. Keep embeddings indexed for similarity search; keep metadata (user ID, timestamp, tag, cost) in relational columns for filtering. Queries can combine semantic similarity ("find memories about X") with structured filters ("created after 2025-01-01"). This is more complex but essential for multi-tenant agents with compliance audits.

Summarization is the key to keeping long-term memory tractable. Without it, memory grows unbounded. A practical strategy:

  • Every 50 to 100 conversation turns, prompt the LLM to summarize the conversation. Example: "Summarize the last 50 messages for future reference. Include any new preferences, decisions made, and open questions."

  • Store the summary as a fact in long-term memory, with an embedding.

  • Delete the raw messages from short-term buffer.

  • Every 30 to 90 days, delete summaries that are older than a retention window, unless tagged "keep indefinitely" (e.g., user preferences).

Summarization costs roughly 0.1 to 0.5 USD per 1000 turns (using GPT-4-mini or similar). For a high-volume agent, this is negligible. The alternative, storing all raw messages, costs 10x more in storage and retrieval.

A second decay mechanism is importance scoring. Not all memories are equally valuable. A memory like "User prefers email over Slack" is timeless; a memory like "User asked about Q2 roadmap on 2025-06-10" decays quickly. Tag memories by importance and lifespan when they're created, then prune based on age and importance.

Vector-backed retrieval: RAG for agents

Retrieval-Augmented Generation (RAG) is now standard in agent memory. At each turn, the agent's input is embedded, and the most similar facts from long-term memory are retrieved and injected into the short-term buffer before inference.

The embedding pipeline: User sends message -> embed it using a dense model (OpenAI text-embedding-3-small, Voyage-02, or similar, 384 to 1536 dimensions) -> search the vector DB for top-k nearest neighbors (typical k = 3 to 10) -> retrieve the corresponding facts and metadata -> inject into prompt.

Choice of embedding model matters. Smaller models (384 dims) are fast and cheap but less accurate. Larger models (1536 dims) are slower and more expensive but capture finer distinctions. For agent memory, a 768 to 1024 dimensional embedding is a practical sweet spot: fast (50 to 100 ms per query) and accurate enough to distinguish "dark mode feature request" from "lighting in the office."

Ranking is crucial. The vector DB returns top-k by similarity score, but similarity is noisy. A practical refinement:

  • Retrieve top-20 candidates from the vector DB.

  • Score each candidate by relevance heuristics: recency (recent memories score higher), importance tag, user-provided ratings.

  • Re-rank and keep top-5.

  • Inject into prompt.

This two-stage retrieval (fast vector search, then slow re-ranking) is more expensive than simple top-k but reduces noise and hallucination.

Hallucination mitigation: Retrieved memories are facts, not LLM outputs, but the agent can still misinterpret or confabulate connections. Three safeguards: (1) store exact quotes with timestamps and sources ("User email: john@example.com, confirmed 2025-11-15"), (2) include confidence scores for each fact, (3) prompt the agent to cite sources ("If you use a memory, mention when and where it came from"). Never retrieve a memory and let the agent invent new details from it.

Privacy and compliance: memory you can delete

A persistent agent memory system is now a data processing system under GDPR, CCPA, and similar regulations. Users have the right to know what's stored, to access it, and to delete it. Technically, this means:

  • Auditability: Every fact in long-term memory must be tagged with creation date, source (which conversation), and relevance. An admin or user should be able to query "show me all facts about user X" and get a list.

  • Retention policies: Define how long facts live. Conversation summaries might expire after 90 days; user preferences might be permanent unless explicitly deleted. Automate deletion via cron jobs or event-driven purges.

  • User deletion: Provide an API endpoint to delete all memories for a user. This is not trivial in a vector DB; you must either (1) scan all records and delete matching ones (slow), (2) maintain a relational index of user -> fact IDs (reliable), or (3) use vector DB filters on the user_id field (if supported).

  • Encryption: Long-term memory often contains sensitive info (health details, financial data, identity). Encrypt at rest and in transit. If possible, encrypt values in the vector DB using a user-specific key, so even the platform can't read memories without explicit user access.

For multi-tenant agents, this is non-negotiable. A single privacy breach (agent of User A retrieving memories of User B) is catastrophic. Implement row-level security: every memory record has a user_id field, and queries are always filtered by the authenticated user.

Common pitfalls and failure modes

Building agent memory systems reveals hidden assumptions quickly. Here are the patterns that break in production:

Unbounded growth: A team ships an agent without summarization. After 1000 conversations, the long-term memory store is queried, embedding 50,000 facts, and retrieval takes 2 seconds per turn. Latency becomes unacceptable, and costs spike. Solution: summarization is not optional. Plan for it from day one.

Retrieval noise: The agent retrieves irrelevant memories and uses them in responses. Example: User asks "How do I reset my password?" and the agent retrieves a memory from 18 months ago ("User forgot password last time, was very frustrated") and apologizes preemptively, confusing the user. The retrieved memory was technically similar but contextually wrong. Solution: re-ranking, recency decay, and semantic clustering reduce noise. Accept that perfect recall is impossible; aim for 80% relevant memories.

Hallucination from retrieval: The agent is given a fact ("User mentioned a dog last month") and invents details ("I remember you have a golden retriever named Sophia who loves fetch"). The retrieval system is not to blame; the agent is extrapolating beyond what was stored. Solution: train agents to be conservative with retrieved facts. Enforce quotation and source citation. Use a system prompt like "Use retrieved facts exactly as written. Do not invent details."

Privacy incidents: A multi-tenant agent system stores all facts without user tagging. A data breach exposes memories of 10,000 users to each other. Solution: user-scoped queries, access controls, encryption, and continuous audits are non-negotiable from the start, not added later.

Cost explosion: Embedding every fact, every conversation, and every user interaction creates millions of embeddings. The team realizes they're spending 2000 USD per month on embedding API calls. Solution: be selective. Embed summaries, not raw messages. Batch embed at quiet hours. Use cheaper embedding models (Jina, mixedbread) if accuracy allows.

Practical next steps

Building a production agent memory system requires integrating five components: a short-term buffer (in-memory or Redis), a long-term store (vector DB plus relational DB for metadata), an embedding pipeline (fast, low-latency model), a retrieval and re-ranking layer, and a summarization loop. Start small: implement short-term buffering and basic retrieval first. Add summarization once you have more than 50 turns of history per user. Add privacy controls once you have more than 1000 users. The architecture you build depends on your agent's workload; measure first, optimize later. But do measure: token cost, retrieval latency, memory accuracy, and user impact. Without metrics, you'll tune the wrong knobs.


This article was originally published on AI Glimpse.

Top comments (0)