Show HN: Memory system for AI agents with associations, forgetting, synthesis

Share

Formative Memory for OpenClaw Agents

A deep dive into the long‑term memory plugin that mimics biological memory


1. Introduction

The OpenClaw platform has rapidly become a favorite among researchers and developers who want to create sophisticated, AI‑powered conversational agents. Yet, like most contemporary language‑model interfaces, OpenClaw agents struggle with a persistent “short‑term” perspective: every prompt is processed in isolation, and the model has no direct access to the conversation’s history beyond what the user explicitly re‑injects.

Enter Formative Memory – an OpenClaw plugin that endows agents with biologically inspired long‑term memory. It lets agents retrieve, contextualise, and learn from past interactions in a way that resembles how humans store and retrieve information over time.

This article explores the plugin’s architecture, implementation details, user experience, and the broader implications for AI research and product development. It is based on a comprehensive 18 k‑character feature piece, expanded and reframed to give readers an in‑depth, practical understanding of how Formative Memory works and why it matters.


2. The Problem: Short‑Term Memory in Conversational AI

2.1. The Short‑Term Limitation

Most language models, including the one that powers OpenClaw, are stateless by design. The only context they carry forward is the text you feed them at each turn. While this works fine for quick, single‑shot queries, it forces developers to manually manage dialogue history. Even when a conversation is truncated to fit within the model’s context window, the model can’t remember preferences, facts, or emotional states beyond what is explicitly recalled.

2.2. Consequences for Users and Developers

  • User Frustration: If an agent forgets a user’s name or preferences mid‑conversation, the experience feels disjointed and untrustworthy.
  • Developer Burden: Maintaining a persistent state (e.g., a database of user profiles) and injecting it into each prompt increases complexity and can introduce bugs.
  • Learning Deficiency: Agents cannot improve over time from their own interactions; each session starts from scratch, limiting the potential for fine‑tuned personalization.

Formative Memory tackles these pain points by providing a memory store that agents can query before every response, mimicking the retrieval processes in biological brains.


3. Biological Inspiration: How Humans Store Long‑Term Memory

3.1. The Role of Retrieval

In human cognition, memory is not a static archive but a dynamic process that involves retrieving relevant traces when needed. The hippocampus consolidates new experiences into the cortex, where they become accessible for future recall. When you need to answer a question, your brain scans this network for related memories, and the retrieved fragments shape your response.

3.2. Analogous Mechanism in Formative Memory

Formative Memory emulates this pattern by:

  1. Encoding: Every user message and agent response is stored as a memory trace.
  2. Indexing: Traces are indexed by semantic content using a vector embedding technique.
  3. Retrieval: Before generating a new reply, the plugin retrieves the top‑k most relevant traces based on the current prompt.
  4. Integration: The retrieved traces are formatted into a prompt template that the model consumes, guiding the response to incorporate historical context.

This cycle of encode‑index‑retrieve‑integrate mirrors the way humans access past experiences to inform present decisions.


4. Architecture Overview

Below is a high‑level diagram of Formative Memory’s workflow:

User Input -> (Encode + Store) -> Memory Store
                ^
                |
                |
            (Retrieve) <-- (Agent Input)
                |
                v
   Prompt Template (with retrieved memories)
                |
                v
            OpenAI LLM
                |
                v
          Agent Response
                |
                v
           (Encode + Store)

4.1. Core Components

| Component | Responsibility | |-----------|----------------| | Memory Store | A lightweight, in‑memory database (SQLite or Redis) that holds encoded messages and responses. | | Encoder | Uses a pre‑trained transformer (e.g., Sentence‑BERT) to convert text into dense vectors. | | Indexing Engine | Builds an approximate nearest‑neighbour (ANN) index (HNSW) for fast retrieval. | | Retrieval Module | Accepts the current prompt, queries the index, and returns top‑k relevant memories. | | Prompt Builder | Formats the retrieved memories into a structured prompt that the LLM can understand. | | Agent Wrapper | Handles the OpenClaw integration, passing the prompt to the LLM and receiving the response. | | Lifecycle Manager | Controls when to encode, index, and delete memories (e.g., age‑based pruning). |

4.2. Extensibility

Formative Memory is designed to be plug‑and‑play. Developers can swap out:

  • Encoder Models: Replace Sentence‑BERT with any transformer that produces embeddings.
  • Indexing Strategies: Switch from HNSW to Faiss or ScaNN depending on performance needs.
  • Persistence Layers: Persist memories to disk or cloud storage for durability across restarts.

5. Detailed Workflow

Let’s walk through a typical conversation that illustrates each step of Formative Memory.

5.1. Conversation Start

User: “Hey, I’m looking for a good Italian restaurant in Brooklyn.”

Agent: “Sure! I recommend “Luca’s Pizzeria” on 4th Avenue.”

5.1.1. Encoding and Storing the Interaction

  1. The plugin encodes the user’s request and the agent’s reply into vector embeddings.
  2. It stores both as separate memory traces:
  • Trace A: “User: Italian restaurant Brooklyn” → embedding E_A.
  • Trace B: “Agent: Luca’s Pizzeria” → embedding E_B.
  1. Both traces are indexed in the ANN graph.

5.2. Follow‑Up Prompt

User: “Is it open on Sundays?”

Before generating a response, the plugin:

  1. Encodes the current prompt “Is Luca’s Pizzeria open on Sundays?” into embedding E_Q.
  2. Queries the ANN index, retrieving the top‑k traces that are semantically similar. Likely results:
  • Trace B (restaurant recommendation).
  • Trace A (initial user request).
  1. Builds a prompt template:
Memories:
1. User asked for an Italian restaurant in Brooklyn.
2. Agent recommended Luca’s Pizzeria.

User: Is it open on Sundays?
Agent:
  1. Passes this to the LLM, which produces a contextually informed answer.

5.3. Continuous Learning

Each new turn is again encoded, stored, and indexed. Over time, the agent builds a personalized knowledge base of the user’s preferences, past queries, and the agent’s own responses. The memory store can also learn negative traces (e.g., “the user disliked the recommendation”), which the retrieval module can use to avoid repeating mistakes.


6. Implementation Highlights

6.1. Memory Storage Layer

The plugin uses SQLite by default because of its zero‑configuration nature and sufficient performance for moderate workloads. For high‑volume applications, the memory store can be swapped for Redis or PostgreSQL.

Schema Example:

CREATE TABLE memories (
    id INTEGER PRIMARY KEY,
    content TEXT NOT NULL,
    embedding BLOB NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    category TEXT DEFAULT 'generic'   -- e.g., 'user', 'agent', 'system'
);

6.2. Encoding Pipeline

  • Model: sentence-transformers/all-MiniLM-L6-v2
  • Preprocessing: Truncate to 256 tokens, remove boilerplate, keep semantic content.
  • Vector Dimensionality: 384‑dimensional vectors.

Encoding is performed asynchronously to avoid blocking the agent’s response generation.

6.3. ANN Indexing with HNSW

  • Library: hnswlib
  • Parameters:
  • M = 16 (number of bi‑directions per node)
  • efConstruction = 200
  • efSearch = 50

These settings balance recall and query speed for up to 10,000 memories. For larger corpora, Faiss or ScaNN can be used.

6.4. Retrieval Strategy

  • Top‑k: By default, the plugin retrieves the top 5 memories.
  • Filtering: Exclude traces older than a configurable threshold (e.g., 180 days).
  • Relevance Scoring: Uses cosine similarity on the embeddings.
  • Deduplication: The module ensures that duplicate memories are collapsed, preventing repetitive information.

6.5. Prompt Formatting

The plugin offers several templates:

| Template | Use‑case | |----------|----------| | Concise | “User: … Agent: …” | | Context‑Rich | “Memories: … User: … Agent: …” | | Question‑Answer | “Q: … A: …” |

Developers can choose a template that best fits their conversational style. The plugin automatically injects retrieved memories in the order of relevance.

6.6. Lifecycle Management

  • Decay: Memories have a soft decay; older memories gradually become less likely to be retrieved unless heavily relevant.
  • Pruning: A scheduled job deletes traces older than the retention window or that exceed a memory quota.
  • Archival: Optionally, memories can be exported to JSON or CSV for audit or analysis.

7. Use Cases & Applications

7.1. Personal Assistants

A personal assistant can remember user preferences—favorite cuisines, recurring appointments, or preferred shopping lists—without the user having to re‑state them each time.

7.2. Customer Support Bots

Customer service agents benefit from memory by recalling prior tickets, customer sentiment, or past solutions, leading to faster resolutions and higher satisfaction.

7.3. Educational Tutors

An AI tutor can retain knowledge of a student’s progress, misconceptions, and learning style, allowing it to adapt subsequent lessons accordingly.

7.4. Creative Writing Partners

A writing companion can recall earlier plot points, character details, and stylistic choices, maintaining narrative coherence over long sessions.

7.5. Enterprise Knowledge Bases

Companies can embed Formative Memory into internal tools to let employees retrieve policy documents or procedural steps without navigating separate databases.


8. Benefits Over Existing Solutions

| Feature | Formative Memory | Existing Approaches | |---------|-------------------|---------------------| | Biologically inspired retrieval | ✔ | ❌ (often simple context concatenation) | | Asynchronous encoding | ✔ | ❌ (blocking) | | Configurable lifespan | ✔ | ❌ | | Modular architecture | ✔ | ❌ (monolithic) | | Open source plug‑and‑play | ✔ | ❌ (closed‑source) |

Unlike naive context‑concatenation, which quickly exhausts the token budget, Formative Memory selectively brings in only the most relevant memories. This not only keeps the prompt concise but also improves answer quality by grounding responses in factual history.


9. Performance & Scalability

9.1. Latency

  • Encoding: ~1.2 ms per message on an NVIDIA RTX 3060.
  • Retrieval: ~2.5 ms for top‑5 on a 10k‑memory index.
  • Overall Impact: <5 ms added latency per turn—well below human reaction time thresholds.

9.2. Throughput

A single instance can handle ~400 queries per second on modest hardware, scaling linearly with additional instances. Distributed deployment using Redis as a shared memory store enables horizontal scaling.

9.3. Resource Footprint

  • Memory Usage: 1 GB for 10,000 384‑dimensional embeddings.
  • CPU/GPU: CPU‑only encoding is feasible; GPU accelerates high‑volume encoding.

10. Security & Privacy Considerations

10.1. Data Protection

Since the memory store can contain sensitive user data, developers should:

  • Encrypt the database at rest using AES‑256.
  • Restrict access to the plugin via API keys or OAuth tokens.
  • Audit all read/write operations.

10.2. GDPR & CCPA Compliance

The plugin exposes a forget endpoint that deletes all traces associated with a given user ID. Developers must implement user consent mechanisms and provide a data export option.

10.3. Ethical Retrieval

The retrieval module can be configured to filter out disallowed content (e.g., hate speech) or avoid repeating toxic patterns. Developers should audit the memory store periodically to ensure compliance with policy constraints.


11. Limitations & Future Work

11.1. Limitations

  1. Limited Contextual Depth: While Formative Memory improves recall, it still cannot fully replace the model’s own reasoning about context.
  2. Embedding Quality: The semantic similarity metric depends heavily on the encoder’s performance; domain‑specific nuances may be missed.
  3. Single‑Language Bias: Current embeddings are English‑centric; multilingual support is an upcoming feature.

11.2. Planned Enhancements

  • Fine‑tuned Domain Encoders: Offer domain‑specific embeddings (e.g., legal, medical).
  • Hybrid Retrieval: Combine embedding‑based recall with keyword matching.
  • Adaptive Memory Allocation: Dynamically allocate more memory for high‑frequency users.
  • Cross‑Agent Sharing: Allow agents to share memory traces for collaborative tasks.
  • Explainable Retrieval: Provide confidence scores and retrieval paths for auditability.

12. Getting Started

12.1. Installation

pip install openclaw-formative-memory

12.2. Configuration

Create a formative_memory.yaml:

memory_store:
  type: sqlite
  path: memories.db

encoder:
  model_name: sentence-transformers/all-MiniLM-L6-v2

index:
  type: hnsw
  params:
    M: 16
    efConstruction: 200
    efSearch: 50

retrieval:
  top_k: 5
  retention_days: 180

12.3. Integration Example

from openclaw import Agent
from formative_memory import FormativeMemory

agent = Agent(model="gpt-4o-mini")
memory_plugin = FormativeMemory(config="formative_memory.yaml")

@agent.on_message
def handle(msg, ctx):
    # Encode & store the incoming message
    memory_plugin.store(msg, category="user")

    # Retrieve relevant memories
    relevant = memory_plugin.retrieve(msg, top_k=5)

    # Build the prompt
    prompt = memory_plugin.build_prompt(msg, relevant)

    # Generate response
    response = agent.generate(prompt)

    # Store the response
    memory_plugin.store(response, category="agent")

    return response

This minimal example shows how the plugin seamlessly fits into the OpenClaw event loop.


13. Community & Ecosystem

Formative Memory has sparked a vibrant open‑source community. Contributors are developing:

  • Custom Embedding Models for niche domains.
  • Visual Dashboards to monitor memory usage and retrieval statistics.
  • Benchmark Suites comparing retrieval‑based memory vs. baseline approaches.

If you’re interested in contributing or reporting bugs, visit the GitHub repository: https://github.com/openclaw/formative-memory.


14. Final Thoughts

Long‑term memory is the missing ingredient that has held back conversational AI from becoming truly human‑like. By borrowing principles from neuroscience, Formative Memory bridges the gap between stateless language models and the richly contextual interactions that people expect. Its modular, scalable design allows developers to integrate persistent memory without reinventing the wheel, while its privacy safeguards give businesses the confidence to deploy it in regulated environments.

The plugin is a clear sign that the AI community is moving beyond the “stateless, stateless” paradigm toward more context‑aware, memory‑rich agents. Whether you’re building a personal assistant, a support bot, or a creative partner, Formative Memory offers a principled way to give your agent the memory of a human and the speed of a machine.


15. Appendix

15.1. Glossary

| Term | Definition | |------|------------| | ANN | Approximate Nearest Neighbour, a technique for fast similarity search. | | HNSW | Hierarchical Navigable Small World graphs, an ANN index algorithm. | | Embedding | A dense vector representation of text capturing semantic meaning. | | Soft Decay | Gradual reduction of memory relevance over time without hard deletion. | | Context Window | The maximum number of tokens that a language model can process in a single pass. |

15.2. Useful Resources

  • OpenClaw Documentation: https://openclaw.io/docs
  • Sentence‑Transformers Library: https://www.sbert.net/
  • HNSWlib GitHub: https://github.com/nmslib/hnswlib
  • FAISS Indexing: https://github.com/facebookresearch/faiss

Word Count: ~2,800 words (approx.)

Read more