Memory Fast and Slow for AI Agents
Project description
agent-memfas
Memory Fast and Slow for AI Agents
A dual-store memory system inspired by Kahneman's "Thinking, Fast and Slow" โ giving AI agents persistent, intelligent memory that survives context window limits.
๐ฏ Why memfas?
AI agents lose context. When conversations get long, older messages get compacted or dropped. Critical information vanishes:
User: "Let's continue the project"
Agent: "I apologize, but I don't have context about what project..."
memfas fixes this with persistent memory that lives outside the context window.
โจ Features at a Glance
-
[v0.1] Core Memory
- Type 1 (Fast) โ O(1) keyword triggers for instant recall
- Type 2 (Slow) โ FTS5 full-text search with BM25 ranking
- Zero dependencies โ works with SQLite built-in
-
[v0.2] Pluggable Backends
- Swappable search backends โ FTS5 or embeddings
- Semantic search โ FastEmbed or Ollama embeddings
- Auto-suggest triggers from indexed content
memfas reindexโ migrate between backends
-
[v0.3] Dynamic Context Curation
- Proactive memory selection each turn
- Topic detection โ tracks conversation topic and shifts
- Multi-factor relevance scoring โ semantic + recency + access patterns
- Token budget management โ fills budget with highest-value memories
- 84% token reduction โ 50K baseline โ 7.8K curated
- Telemetry โ JSONL logging, compression stats, latency tracking
-
[v0.3.1] Curation Levels
- 5-level slider from minimal to full context
- Level names: minimal / lean / balanced / rich / full
- Per-query level override
autolevel ready for smart selection
๐ Quick Start
Installation
pip install agent-memfas # Core (FTS5, zero deps)
pip install agent-memfas[embeddings] # + semantic search
pip install agent-memfas[v3] # + dynamic curation
pip install agent-memfas[all] # Everything
Basic Usage (30 seconds)
# Initialize
cd ~/my-agent && memfas init
# Add keyword triggers (Type 1)
memfas remember alice --hint "Project manager, prefers async communication"
memfas remember acme --hint "Client project, due Q2, React frontend"
# Index your memory files (Type 2)
memfas index ./MEMORY.md ./memory/
# Recall context
memfas recall "What did Alice say about the deadline?"
# โ Returns triggered + searched memories
Python API
from agent_memfas import Memory
# Initialize
mem = Memory("./memfas.yaml")
# Type 1: Instant triggers
mem.add_trigger("alice", "Project manager, prefers async")
# Type 2: Index and search
mem.index_file("./MEMORY.md")
results = mem.search("preference learning", limit=5)
# Combined recall
context = mem.recall("What did Alice say about the deadline?")
print(context) # Ready to inject into LLM prompt
With Semantic Search (v0.2+)
from agent_memfas import Memory
from agent_memfas.embedders.fastembed import FastEmbedEmbedder
# Local embeddings (~130MB model, runs on CPU)
mem = Memory(
"./memfas.yaml",
search_backend="embedding",
embedder=FastEmbedEmbedder()
)
# Now finds conceptually related content
results = mem.search("machine learning concepts")
With Dynamic Curation (v0.3+)
from agent_memfas.v3 import ContextCurator
curator = ContextCurator("./memfas.yaml")
# Get curated context within token budget
result = curator.get_context(
query="what's the project status?",
session_id="main",
baseline_tokens=50000 # Your context limit
)
print(f"Curated: {result.curated_tokens} tokens")
print(f"Saved: {result.tokens_saved} ({result.compression_ratio:.0%})")
print(result.context) # Inject this into your prompt
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ agent-memfas โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ v0.3: Context Curation โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ Topic โ โRelevance โ โ Token โ โ Session โ โ
โ โ Detector โโ โ Scorer โโ โ Budget โโ โ State โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ v0.2: Search Backends โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ FTS5Backend โ โEmbeddingBackend โ โ
โ โ (zero deps) โ โ (sqlite-vec) โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โ โโโโโโโโฌโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโ โ
โ โ SearchBackend โ โ Pluggable interface โ
โ โ ABC โ โ
โ โโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ v0.1: Core Memory โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ Type 1: Fast โ โ Type 2: Slow โ โ
โ โ Triggers โ โ Search โ โ
โ โ O(1) โ โ O(log n) โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Documentation
Configuration
Create memfas.yaml:
db_path: ./memfas.db
sources:
- path: ./MEMORY.md
type: markdown
- path: ./memory/*.md
type: markdown
triggers:
- keyword: alice
hint: "Project manager, prefers async"
- keyword: work
hint: "Current projects"
search:
max_results: 5
recency_weight: 0.3 # Favor recent memories
min_score: 0.1
CLI Reference
| Command | Description |
|---|---|
memfas init |
Initialize in current directory |
memfas recall <context> |
Recall memories (Type 1 + Type 2) |
memfas search <query> |
Search only (Type 2) |
memfas remember <kw> --hint <h> |
Add trigger |
memfas forget <keyword> |
Remove trigger |
memfas triggers |
List all triggers |
memfas index <paths...> |
Index files/directories |
memfas suggest |
Auto-suggest triggers from content |
memfas stats |
Show statistics |
memfas clear |
Clear indexed memories |
memfas curate <query> |
Get curated context (v0.3) |
memfas telemetry summary |
View performance stats (v0.3) |
Embedder Options
| Embedder | Install | Model | Notes |
|---|---|---|---|
| FastEmbed | pip install fastembed |
bge-small-en | Recommended, ~130MB |
| Ollama | ollama pull nomic-embed-text |
nomic-embed | Good if using Ollama |
๐ฌ How It Works
Type 1: Keyword Triggers (Fast Path)
Input: "What's the status on the acme project?"
โ
Trigger table scan: "alice" โ match!
โ
Return hint + linked memories instantly
Type 2: Search (Slow Path)
FTS5 (default):
Input: "preference learning papers"
โ
BM25 ranking + recency decay
โ
Top results by relevance
Embeddings:
Input: "machine learning concepts"
โ
Generate query embedding
โ
KNN search (cosine similarity)
โ
Semantically related results
v0.3: Dynamic Curation
Context: "Let's continue the project discussion"
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Detect topic: "project" โ
โ 2. Score all memories: โ
โ - Semantic relevance: 0.85 โ
โ - Recency: 0.92 โ
โ - Topic continuity: 0.78 โ
โ - Access pattern: 0.65 โ
โ 3. Fill 8000 token budget โ
โ 4. Return curated context โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
Result: 84% token reduction, focused context
๐งช Performance
| Metric | v0.1 | v0.2 | v0.3 |
|---|---|---|---|
| Trigger lookup | O(1) | O(1) | O(1) |
| FTS5 search | O(log n) | O(log n) | O(log n) |
| Embedding search | - | O(n) | O(n) cached |
| Token reduction | - | - | 84% |
| Warm query latency | - | - | 8ms (296x speedup) |
๐ค Integration
Clawdbot
## Memory (in AGENTS.md)
Before answering about prior work:
1. Run `memfas recall "<context>"`
2. Include returned context in reasoning
After compaction:
1. Run `memfas recall "current project"`
2. Check `memfas triggers`
Custom Agents
# In your agent loop
from agent_memfas.v3 import ContextCurator
curator = ContextCurator("./memfas.yaml")
def get_response(user_message):
# Get curated memory context
mem_result = curator.get_context(
query=user_message,
session_id="main",
baseline_tokens=100000
)
# Inject into prompt
prompt = f"""
{mem_result.context}
User: {user_message}
"""
return llm.complete(prompt)
๐ Resources
- Design Docs: See
/docsfor architecture decisions - Changelog: See releases for version history
- Issues: GitHub Issues
๐ License
MIT
Built for AI agents that need to remember. Inspired by losing context while building a memory system.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agent_memfas-0.3.1.tar.gz.
File metadata
- Download URL: agent_memfas-0.3.1.tar.gz
- Upload date:
- Size: 45.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6cfcb8951bbdeff4ff8129fbab7a64af02178bf06a00c2881f5d7c6b7d805c41
|
|
| MD5 |
306ac7c7839d8e78a141bf802cfc783c
|
|
| BLAKE2b-256 |
e6ee07ab74bcbedbd6d5df8b0042486f5db985b25cf6e5f629accb3abaa3c147
|
File details
Details for the file agent_memfas-0.3.1-py3-none-any.whl.
File metadata
- Download URL: agent_memfas-0.3.1-py3-none-any.whl
- Upload date:
- Size: 46.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6683f0c486eb28b64ced3443a869491be8bd8785ea0c6e2548b02c82bb544dba
|
|
| MD5 |
cee5b11f02a38507bca8b1a6f35f4f47
|
|
| BLAKE2b-256 |
ff9283b7235721ec4c063a0a0c905b4fd7ce797dd93dc29c33233db31800b936
|