AI Agent Memory Layering and Core Document Design: A Practical Guide
This article presents an engineering approach to fixing three common failures in AI agents and coding assistants: memory loss and distortion in long conversations, skill lists that grow without improving selection accuracy, and constraints that drift because they live only in prompts. It details a four-layer memory architecture, a dynamic skill pyramid with promotion and retirement rules, a P / non-P decision boundary table, and a core document set that ties the system together, plus notes on cloud deployment and common pitfalls. Readers will get concrete data structures, executable rules, and document templates they can adapt.
AI Agent Memory Layering and Core Document Design: A Practical Guide
Many teams building AI coding tools or automation agents run into the same set of symptoms: long multi-turn conversations end in “amnesia,” occasionally something is remembered but remembered wrong; the skill inventory keeps growing while only a handful of skills ever get invoked; and even when requirements are spelled out clearly, the model still improvises. These are not isolated bugs. They are the result of missing layered design across three chains: memory, skills, and constraints. This article lays out an engineering approach you can actually implement.
1. Diagnosing the Problem: Three Symptoms, Three Structural Defects
- Memory without layers: every conversation fragment is flattened into a single context, so the signal-to-noise ratio at retrieval time is very low and token cost grows linearly with session length.
- Skills without layers: the skill list becomes an unordered directory with high selection cost, mixing low-value draft skills with high-frequency, stable ones.
- Constraints without a carrier: rules are scattered across prompts with no versionable, diffable document, so model behavior drifts away from team expectations.
The corresponding governance approach: turn memory into a funnel of compress → distill → understand → consolidate; organize skills as a pyramid with promotion; and record constraints as structured, lookup-friendly tables. One core document set ties all three together.
2. A Four-Layer Memory Architecture: From Raw Streams to Reusable Capability
Make memory explicitly layered, and allow flow only in one direction between layers so noise cannot backflow:
- Raw layer: stores the complete conversation and tool-call stream. It is for archival and retrieval only and does not participate in reasoning.
- Cluster layer: groups raw records into session-level clusters by task intent, stripping out small talk and repeated trial-and-error.
- Distill layer: abstracts each execution into a structured entry with goal, actions, outcome, and failure points.
- Understanding and skill layers: the model generalizes from the distilled entries; the stable parts are solidified into skills or rules, forming a positive loop from memory to skills. Skill usage records then become new memory input in turn.
Example data structure:
{"session_id": "s-20260821-01","intent": "Batch fix failing tests","actions": ["locate failing cases", "group by root cause", "fix group by group"],"outcome": "success","failure_points": ["dependency version mismatch caused a false diagnosis"],"promote_candidate": true}
The key design principle is “remember less, remember firmly”: what enters the reasoning context is always the compressed result from the top layer, and the raw layer is retrieved on demand only when you need to trace something back.
3. The Skill Pyramid: A Dynamic View Instead of Static Labels
Manage skills in four layers by invocation frequency and value. The ratios are only a starting point; what matters is the promotion and retirement rules:
- Base layer (~55%): everyday capabilities such as file read/write, command execution, and web access.
- General layer (~30%): cross-scenario capabilities such as debugging and root-cause analysis, planning, and code review.
- Specialized layer (~15%): scenario-specific capabilities such as data training, creative design, and operations on code hosting platforms.
- Experimental layer (~5%): drafts and new skills still being validated.
Write promotion criteria as executable rules:
Experimental -> Specialized: invoked >= 3 times within 30 days AND rated usable by a humanSpecialized -> General: reused successfully in >= 3 different projects AND no project-coupled hardcodingAny layer -> Retirement: zero invocations for 90 consecutive days, or failure rate persistently above threshold
The pyramid is a dynamic analytical view, not a one-time classification. Recompute the distribution every quarter to keep layers from ossifying.
4. The P / Non-P Table: Turning “What Not to Do” into a Lookup-able Decision Boundary
Users express P (what they want); the model has to reason about non-P (boundaries and taboos). Consolidate the results of past clarifications into a growing two-column table, and the next time a similar request comes in, look it up instead of asking again:
| Scenario | P (want) | Non-P (do not) |
|---|---|---|
| Generating code | A runnable minimal implementation | No new dependencies, no changes to the directory structure |
| Producing docs | Conclusion first | No marketing language, no piled-up background |
| Fixing a bug | Minimal change | No opportunistic refactoring of unrelated modules |
This table is essentially a versioned record of decision boundaries. It should be added to and pruned as the project evolves, not treated as fixed dogma.
5. The Core Document Set: The Hub of the Whole System
The core document set matters as much as the skills themselves. From an execution-architecture standpoint, writing the constraints down first pays off more than piling up skills first.
- Primary document (AGENTS-style): describes the execution flow, rule precedence, skill orchestration mechanism, reporting format, memory layer structure, and fault tolerance and fallback strategy. It is the hub that connects the other documents.
- Skill pyramid document: records the layer ratios, promotion conditions, retirement rules, and the timestamp of the last recomputation.
- P / non-P document: records the boundary entries and where each one applies.
Writing guidelines: itemized, decidable, testable. Avoid unverifiable phrasing such as “as much as possible” or “appropriately.”
## Execution Flow1. Read the primary document and the P / non-P table2. Retrieve upper-layer memory; fetch from the raw layer if needed3. Select a skill from the pyramid; fall back to the base layer on failure4. Report in a fixed format: conclusion / changes / risks / to confirm
6. Running It on the Cloud: Decoupling and Observability
To productize the system above, put the memory store, the skill registry, and the document repository on general-purpose cloud capabilities: object storage for archiving the raw stream, a message queue to asynchronously decouple memory distillation, a container platform to run distillation and generalization jobs, and logging plus monitoring and alerting for call-chain tracing and a failure-rate dashboard. Baidu AI Cloud offers corresponding capabilities in these general categories, and you can size them to your team; what matters is keeping one-way flow between layers and end-to-end observability.
7. Common Pitfalls
- Stuffing all history into the context: cost goes up and attention gets diluted.
- Skills that are only added and never removed: selection accuracy drops as the inventory grows.
- Constraints that live only in prompts: they cannot be reviewed and cannot be rolled back.
- A P / non-P table written once and frozen: after the project evolves, it becomes a source of wrong constraints.
Push forward with the four-piece set — layered memory, skill pyramid, boundary table, core document set — and keep correcting it with promotion and retirement rules. That is what lets an agent stay stable, controllable, and reusable over long-running operation.
