Local-First Long-Term Memory Plugin for AI Agents: Design and Practice
This article presents a local-first design for a long-term memory plugin for AI agents, called "motion memory." It explains why cross-session continuity is the real bottleneck for customizable agent frameworks, compares three common approaches, and details a plugin mechanism that uses local storage as the single source of truth and edge compute for periodic memory management. You will find the four design constraints, installation steps, a four-layer traceability model, the capture-scoring-consolidation-persistence pipeline, context assembly and retrieval strategy, and notes on privacy, operations, and current limitations.
1. Background: Why Agents Need “Motion Memory”
Customizable AI coding tools and agent frameworks are no longer limited by conversation capability. The real short board is cross-session continuity. Conclusions confirmed repeatedly in a long conversation, a user’s coding preferences, hard project constraints, and pitfalls hit in a specific environment all tend to disappear when the session ends. The next conversation starts from zero, wasting context budget and making the assistant look like it does not understand you.
There are usually three ways to address this. The first is to stuff all history into the context, which is expensive and quickly hits the limit. The second is to rely on a cloud-hosted memory service, which sends data outside your environment, bills by usage, and leaves privacy and cost out of your control. The third is to externalize memory as local documents and let a local model handle consolidation. This article discusses the third approach, which we call “motion memory”—the name reflects continuous motion and gradual accumulation. In essence, it is a plugin mechanism that treats local storage as the single source of truth and uses edge compute for periodic memory management.
It fits agent runtime frameworks with high customizability (referred to below as DSH-style frameworks) and does two things: automatically distills content worth keeping into local memory documents, and maintains a long-term memory that gets to know you better over time through conversation tracking plus periodic summarization. Storage stays local, the model stays local, and everything is controllable and inspectable.
2. Design Principles: Local-First and Cost Control
2.1 Why Not Use a Paid Large Model for Memory Consolidation
Memory consolidation works by assembling context and prompting a model: each round stitches the currently relevant memory fragments into the prompt, then asks the model to extract, deduplicate, and merge. This means cache hit rates are very low and nearly every call is a full input. With a token-billed cloud model, cost grows linearly or even super-linearly with memory volume.
So the selection principle is: consolidation does not demand deep reasoning, and a local small model in the 7B–9B range (loaded through a local inference runtime) is fully sufficient. Reserve the expensive compute for the main model that writes code and performs reasoning, and hand the dirty work to the local small model. That is the cost foundation of this architecture.
2.2 Four Hard Constraints
- Data stays in your environment: memory documents are plain text on local disk, version-controllable and hand-editable.
- Explainable and traceable: every memory can be traced back to its source session and time, avoiding black-box memory.
- Can be disabled and cleared: memory is an enhancement, not a dependency, and can be turned off or wiped at any time.
- Structure before intelligence: guarantee usability with clear structure first, then talk about model capability.
3. Installation and Enablement: Two Paths
3.1 Manual Placement (Recommended for the First Time)
The whole thing has three parts: a memory core plugin (intercepts session events and triggers consolidation), a settings UI plugin (entry script, frontend script, and package manifest), and a set of pure-function modules (side-effect-free logic for parsing, scoring, merging, and so on).
Key steps:
- Put the core plugin and the pure-function modules into the plugin directory of the runtime framework profile.
- The settings UI plugin loads its frontend part as a “package,” so create a symlink or copy in the profile’s dependency directory (use a directory junction command on Windows, a symlink command on Unix-like systems).
- Register the two plugin entries in the profile’s patch configuration, like:
- insert:- id: motion-memoryname: ./plugins/motion-memory.js- id: mm-settingsname: mm-settings
- Restart the framework, and the memory tools and settings panel take effect automatically.
3.2 Let the Running Assistant Install It
Handing the steps above to the assistant in the current session works too: it can read directories, write configuration, and create links. The value of this path is verifying whether the assistant can operate its own runtime environment, but it also requires you to keep reviewing configuration file changes. Back up the profile configuration first.
4. Memory Structure: A Four-Layer Traceability Model
Memory is not one large all-inclusive text block but a four-layer structure, traceable layer by layer:
- Raw layer: original fragments archived per session, append-only, the final basis.
- Item layer: atomic memories extracted from the raw text, with type (conclusion/preference/constraint/method/lesson), body, source pointer, timestamp, and confidence.
- Topic layer: item sets aggregated by project or domain, forming “the full story of this project.”
- Summary layer: high-level views produced by periodic summarization, for fast context injection.
Retrieval locates top-down and gathers evidence bottom-up: use the summary layer to judge relevance, drill down to the item layer for evidence, and go back to the raw layer to verify when necessary. This controls injection size while keeping conclusions grounded.
5. Runtime Mechanism: Conversation Tracking and Periodic Summarization
The pipeline has four steps:
- Capture: listen for session end or stage checkpoints and grab candidate fragments.
- Score: apply heuristic rules (does it contain conclusion words, was it confirmed by the user, does it recur) to filter out small talk and one-off chatter.
- Consolidate: the local small model extracts and normalizes candidates, merges synonymous items, and on conflict keeps the new version while marking the old one.
- Persist: write to the item layer and topic layer, and trigger summary rebuilding on a cycle (such as every N sessions or daily).
Illustrative prompt structure (pseudocode):
[current memory summary]{summary}[relevant items]{retrieved}[new fragment]{turn}Return a JSON array with fields: type,text,source,confidence; return an empty array if there is no value.
Engineering recommendations: fix the output schema for easy parsing; validate model output against the schema and fall back to rule-based extraction on failure; cap the number of items injected per call to avoid context bloat.
6. Context Assembly and Retrieval Strategy
Injection follows a “minimal sufficient” principle: the summary layer is always present but length-limited (within 300 characters, for example), the item layer takes Top-K by semantic and keyword relevance to the current task, and the raw layer is not injected by default. For conflicting information, use “new overrides old + keep history,” and record versions on items so you can trace “why it was decided this way back then.”
7. Privacy, Security, and Operations
- Fully local: no outbound requests, suitable for internal code and compliance-sensitive projects.
- Auditable: all writes can be diffed, and it is recommended to put the memory directory under version control.
- Least privilege: the plugin only reads and writes the designated directory and avoids scanning the whole disk.
- Fallback on failure: consolidation failure does not affect the main session, and the agent runs in no-memory mode when memory is missing.
8. Limitations and Collaboration
Typical limitations of this early implementation: extraction quality is bounded by the small model’s capability, long-cycle data accumulation is not fully validated, and edge cases (multiple projects in parallel, frequent context switching) still need polish. A reasonable expectation is to treat it as a “runnable, modifiable, co-buildable” skeleton: usable features and clear structure, with contributions via issues and code welcome to make the flow solid.
9. Conclusion
The key to long-term memory is not how large the model is, but whether the structure is clear, the sources are checkable, and the cost is controllable. With local documents as the source of truth, edge compute for periodic consolidation, and a four-layer structure for traceability, this motion memory approach offers a pragmatic starting point for agent continuity, and leaves room for later on-demand expansion in environments such as Baidu AI Cloud (for example, object storage for archiving, log services for auditing, and monitoring and alerting to track consolidation task health).
