Component · L4
Agent Memory
github.com/juanmmm21/agentic-memory-layer
A structured memory layer providing short-term and long-term vector memory for AI agents.
- Lines
- 302
- Files
- 6
- Test files
- 1
- Commits
- 9
- Started
- 26 Jun 2026
- Last commit
- 6 Jul 2026
- Python 100%
● Where it sits
Layer 4 of AI Core — agents.
L1 Data & input
L2 Representation & retrieval
L3 Inference & control
L4 Agents
L5 MLOps & quality
↳ Flagship
Feeds into
ReAct AgentsREADME · 3 min read
Commits per day
Persistent, structured memory layer for AI agents that remedies the context limitation of language models (amnesia) via a hybrid architecture: a short-term conversational buffer with automatic synthesis and a long-term episodic store with temporal decay based on the Ebbinghaus forgetting curve.
Hybrid Memory Architecture
The module implements a coordinated two-tier storage system to balance retrieval speed in active dialogue with the historical retention of relevant facts.
graph TD
Input[Agent Input / Conversation Turn] --> ShortTerm[Short-Term Memory: Conversational Buffer]
subgraph Short-Term Memory
ShortTerm --> LengthCheck{Turns > Max Limit?}
LengthCheck -->|Yes| Summarize[Extract Facts & Compact]
LengthCheck -->|No| KeepActive[Keep Turns Intact]
Summarize --> SaveLong[Persist Fact to Long-Term]
Summarize --> SummaryBuffer[Accumulated Summary History]
end
subgraph Long-Term Memory (Episodic)
SaveLong --> VectorGen[Local Embedding Generator]
VectorGen --> StorageCheck{NanoVectorDB available?}
StorageCheck -->|Yes| NanoVectorDB[HNSW Index in agent_long_term_memory.json]
StorageCheck -->|No| MemoryList[Fallback: In-Memory List]
end
Query[Agent Query / Search] --> RecallFlow[Retrieval Loop - Recall]
RecallFlow --> SearchVectors[Nearest Vector Search]
SearchVectors --> Ebbinghaus[Apply Exponential Forgetting Curve]
Ebbinghaus --> RankResults[Top-K Most Important Memories]1. Short-Term Memory (Conversational Buffer)
Keeps the latest interactions in their original message format (role and content).
- Capacity Limit: The buffer accepts a maximum number of turns ( double interactions, i.e. 10 messages total).
- Automatic Synthesis Algorithm: When the limit is exceeded, the system heuristically extracts the key facts from the older turns, leaving the last 2 turns (the most recent) intact in the buffer. The summarized information is appended to the
conversation_summarystring, which is prepended to the agent's prompt, freeing up useful space in the context window without breaking the session's thread of continuity.
2. Long-Term Memory (Episodic Decay Memory)
Permanently stores facts, associating each with a feature vector (embedding), a creation timestamp, and a subjective relevance factor.
The Exponential Forgetting Curve (Ebbinghaus Heuristic)
When performing a memory search to answer a user query, the system doesn't limit itself to evaluating vector semantic similarity. It applies an exponential temporal decay formula to relevance to mimic biological memory:
Where:
- is the cosine similarity projected onto the range between the query vector and the indexed memory vector :
- is the importance weight originally assigned to the fact when it was stored.
- is the forgetting-speed coefficient (
decay_factor, defaults to0.05). - represents the memory's age in seconds.
Under this model, old memories with low importance are "forgotten" (their score tends to zero), while critical facts with high importance or extremely recent and semantically related memories dominate the ranking.
Deterministic Embedding Generator (Local Word-Hash)
To operate offline with no external dependencies, the module locally computes a unit vector of dimension :
- Splits the fact's text into words using the regular expression
\w+. - Computes the MD5 hash of each word and calculates the corresponding modular index:
- Increments the vector's value at position : .
- Applies L2 normalization to the resulting vector to project it onto the surface of a unit hypersphere, allowing the dot product to represent exact cosine similarity.
Connection to the Ecosystem
This component connects to the infrastructure's modules as follows:
- nano-vector-db: Acts as the physical store for episodic memory. When
save_factis invoked, the memory saves the vector and its metadata usingNanoVectorDB.insert(id, vector, metadata)and persists the data to disk under theagent_long_term_memory.jsonfile. - orchestra-agents: Each autonomous agent (Researcher, Writer) uses an instance of this memory to persist its ReAct execution scratchpad and retrieve historical contextual information from past runs.
Project Structure
memory.py: Implementation of theAgenticMemoryclass, the short-term buffer with its synthesis algorithm, the local embedding hasher, and the Ebbinghaus temporal scoring engine.test_memory.py: Asynchronous unit test suite that validates the buffer's compaction rate, the consistency of the hash embeddings, and the accuracy of the exponential decay rate against elapsed time.example.py: Interactive demo simulating the passage of time (30 seconds and 10 minutes) that illustrates how the score of old memories decays and which ones are retained by the agent.
Installation and Usage
1. Initialize the Environment and Install Dependencies
Navigate to the directory and install the required local dependencies:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
2. Run Unit Tests
The suite validates that the temporal decay logic and HNSW persistence exactly match the theory:
.venv/bin/python -m unittest test_memory.py
3. Run the Forgetting Curve Demo
Run the demo to visualize how memory scores decay over time:
.venv/bin/python example.py