Reading Memory
Agents read memory to access past results, user preferences, and prior context. Good memory retrieval uses specific keys and returns focused data, not entire memory dumps.
6 min•By Priygop Team•Updated 2026
Memory Read Patterns
- Exact key lookup: retrieve a specific value by its key — fastest and most reliable
- Filtered query: find records matching certain criteria (e.g., orders for a specific customer)
- Recency query: retrieve the most recent N records (e.g., last 5 agent actions)
- Semantic search: retrieve memories by meaning (e.g., 'find what we know about Django')
- Aggregation: calculate counts, sums, or averages from stored data
Selective Memory Access
Selective Memory Access
# Read only what the agent needs — avoid dumping entire state
class AgentMemory:
def __init__(self):
self._store = {}
def write(self, namespace: str, key: str, value):
"""Write a value to a namespaced key."""
full_key = f"{namespace}:{key}"
self._store[full_key] = value
def read(self, namespace: str, key: str, default=None):
"""Read a specific value."""
return self._store.get(f"{namespace}:{key}", default)
def read_all(self, namespace: str) -> dict:
"""Read all values in a namespace."""
prefix = f"{namespace}:"
return {
k[len(prefix):]: v
for k, v in self._store.items()
if k.startswith(prefix)
}
def exists(self, namespace: str, key: str) -> bool:
return f"{namespace}:{key}" in self._store
memory = AgentMemory()
# Research agent stores what it finds
memory.write("research", "django_summary", "Django is a batteries-included web framework...")
memory.write("research", "fastapi_summary", "FastAPI is a modern async web framework...")
memory.write("user:U001", "preferred_format", "bullet_points")
# Later — read only what's needed
django_info = memory.read("research", "django_summary")
user_format = memory.read("user:U001", "preferred_format")
print("Django:", django_info[:40])
print("Format:", user_format)
print("All research:", list(memory.read_all("research").keys()))Key Takeaways
- Agents read memory to access past results, user preferences, and prior context.
- Exact key lookup: retrieve a specific value by its key — fastest and most reliable
- Filtered query: find records matching certain criteria (e.g., orders for a specific customer)
- Recency query: retrieve the most recent N records (e.g., last 5 agent actions)