Skip to main content
Agents can accumulate lots of data. Memory limits cap storage and auto-evict old entries.

Basic Usage

from splinter.control import MemoryStore

store = MemoryStore(
    max_size_bytes=10*1024*1024,  # 10 MB
    max_entries=1000,
)

# Store data
store.set("researcher", "findings", large_data)

# Retrieve data
data = store.get("researcher", "findings")

Eviction Policies

from splinter.control import MemoryStore, EvictionPolicy

# LRU: Evict least recently used
store = MemoryStore(
    max_entries=1000,
    policy=EvictionPolicy.LRU,
)

# FIFO: Evict oldest first
store = MemoryStore(
    max_entries=1000,
    policy=EvictionPolicy.FIFO,
)

# LFU: Evict least frequently used
store = MemoryStore(
    max_entries=1000,
    policy=EvictionPolicy.LFU,
)

Per-Agent Limits

store = MemoryStore(
    max_size_bytes=50*1024*1024,  # 50 MB total
)

# Per-agent caps
store.set_agent_limit("researcher", max_size_bytes=20*1024*1024)
store.set_agent_limit("writer", max_size_bytes=10*1024*1024)

Memory Usage

# Check usage
usage = store.get_usage()
print(f"Total: {usage.total_bytes / 1024 / 1024:.1f} MB")
print(f"Entries: {usage.total_entries}")

# Per-agent usage
agent_usage = store.get_agent_usage("researcher")
print(f"Researcher: {agent_usage.bytes / 1024 / 1024:.1f} MB")

TTL (Time to Live)

Auto-expire old entries:
# Expire after 1 hour
store.set("researcher", "cache", data, ttl_seconds=3600)

# Check if expired
if store.is_expired("researcher", "cache"):
    # Refresh data
    pass

Memory Warnings

from splinter.exceptions import MemoryLimitError

try:
    store.set("researcher", "huge_data", very_large_data)
except MemoryLimitError as e:
    print(f"Memory limit exceeded: {e.current_bytes} / {e.limit_bytes}")

Cleanup

# Clear specific agent
store.clear_agent("researcher")

# Clear all
store.clear_all()

# Force garbage collection
store.gc()

Best Practices

10-50 MB per agent is usually enough. More = slower performance.
Don’t keep stale data forever.
If you’re hitting limits often, optimize what you’re storing.
LRU for caches, FIFO for logs, LFU for frequently accessed data.