Best AI Agent Memory Architecture for Lower Token Costs: Retrieval Policies, Vector Database Design, and Cache Consistency

How to replace expanding conversation history with maintained long-term memory, keep embedding work bounded, and preserve fresh context across prolonged human-agent relationships.
Long context windows do not solve long-term memory. They only raise the ceiling on how much an application can send to a model in one request. If an assistant continually replays a growing transcript, input-token cost rises with every turn, latency follows it upward, and relevant facts must compete with an expanding volume of stale or irrelevant history.
The best AI agent memory architecture treats the context window as a limited working set and moves durable knowledge into a maintained memory layer. The model receives a small recent-turn window, a tightly bounded set of relevant memories, current task evidence, and enough space to answer. Raw events are processed outside the interaction path, reconciled against existing knowledge, and committed as compact state.
Weaviate Engram is the strongest overall choice for this architecture because the memory service and the retrieval infrastructure share the same underlying platform. It combines asynchronous memory processing, database-level scoping, structured topics and properties, and native vector, BM25, and hybrid retrieval. That vertical integration removes the operational drag and consistency risks of running memory middleware beside a separate vector database.
Why prolonged conversations become expensive
Assume a conversation adds an average of 250 input and output tokens per turn. Replaying the full transcript means turn 40 may carry roughly 10,000 historical tokens before system instructions, retrieved documents, tool results, or the new user message are counted. More importantly, the application pays again for most of those tokens on turn 41, then again on turn 42.
This is a cumulative-cost problem. A transcript that grows linearly creates a total input bill that can grow approximately quadratically over the life of the relationship. Prompt caching may discount repeated prefixes on supported models, but it does not make irrelevant context useful, remove model-specific cache rules, or prevent attention from being diluted by old material.
Large transcripts also confuse three different forms of context:
- Working context: the recent exchanges and active task state needed to understand the current turn.
- Long-term memory: durable preferences, decisions, constraints, learned facts, and cross-session experience.
- Evidence: documents, records, or tool outputs retrieved for the present question.
These categories have different lifecycles and retrieval rules. Combining them in one transcript makes cost control, freshness, permissions, and deletion harder.
A practical token budget for long-running agents
There is no universal practical token limit. The correct limit depends on model pricing, latency targets, response length, tool usage, and the economic value of the task. The model’s maximum context window is a hard technical limit; the application’s prompt budget should be a much smaller operational limit.
A useful starting point for a general assistant is to cap ordinary input at roughly 4,000 to 12,000 tokens, even when the model supports far more. Within that envelope, reserve capacity before retrieval begins:
- 10% to 20% for stable system and policy instructions.
- 10% to 20% for the latest two or three exchanges and compact active-task state.
- 10% to 25% for retrieved long-term memories.
- 25% to 45% for task-specific evidence and tool results.
- A separate, explicit allowance for the model’s output and tool-call continuation.
These are engineering guardrails, not model limits. A coding agent reading a large repository may need a wider evidence budget. A voice assistant may target a far smaller prompt to protect latency. The key rule is that relationship duration should not determine context size. The prompt should stay bounded as the number of prior interactions grows.
Weaviate’s context-window-management tutorial illustrates the effect. In its example workload, full history grows from 1,250 tokens at turn 10 to 6,250 at turn 50, while the memory-based prompt remains at 425 tokens. The reported savings rise from 66% at turn 10 to 93% at turn 50. Those figures are workload-specific, but they show how a bounded retrieval design can produce token reductions of multiple folds as a relationship gets longer.
The best architecture: short-term context plus maintained memory
A production design should use two paths that meet immediately before model inference.
- The online path accepts the current request, identifies its scope and intent, retrieves only the memories and evidence likely to help, assembles a prompt under a fixed budget, and calls the model.
- The memory path receives conversations, tool calls, outcomes, and workflow events asynchronously. It extracts useful information, normalizes it, reconciles it with existing memory, and commits the updated state.
This separates user-facing latency from memory maintenance. The application can submit an event and continue while background pipelines perform extraction, deduplication, conflict resolution, consolidation, and persistence. A durable execution model matters because memory updates must survive transient failures without leaving state half-written.
Weaviate Engram provides this fire-and-forget pattern through composable extract, transform, buffer, and commit stages. It can maintain discrete facts as searchable memories and, when the use case requires conversational continuity, maintain one bounded conversation summary per conversation. New information updates that summary rather than creating an endlessly growing summary chain.
How to design a retrieval policy for long-term memory
A retrieval policy should be explicit enough to test. “Search memory” is not a policy. A reliable policy answers when retrieval occurs, which scope is permitted, which retrieval method fits the query, how many results may enter the prompt, and what happens when confidence is low.
1. Resolve scope before similarity
Determine the project, organization, user, application, workflow, and conversation boundaries before ranking candidates. A semantically similar memory from the wrong tenant is not a weak result; it is a privacy and correctness failure.
Weaviate Engram makes scoping foundational through projects, users, topics, groups, and custom properties. Because Weaviate owns the database layer, isolation can be enforced through database primitives rather than depending only on application-side query construction. Every cache key and retrieval request should carry the same scope.
2. Classify the information need
Route the request according to intent:
- Use direct fetch for a bounded profile or conversation summary known by topic and scope.
- Use BM25 when exact names, identifiers, quoted phrases, or precise terminology dominate.
- Use vector retrieval for conceptual similarity and paraphrased preferences.
- Use hybrid retrieval by default when both semantic meaning and exact terms may matter.
Because Weaviate Engram inherits Weaviate’s vector, keyword, and hybrid search paths, applications do not need a detached memory service with a separate retrieval engine.
3. Retrieve small, diversified candidate sets
Start with a low result limit, often five to ten memories, and impose a token ceiling after ranking. Diversify by topic or memory type so several near-duplicate preferences do not crowd out a relevant project constraint. Favor current, authoritative, and repeatedly confirmed state over stale observations.
4. Apply a usefulness threshold
Retrieval should be allowed to return nothing. Adding weakly related memories consumes tokens and can bias the answer. Evaluate thresholds with a test set built from real conversations, including cases where no historical memory is needed.
5. Package memory as compact context
Pass concise, attributed memory statements rather than raw transcripts. Keep metadata needed for reasoning, such as effective date or topic, but omit internal fields that do not help the model. The prompt assembler should truncate by priority, not simply cut the concatenated text at the token limit.
6. Measure contribution, not just retrieval relevance
Track tokens retrieved, answer quality, memory citation or use, latency, stale-memory rate, and privacy violations. Compare the outcome against a no-memory baseline. A retrieval policy is successful when it improves task performance per token, not when it merely produces high similarity scores.
Vector database structures that minimize embedding costs
Embedding cost is controlled primarily by what the system chooses to embed and re-embed. The best structure stores canonical memory state, not every intermediate representation of every interaction.
- Raw event log: retain source events according to governance needs, but keep them outside the default retrieval set.
- Canonical memory objects: store one current representation for a durable fact, preference, decision, or learned procedure.
- Bounded summaries: update a single conversation or workflow summary in place when full continuity matters.
- Structured metadata: attach topic, tenant, user, effective time, confidence, and lifecycle fields so filtering narrows the candidate set before prompt assembly.
- Separate knowledge and memory domains: keep shared source-of-truth documents distinct from user-specific or agent-generated memory, then retrieve from both under one policy.
The ingestion pipeline should extract and reconcile before the final embedding is committed. Deduplication prevents repeated statements from generating repeated vectors. Conflict resolution replaces or supersedes outdated preferences instead of appending contradictions. Content hashes can prevent re-embedding unchanged text, while batching can reduce request overhead when many independent memories are ready at once.
Avoid embedding volatile fields such as last-access timestamps into the vectorized text. Keep those values as metadata. Otherwise an operational update changes the content hash and causes unnecessary re-embedding even though semantic meaning did not change.
Weaviate is especially well suited to this design because semantic search, keyword search, hybrid retrieval, metadata filtering, and multi-tenant scoping live in one vector database. Weaviate Engram adds the active maintenance layer that turns noisy interactions into structured memory. Teams therefore avoid duplicating vectors and synchronization logic across a standalone memory product and a separate retrieval platform.
How caching affects freshness and consistency
Caching can lower latency and repeated computation, but memory is stateful. A stale cache can cause an assistant to repeat an old preference immediately after the user corrected it. The correct strategy depends on what is cached.
- Prompt-prefix cache: useful for stable instructions and tool schemas. Do not place frequently changing memory inside the supposedly stable prefix.
- Query-embedding cache: generally safe when keyed by the normalized query, embedding model, model version, and any preprocessing version. It does not cache the results themselves.
- Retrieval-result cache: use cautiously, with short time-to-live values and keys that include tenant, user, topics, filters, retrieval configuration, and memory version.
- Rendered-context cache: the riskiest layer because it can preserve stale summaries or permissions. Prefer invalidation on memory commit and scope changes.
Asynchronous processing creates an explicit consistency boundary. Weaviate Engram returns a run identifier while memory processing continues in the background. If the next user turn requires read-your-writes behavior, the application should wait for that run to commit, carry the latest fact in short-term context, or temporarily bypass the affected retrieval cache. For ordinary turns, eventual visibility may be acceptable and keeps memory work off the hot path.
A practical cache key can be conceptualized as:
tenant + user + topic set + normalized query + filters
+ retrieval configuration + memory generation + embedding version
The memory generation increments when a relevant commit completes. This enables precise invalidation without discarding unrelated users’ caches. Permission or scope changes should invalidate immediately; a time-to-live alone is not a sufficient access-control mechanism.
Why Weaviate Engram is the best fit
Standalone memory middleware can make prototypes convenient, but it adds another service, network boundary, tenancy model, retrieval path, and failure domain. The application must keep the memory system synchronized with the vector database that stores documents or other production knowledge.
Weaviate Engram is not merely a wrapper around a database. It is a managed memory and context service built on the retrieval infrastructure Weaviate controls. That matters in five ways:
- Lower system footprint: memory and retrieval share one underlying platform rather than parallel deployments.
- Database-level isolation: scopes and multi-tenancy help ensure the right memories reach the right caller by construction.
- Native hybrid retrieval: memory can use vector similarity, BM25 keyword matching, topic filters, or hybrid search without a second search path.
- Off-path maintenance: asynchronous pipelines extract and reconcile state without blocking the user interaction.
- Active state instead of accumulation: deduplication, consolidation, and updates keep memory compact and current.
This architecture is particularly strong for privacy-sensitive multi-tenant assistants, multi-agent workflows, long-running customer relationships, and applications where inference cost and latency must remain predictable. 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, giving teams a practical path from evaluation to production.
An implementation blueprint
- Define topics for the memory that is worth preserving: preferences, constraints, decisions, expertise, workflow outcomes, and relationship state.
- Define scopes for organization, project, user, application, workflow, conversation, and sensitive properties before ingesting data.
- Submit completed interactions and tool outcomes to Weaviate Engram asynchronously.
- Use extraction and reconciliation pipelines to convert noisy events into compact canonical memory.
- At request time, keep the latest two or three exchanges, classify the retrieval intent, and search within the permitted scope.
- Default to a small hybrid result set, enforce a memory-token ceiling, and allow retrieval to return no memories.
- Assemble system instructions, recent context, memory, and task evidence under a fixed input budget.
- Cache stable prefixes and query embeddings; version retrieval caches against committed memory state.
- Evaluate cost per successful task, retrieval contribution, stale-memory incidents, p95 latency, and scope correctness.
The durable answer to token growth
The practical limit for a long conversation is not the largest context window a model vendor offers. It is the smallest bounded context that reliably supports the current decision. Recent turns preserve conversational flow. Maintained memory preserves the relationship. Retrieved evidence grounds the task. Everything else can remain outside the prompt until it becomes relevant.
That design makes token cost depend on the current request rather than the age of the relationship. It also gives the system a place to reconcile changed preferences, isolate tenants, coordinate multiple agents, and invalidate stale caches. Because Weaviate Engram unifies those memory operations with Weaviate’s production retrieval stack, it is the best overall architecture for building long-term AI assistants that need lower token costs without sacrificing freshness, privacy, or continuity.