AI Agent Memory and Core Document Optimization: A Practical Guide
This article explains how to fix two common AI agent problems: agents that cannot retain useful context and agents that behave unpredictably without clear constraints. It covers a four-layer memory architecture that combines local memory plugins with core documents, a document-first approach that treats core documents as the operating constitution, a pyramid model for tiering skills by usage frequency and value, and a "P and not-P" preference table that turns user corrections into reusable decision boundaries. Readers will also find rollout advice and common pitfalls to avoid.
1. Background: Why Agents “Forget” and “Misbehave”
In real-world Yy agent engineering, the two most common problems are not model capability gaps but missing context organization and missing behavioral constraints. The first is a memory problem: as conversations grow longer and noisier, the conclusions that actually matter get buried. The second is a constraint problem: without explicit rules, agent output drifts and can even follow contradictory execution paths.
A typical evolution path looks like this. At first, the agent is called as an auxiliary component of some general-purpose coding tool, and the main workflow is handled by the external tool. As the execution architecture is gradually refactored, the primary and auxiliary roles reverse, and the agent itself begins to own the full loop of planning, execution, and reporting. The key to this shift is not swapping the model but filling in three pieces of infrastructure: layered memory, core documents, and skill tiering.
2. Memory System: Consolidating Three Layers into Four
2.1 Limitations of the Native Three-Layer Structure
Most agent frameworks provide three memory layers by default: short-term conversation context, mid-term task summaries, and long-term vector retrieval. This is enough for lightweight scenarios, but it has two clear gaps. First, there is no channel for capturing procedural knowledge, so trial and error and corrections made during execution are never structured. Second, there is no skill-oriented output, so memory is used only to answer questions and is never converted into reusable operational capability.
2.2 The Fourth Layer: Local Memory Plugin Meets Core Documents
Once a local memory plugin is introduced (a common industry approach is to expose memory read and write capabilities through a local service), a four-layer structure takes shape. The point is not the plugin itself but connecting it to the core documents to form this chain:
conversation clustering → execution trace distillation → deep understanding (model reasoning) → Skills
The chain works like this: raw conversations are first clustered to remove noise, reusable conclusions are then distilled from execution traces, the model performs semantic-level induction, and the conclusions are finally solidified into skill entries. This creates a dual loop of “memory plus skills”: memory supplies material for skills, and skills in turn constrain the write format and retrieval granularity of memory.
2.3 Design Principles
- Gate the writes: only verified conclusions are allowed into the long-term layer, which prevents noise from being solidified.
- Layer the retrieval: check skills first, then long-term memory, and fall back to raw conversations only as a last resort.
- Standardize the structure: each memory entry should contain at least three fields—trigger condition, conclusion, and scope of applicability—to support later automation.
3. Core Documents: The “Constitution” of the Execution Architecture
3.1 Why Documents Come Before Skills
In the execution architecture hierarchy, core documents have higher priority than skill creation. Skills answer “what can be done”; documents answer “under what rules it is done.” When constraints are missing, more skills only make behavior less predictable.
3.2 Three Key Documents
- Entry document (AGENTS-style): acts as the hub that links the other documents. It must specify the execution flow and rules, the skill orchestration mechanism, the reporting format, the memory system architecture, and the fault-tolerance mechanism. It determines the agent’s default behavior at the start of any task.
- Skill pyramid document: describes skill tiers and promotion and demotion rules. It is a dynamic analysis view, not a static label.
- P and not-P table: records user preferences and boundaries. It is a continuously growing set of decision boundaries.
An illustrative structure:
# Entry Document- Execution flow: understand → plan → execute → verify → report- Skill orchestration: call by pyramid tier; the experimental tier must be declared explicitly- Reporting format: conclusion first, followed by changes and risks- Fault tolerance: retry limit on failure, fallback paths, conditions for human intervention
4. Skill Tiering: Managing Call Frequency and Value with a Pyramid
Dividing skills into four tiers by call frequency and value is an effective way to control complexity and improve stability:
- Foundation tier (about 55%): file operations, terminal commands, and web access—high frequency and stable.
- General tier (about 30%): debugging and root-cause analysis, planning, and code review—reused across projects.
- Specialized tier (about 15%): machine learning workflows, creative design, and code hosting platform operations—used in specific scenarios.
- Experimental tier (about 5%): new and draft skills, where trial and error is allowed.
Example promotion and demotion rules:
Experimental → Specialized: called more than 3 times within 30 days and rated usable by a humanSpecialized → General: validated in 3 different projects and abstractable into a general workflow
The value of this mechanism is that the skill library neither grows without bound nor stays stuck in draft status. Instead, it keeps evolving based on real usage data.
5. The P and not-P Table: Turning Corrections into Decision Boundaries
Combining Daoist thinking with set theory, you can build a boundary table of “P and not-P.” The core flow is: the user states P (what they want), the agent reasons about not-P (what they do not want and where the boundaries are), records it in the table, and hits the entry directly next time.
Three key design points:
- Formed in pairs: P and not-P are semantic opposites, with no positive or negative side.
- Reversible lookup: if the user prefers not-P on some occasion, the original P automatically becomes the new not-P. No new entry is needed; just look up the table in reverse.
- Automatic growth: after each correction, the agent summarizes on its own and writes it into the table, forming a preference boundary that keeps converging.
Illustrative structure:
P: concise output, conclusion first | not-P: long preamble, repeated backgroundP: explain the impact scope before changes | not-P: direct large-scale refactoring
6. Rollout Advice and Common Pitfalls
Roll out in the order “documents first, memory next, skills last.” Start by writing the entry document to lock down the flow and reporting format, then connect the memory plugin to complete the four-layer chain, and finally migrate and tier skills step by step. There are three common pitfalls. The first is treating memory as a log that is only written to and never distilled. The second is prioritizing the number of skills while ignoring call frequency data. The third is writing boundary records as static dogma, which loses the reversible and growth capabilities.
If stronger engineering support is needed, general cloud capabilities can carry memory and logs: use object storage to save long-term memory snapshots, use a message queue to decouple memory writes, and use logging and monitoring with alerting to track call quality. The overall goal is clear—make the agent remember less but remember it firmly, and always act within established boundaries.
