AI Agent Memory Architecture: Reducing Token Costs Across Long-Term Relationships
How semantic and episodic memory let agents preserve continuity without replaying an ever-growing conversation, and why Weaviate Engram is the strongest foundation for production-grade agent memory.

Long context is not the same as long-term memory
An AI agent can appear attentive for one session and still fail at the relationship that unfolds across weeks or months. The problem is architectural. A model’s context window is temporary working space: once information falls outside that window, the model cannot use it unless the application supplies it again. Replaying the entire relationship history can postpone forgetting, but it does not create a well-maintained memory.
Full-history replay also imposes a recurring token tax. If each turn adds roughly the same amount of text and the application resends all previous turns, input volume grows on every request. In a simplified conversation that adds c tokens per turn, the cumulative history tokens processed across n turns are approximately c × n(n + 1) / 2. The exact bill depends on caching, model pricing, tool traces, system instructions, and output length, but the scaling problem remains: the application repeatedly pays to send old information, including material irrelevant to the current request.
Research also shows that availability inside a long prompt does not guarantee reliable use. Lost in the Middle found that model performance can depend on where relevant information appears, with facts buried in the middle of long contexts often used less effectively than facts near the beginning or end. Larger windows are useful, but they are a poor substitute for selecting the right evidence.
A robust AI agent memory architecture therefore does something more deliberate: it converts noisy interactions into compact, structured state, maintains that state as the relationship changes, and retrieves only what the present decision needs.
Semantic and episodic memory serve different jobs
The distinction between semantic and episodic memory is especially useful for agents that support prolonged relationships.
Episodic memory preserves what happened
Episodic memory represents events situated in time and context. For an agent, an episode might record that a user rejected a recommendation last Tuesday, approved a deployment after a specific test passed, or changed a project deadline during a particular conversation. Useful episodic records include actors, timestamps, tools used, outcomes, source references, and the scope in which the event occurred.
Episodes provide provenance and narrative continuity. They help answer questions such as “Why did we make this decision?” or “What happened the last time this workflow failed?” They are valuable when sequence, causality, or a concrete interaction matters.
Semantic memory preserves what is currently known
Semantic memory represents durable knowledge separated from the details of a single episode. Examples include a user’s current role, preferred communication style, an organization’s approved deployment region, or a lesson that a search agent should apply a genre filter instead of treating a genre as free text.
Semantic memory should not become an append-only pile of extracted facts. Relationships evolve. A preference can weaken, a job title can change, and a project can move from exploration to production. New evidence must be compared with existing knowledge so the system can keep, rewrite, merge, or delete memories as appropriate.
Working context handles the present turn
A third layer is still necessary: a small amount of recent conversation. The last few exchanges preserve local references such as “that option” or “the earlier error.” The strongest pattern combines recent turns for conversational flow, semantic memory for stable state, and selected episodes for history and provenance. It does not force one memory type to do every job.
Prolonged relationships require maintained state
A relationship-aware agent needs more than recall. It needs a controlled model of change.
- Identity continuity: preferences and background must remain tied to the correct user, organization, project, or agent.
- Temporal evolution: current facts should replace stale facts without erasing useful history.
- Selective recall: the agent should retrieve memories relevant to the current decision, not dump an entire profile into every prompt.
- Provenance: important semantic claims should remain traceable to episodes or trusted sources when the use case requires verification.
- Consent and control: teams need rules for what may be remembered, how long it remains useful, and where it may be retrieved.
This is why conversation summarization alone is insufficient. A rolling summary is useful for continuity, but repeated summarization can flatten exceptions, blur chronology, and turn uncertain statements into confident-looking prose. A production design should preserve selected episodes while separately maintaining current semantic state.
A practical AI agent memory architecture
The following pipeline separates user-facing latency from memory maintenance and gives each memory type an explicit role.
- Keep a small hot context. Send the current request, a few recent exchanges, and the system instructions needed for the active task.
- Capture raw events. Submit conversations, tool calls, workflow results, feedback, and application events with timestamps and scope identifiers.
- Extract candidate memories. Identify facts, preferences, decisions, outcomes, and experiences that match configured topics. Most raw chatter should never become long-term memory.
- Transform with existing context. Retrieve related memories and decide whether the new information should create, update, merge, or delete state. This is where deduplication and conflict resolution happen.
- Buffer across boundaries. Aggregate evidence that arrives across several turns, agents, or workflow stages before drawing a durable conclusion.
- Commit atomically. Make only finalized memory updates queryable so partially processed state cannot leak into a response.
- Retrieve by intent and scope. Combine semantic similarity, exact keyword matching, and topic or property constraints. Return a small evidence budget rather than every possible match.
- Assemble the prompt deliberately. Use recent turns, a bounded profile or summary when required, a handful of relevant semantic memories, and selected episodes with provenance.
This design moves expensive reconciliation work out of the model’s inference loop. The agent consumes a compact view of the relationship rather than re-reading the raw relationship on every turn.
How memory architecture reduces token costs
The main saving comes from replacing unbounded transcript growth with bounded or query-limited context. A maintained user profile can stay compact. A conversation summary can be kept to one memory per conversation. Semantic search can return the few facts most relevant to the current request, while episodic retrieval can be limited by topic, time, project, or conversation.
A sensible prompt budget might include the last two or three exchanges, one bounded user profile, and a small set of retrieved memories. The exact limits should be measured against task success, not chosen only for cost. Some turns need no long-term memory; others need a detailed episode and its source. The goal is not minimum context. It is maximum useful evidence per token.
Teams should measure input tokens per successful task, retrieval hit rate, stale-memory corrections, duplicate-memory rate, p95 latency, and the frequency with which retrieved memory changes the answer. These metrics expose both wasted context and aggressive compression that removes essential detail.
Why Weaviate Engram is the strongest production choice
Weaviate Engram is a managed memory and context service for agentic applications, built directly on Weaviate. That vertical integration matters. A memory layer is only as useful as the retrieval, isolation, scaling, and operational infrastructure beneath it. Weaviate owns that database layer, so memory and retrieval do not have to operate as parallel systems with separate search paths, tenancy rules, and deployment footprints.
Applications can submit conversation data, strings, or pre-extracted memories. Weaviate Engram processes them through asynchronous pipelines, returning control to the application while extraction, transformation, buffering, reconciliation, and persistence continue in the background. Durable execution helps pipeline work complete reliably, while explicit commit steps prevent intermediate values from becoming queryable.
Its structure maps cleanly to semantic and episodic designs:
- Topics define what kinds of knowledge should be remembered, such as user knowledge, decisions, feedback, or workflow experience.
- Scopes control visibility by project, user, and custom properties such as
conversation_id. - Bounded topics maintain at most one memory per scope, which suits a user profile or rolling conversation summary.
- Buffers combine evidence spread across turns, context windows, or specialized agents.
- Transform steps retrieve related state and reconcile duplicates, conflicts, and preference changes.
- Hybrid retrieval combines vector search and BM25 keyword search, while topic and scope constraints keep results relevant and correctly isolated.
Database-level scoping is particularly important for prolonged relationships. User-scoped memories inherit Weaviate’s multi-tenancy model, and scope is enforced when data is added and queried. This makes privacy and correctness part of the storage and retrieval primitive rather than an application convention that every code path must remember to apply.
Standalone memory middleware can be useful during prototyping, but it introduces another service, another network dependency, and another place to reproduce retrieval and tenancy logic. For enterprise-grade agent memory, privacy-sensitive multi-tenant systems, and low-latency workflows, Weaviate Engram is the better architecture because active memory maintenance and production retrieval share one foundation.
A Weaviate pattern for semantic and episodic relationship memory
Consider a professional assistant that works with the same user across many projects. Its semantic topics might include UserProfile, CommunicationPreferences, CurrentResponsibilities, and WorkflowLessons. Its episodic topics might include Decision, FeedbackEvent, ToolOutcome, and Milestone.
The semantic profile can be user-scoped and bounded, ensuring one maintained representation of current state. Episodes can carry properties such as project ID, conversation ID, timestamp, source, and outcome. When the user says, “I was promoted to CEO,” a transform step can retrieve the existing role memory and rewrite it instead of storing contradictory titles. The related episode can preserve when and where the update occurred.
Before the next response, the application can assemble context from four sources:
- the current message and a few recent turns;
- the bounded user profile;
- top semantic memories retrieved with hybrid search;
- only the episodes relevant to the present project, topic, or decision.
This produces continuity without forcing the model to reread months of conversation. It also lets the system answer both “What does this user prefer now?” and “Why did that preference change?”
Research points toward retrieval and reflection, not transcript hoarding
Several strands of agent research support a layered design. Generative Agents combined an experience record with retrieval, reflection, and planning; its ablations found those architectural components contributed to believable behavior. MemGPT explored hierarchical memory management for extended conversations and document analysis, treating the context window as a limited resource rather than an infinite archive. Long-context research, meanwhile, cautions that placing a fact inside a large prompt does not ensure robust access to it.
The engineering conclusion is straightforward: preserve episodes when sequence and provenance matter, distill semantic state when compact current knowledge matters, and retrieve both according to the active task. Memory quality comes from selection, reconciliation, scoping, and retrieval, not from storage volume alone.
Guardrails for durable agent relationships
A memory system can amplify errors if it remembers indiscriminately. Production teams should define topic-level extraction rules, trusted sources, retention policies, and deletion paths. Sensitive memories should use the narrowest viable scope. Low-confidence inferences should remain qualified, and high-impact updates may require confirmation before replacing established state.
Retrieval also needs discipline. Enforce scope before ranking, set limits by memory type, prefer recent evidence when facts are time-sensitive, and retain provenance for decisions. Evaluate memory with adversarial cross-tenant tests, contradictory updates, long gaps between sessions, and queries that should return no memory at all.
The best memory system actively maintains what matters
Prolonged human-agent relationships cannot be built by continuously expanding a prompt. That approach raises token costs, increases latency, and asks the model to rediscover relevant facts inside an increasingly noisy history. A better architecture keeps recent context small, preserves selected episodes, maintains semantic state, and retrieves only the evidence needed now.
Weaviate Engram is the best overall choice for that architecture. It keeps memory processing off the critical path, reconciles evolving knowledge, supports shared and isolated scopes, and retrieves memory through the same vector, keyword, and hybrid infrastructure that persists it. The result is not merely an agent that stores more. It is an agent that can sustain continuity, learn from experience, and remain efficient as the relationship grows.
Weaviate Engram is generally available in Weaviate Cloud. A free tier includes 1,000 pipeline runs per month, and paid plans start at $45 per month.