Memory System

LibreFang's memory system provides persistent storage, semantic search, and knowledge graph functionality.


Overview

The LibreFang memory system includes:

  • SQLite Persistence - Structured KV storage
  • Vector Embeddings - Semantic search capability
  • Knowledge Graph - Entities and relationships
  • Session Management - Cross-channel memory
  • Usage Tracking - Cost and usage statistics

Architecture

┌─────────────────────────────────────────┐
              Agent Loop
└─────────────────┬───────────────────────┘


┌─────────────────────────────────────────┐
           Memory Subsystem
├─────────────────────────────────────────┤
  ┌─────────┐  ┌─────────┐  ┌─────────┐
 Session Vector  │Knowledge│
 Store Search Graph
  └─────────┘  └─────────┘  └─────────┘
├─────────────────────────────────────────┤
           SQLite Database
└─────────────────────────────────────────┘

Configuration

Basic Configuration

[memory]
decay_rate = 0.05
sqlite_path = "~/.librefang/data/librefang.db"

Advanced Configuration

[memory]
decay_rate = 0.05
sqlite_path = "~/.librefang/data/librefang.db"
vector_dimension = 1536
max_memory_items = 10000
auto_compact = true
FieldTypeDefaultDescription
decay_rateFloat0.05Memory confidence decay rate
sqlite_pathString~/.librefang/data/librefang.dbDatabase path
vector_dimensionInteger1536Vector dimension
max_memory_itemsInteger10000Maximum memory entries
auto_compactBooleantrueAuto compaction

Session Management

Create Session

# Create new session
librefang session create --name "research-project"

List Sessions

# List all sessions
librefang session list

Session Operations

# View session details
librefang session info <session-id>

# Delete session
librefang session delete <session-id>

# Compact session
librefang session compact <session-id>

# Export session
librefang session export <session-id> --format json

Memory Operations

Store Memory

# Store simple memory
librefang memory store --key "user:preference:theme" --value "dark"

# Store structured data
librefang memory store --key "project:details" --data '{"name": "LibreFang", "version": "1.0"}'

# Store with tags
librefang memory store --key "note:1" --value "Important note" --tags "work,urgent"

Search Memory

# Keyword search
librefang memory search "project"

# Vector search (semantic search)
librefang memory search --vector "find information about AI agents"

# Search with filters
librefang memory search "meeting" --tags "work" --limit 10

Memory Operations

# Read memory
librefang memory get <key>

# Update memory
librefang memory update <key> --value "new value"

# Delete memory
librefang memory delete <key>

# List all memories
librefang memory list --prefix "project:"

LibreFang supports semantic search with vector embeddings:

# Semantic search example
librefang memory search --vector "machine learning techniques for text classification"

Similarity Threshold

[memory]
similarity_threshold = 0.75

API Endpoint

# Semantic search API (GET with query params)
curl "http://127.0.0.1:4545/api/memory/search?q=find+information+about+AI&limit=10&threshold=0.8"

Knowledge Graph

Entity Management

# Add entity
librefang kg add-entity --type "person" --name "John Doe" --properties '{"role": "developer"}'

# List entities
librefang kg list-entities --type "person"

# Search entities
librefang kg search-entities "John"

Relationship Management

# Add relationship
librefang kg add-relation \
  --from "person:john" \
  --relation "works_at" \
  --to "company:acme"

# List relationships
librefang kg list-relations --entity "person:john"

# Query relationships
librefang kg query --from "person:john" --relation "works_at"

Graph Queries

# Query path
librefang kg path --from "person:alice" --to "company:acme"

# Query subgraph
librefang kg subgraph --entity "person:bob" --depth 2

Session Compaction

Auto Compaction

Automatic compaction when session message count reaches threshold. Compaction settings are managed internally by the runtime and are not exposed in config.toml. The default threshold is 80 messages, keeping the 20 most recent verbatim and summarizing the rest.

Manual Compaction

# Compact session
librefang session compact <session-id>

# Compact all sessions
librefang session compact-all

# View compaction status
librefang session compaction-status

Compaction Algorithm

  1. Keep Recent N Messages
  2. Extract Key Information
  3. Generate Summary
  4. Keep Tool Call History

Usage Tracking

View Usage

# View usage statistics
librefang usage

# View Agent usage
librefang usage --agent <agent-id>

# View provider usage
librefang usage --provider

Cost Tracking

# View costs
librefang cost

# View costs by date range
librefang cost --from 2025-01-01 --to 2025-01-31

# Export report
librefang cost export --format csv

API Endpoints

KV Memory Operations

EndpointMethodDescription
/api/memory/searchGETSearch memory (query params: q, limit, threshold)
/api/memoryPOSTStore a memory entry
/api/memory/{id}GETGet a specific memory entry
/api/memory/{id}DELETEDelete a memory entry

Session Operations

EndpointMethodDescription
/api/memory/sessionsGETList sessions
/api/memory/sessions/{id}GETGet session details
/api/memory/sessions/{id}/compactPOSTCompact session

Knowledge Graph

EndpointMethodDescription
/api/memory/kg/entitiesGETList entities
/api/memory/kg/relationsGETList relations
/api/memory/kg/queryPOSTQuery graph

Proactive Memory

Proactive memory allows agents to autonomously surface, consolidate, and recall long-term knowledge without explicit tool calls.

EndpointMethodDescription
/api/memory/proactiveGETList all proactive memory entries
/api/memory/proactivePOSTCreate a proactive memory entry
/api/memory/proactive/{id}GETGet a proactive memory entry
/api/memory/proactive/{id}DELETEDelete a proactive memory entry
/api/memory/proactive/searchGETSemantic search over proactive memories
/api/memory/proactive/consolidatePOSTTrigger consolidation of recent memories

Best Practices

  1. Regular Compaction - Prevent sessions from growing too large
  2. Use Tags - Easy organization and search
  3. Set Decay Rate - Control memory confidence
  4. Monitor Usage - Track costs and usage

Troubleshooting

# Rebuild vector index
librefang memory reindex

# Check index status
librefang memory index-status

Database Bloat

# Clean old data
librefang memory cleanup --older-than 30d

# Vacuum database
librefang memory vacuum

Memory Loss

# Check database integrity
librefang doctor

# Restore backup
librefang memory restore --backup <path>