๐ง Agent Memory OptimizationJune 21, 2026โ
Tests passing
Compact Memory Store
A lightweight Python library for storing and retrieving AI agent memory snapshots in a memory-efficient, compact format using techniques like deduplication and compression. This tool is ideal for developers looking to reduce memory footprint and speed up memory access in long-running agent workflows.
What It Does
- Efficient Deduplication: Avoids storing duplicate memory entries.
- Compression Support: Reduces storage size using the
lz4compression library. - Fast Retrieval: Includes an in-memory cache with configurable size for quick access to frequently used memory entries.
Installation
To install the required dependencies, run:
pip install numpy==1.23.5 lz4==4.3.2Usage
Example
from compact_memory_store import MemoryStore
# Initialize the memory store
store = MemoryStore(cache_size=50)
# Add memory
store.add_memory('key1', {'input': 'hello', 'response': 'world'})
# Retrieve memory
print(store.retrieve_memory('key1')) # Output: {'input': 'hello', 'response': 'world'}
# Delete memory
store.delete_memory('key1')
print(store.retrieve_memory('key1')) # Output: None
# Clear all memory
store.clear_memory()CLI Usage
You can also interact with the memory store using the command line interface:
python compact_memory_store.py add --key key1 --data "{'input': 'hello', 'response': 'world'}"
python compact_memory_store.py retrieve --key key1
python compact_memory_store.py delete --key key1
python compact_memory_store.py clearSource Code
import os
import pickle
import lz4.frame
from collections import OrderedDict
import numpy as np
class MemoryStore:
def __init__(self, cache_size=100):
"""
Initialize the MemoryStore.
Args:
cache_size (int): Maximum number of items to keep in the in-memory cache.
"""
self.memory = {}
self.cache = OrderedDict()
self.cache_size = cache_size
def add_memory(self, key, data):
"""
Add a memory snapshot to the store.
Args:
key (str): Unique identifier for the memory.
data (object): The memory data to store (e.g., dict, list, string).
"""
if not isinstance(key, str):
raise ValueError("Key must be a string.")
serialized_data = pickle.dumps(data)
compressed_data = lz4.frame.compress(serialized_data)
self.memory[key] = compressed_data
# Update cache
self._update_cache(key, data)
def retrieve_memory(self, key):
"""
Retrieve a memory snapshot by its key.
Args:
key (str): Unique identifier for the memory.
Returns:
object: The decompressed memory data.
"""
if key in self.cache:
return self.cache[key]
compressed_data = self.memory.get(key)
if compressed_data is None:
return None
serialized_data = lz4.frame.decompress(compressed_data)
data = pickle.loads(serialized_data)
# Update cache
self._update_cache(key, data)
return data
def _update_cache(self, key, data):
"""
Update the in-memory cache with the given key and data.
Args:
key (str): The key to update in the cache.
data (object): The data to cache.
"""
if key in self.cache:
self.cache.pop(key)
elif len(self.cache) >= self.cache_size:
self.cache.popitem(last=False)
self.cache[key] = data
def delete_memory(self, key):
"""
Delete a memory snapshot by its key.
Args:
key (str): Unique identifier for the memory.
"""
if key in self.memory:
del self.memory[key]
if key in self.cache:
del self.cache[key]
def clear_memory(self):
"""
Clear all stored memory and cache.
"""
self.memory.clear()
self.cache.clear()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Compact Memory Store CLI")
parser.add_argument("action", choices=["add", "retrieve", "delete", "clear"], help="Action to perform on the memory store.")
parser.add_argument("--key", type=str, help="Key for the memory entry.")
parser.add_argument("--data", type=str, help="Data to store (as a string).")
args = parser.parse_args()
store = MemoryStore()
if args.action == "add":
if args.key is None or args.data is None:
print("Error: Both --key and --data are required for 'add' action.")
else:
store.add_memory(args.key, args.data)
print(f"Memory added with key: {args.key}")
elif args.action == "retrieve":
if args.key is None:
print("Error: --key is required for 'retrieve' action.")
else:
data = store.retrieve_memory(args.key)
if data is None:
print("No memory found for the given key.")
else:
print(f"Retrieved memory: {data}")
elif args.action == "delete":
if args.key is None:
print("Error: --key is required for 'delete' action.")
else:
store.delete_memory(args.key)
print(f"Memory deleted with key: {args.key}")
elif args.action == "clear":
store.clear_memory()
print("All memory cleared.")Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- compact_memory_store
- Category
- Agent Memory Optimization
- Generated
- June 21, 2026
- Tests
- Passing โ
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-06-21/compact_memory_store cd generated_tools/2026-06-21/compact_memory_store pip install -r requirements.txt 2>/dev/null || true python compact_memory_store.py