Agent Memory Engineering: From Context Caching to Experience Assets
Most agent frameworks already keep context within a session, but that only solves short-term continuity. This article explains how to turn agent memory into a structured, versioned, permission-aware experience asset system. It breaks memory into four asset types—chat memory, skills, wiki knowledge, and a code graph—describes a four-module architecture, and details three key mechanisms: layered distillation with lifecycle management, budget-constrained retrieval and context injection, and versioning, permissions, and team sharing. It closes with common pitfalls and a minimal starting point for implementation.
1. The Real Gap: Memory Depth, Not Memory Existence
Almost every agent framework has basic memory: it keeps context within a session and maintains consistent references across turns. This solves short-term continuity, is cheap to implement, and has become largely homogeneous. What actually determines output quality is whether memory can be structurally accumulated, precisely recalled, permission-constrained, and reused across a team.
The common industry approach is fairly plain: when a session ends, run an offline summary, write it to a plain text or Markdown file, and read the whole thing back on the next startup. The problem is not that the agent forgets. The problem is that memory degrades into a compressed archive of chat logs:
- Summaries are one-shot; later fact changes cannot correct old conclusions.
- Granularity is vague; you cannot locate a specific decision, parameter, or block of code.
- There is no version concept, so nothing can be rolled back, and you cannot tell which memory is currently valid.
- There are no permissions or team boundaries, so experience cannot be shared safely in multi-person collaboration.
- Only conversations are covered; high-value knowledge such as documents and code sits entirely outside the memory system.
So the core technical question here is: how do you upgrade agent memory from a context cache into a continuously growing experience asset system?
2. Capability Layers: Split Memory into Four Asset Types
Treating memory as a single store is a fundamental design mistake. A better abstraction is to split it by the shape of the knowledge and how it is consumed, into four asset types. Each type has a different write path, recall strategy, and lifecycle.
Chat Memory
Holds user preferences, task constraints, confirmed decisions, and lessons from failures. It is time-sensitive and noisy, so it needs layered distillation: raw turns, session-level summaries, and long-term facts coexist in three tiers, which keeps casual chatter out of the long-term layer.Skills
Turn “a method that worked once” into an executable, reusable sequence of operations, including preconditions, steps, checkpoints, and failure fallbacks. Skills consume procedural knowledge rather than conclusive knowledge, so they must stay verifiable.Wiki (Document Knowledge)
For long-form material, build a drill-down hierarchy: topic, section, key point, source snippet. Retrieval first locates the topic and then refines layer by layer, which is more controllable than a single vector recall and makes citations easier to provide.CodeGraph (Code Call Relationships)
Index code into a call and dependency graph that supports impact queries like “who is affected if I change this function.” It handles structured relationship reasoning that plain text summaries cannot replace.
All four asset types share the same requirements: versionable, auditable, authorizable, and incrementally updatable.
3. Reference Architecture: Splitting Responsibilities Across Four Modules
A deployable memory system is usually decoupled into four layers by responsibility, avoiding an all-in-one monolithic store:
- Memory Core: handles layered distillation, skill extraction, storage, and retrieval; it is the main path for writes and recalls.
- Team Console: handles team and member modeling, asset review and configuration, and orchestration of knowledge workshop build tasks.
- Access Proxy: sits between the agent and memory, handling context injection, on-demand retrieval, and result trimming.
- Knowledge Engine: handles document parsing and code graph construction, producing structured knowledge.
The engineering value of this split: changing the retrieval strategy does not affect knowledge construction, and changing the permission model does not affect how agents connect.
4. Key Mechanism 1: Layered Distillation and Lifecycle
Memory quality depends on the write strategy. A “heat-based layering plus decay and eviction” approach is recommended:
L0 raw interaction traces fully retained, used only for audit and re-distillationL1 session-level summaries task goals, constraints, conclusionsL2 long-term facts user preferences, stable decisions, terminology conventionsL3 skills and knowledge assets reusable processes, document key points, code relationships
Writes must go through extraction and deduplication: a new version of the same fact overwrites the old one, and conflicting items enter a pending-confirmation queue instead of overwriting directly. Reads are assembled in the order “long-term facts first, session summaries as supplements, raw traces as fallback” to control the injected length.
5. Key Mechanism 2: Retrieval, Injection, and Context Budget
Memory injection is not “stuff everything in.” It is a ranking problem under a budget constraint:
- Recall candidates based on the current task intent (hybrid structured filtering plus semantic similarity).
- Weight by asset type: code queries go to the graph first, document queries go to hierarchical drill-down first.
- Trim by token budget, keeping citation identifiers for traceability.
- Record “which memories were injected this time” to support later attribution analysis.
Illustrative logic:
def build_context(task, budget=4000):facts = store.query_long_term(task.user, limit=10)skills = store.query_skills(task.intent, top_k=3)docs = wiki.drill_down(task.topic, depth=3)items = rank_and_trim(facts + skills + docs, budget)return render(items, with_citation=True)
6. Key Mechanism 3: Versioning, Permissions, and Team Sharing
In multi-person, multi-agent collaboration, memory must be governed like code:
- Versioning: every write generates a new version, with diff and rollback support.
- Review: skills and knowledge assets must be validated before entering the store, so wrong experience is not amplified through reuse.
- Permissions: authorize by team, role, and agent; sensitive facts are not cross-domain by default.
- Observability: record hit rate, injection volume, and correction rate, and use the data to clean up low-quality memory.
7. Common Pitfalls and Implementation Advice
- Summarizing without structure: memory cannot be located or corrected.
- Recalling without ranking: context is filled with noise, and results get worse.
- Ignoring the write threshold: wrong experience gets reused repeatedly, forming a negative loop.
- Lacking an evaluation set: you cannot tell whether a change is an improvement or a regression.
Start from a minimal closed loop: first connect the two asset types of long-term facts and skills, establish versioning and review, then gradually add document knowledge and the code graph. If you need large-scale storage and retrieval, you can use object storage to hold raw assets, a message queue to decouple the write path, and a container platform to run index-building tasks, with logging services and monitoring alerts to observe recall quality.
8. Conclusion
Engineering agent memory is essentially upgrading context management into experience asset management. Four asset types, layered distillation, budgeted injection, and version and permission governance together determine whether an agent can turn a one-time success into a reusable capability. The deeper the memory, the wider the agent’s usable boundary.
