Skip to main content

Scoped Memory Store (MemoryStore)

MemoryStore holds reusable knowledge for an agent across conversations. The agent can search existing entries and, when useful, save new facts through its memory tools. It is in memory by default. Supply a persistence store and namespace to reload and save entries through the shared Rust persistence engine. Use the namespace to isolate an agent, project, or tenant; use scopes to organize categories inside that namespace. User-specific preferences are an optional use case, not the default memory model.

from ish import MemoryStore, ScopedMemoryEntry

# Initialize with seed memories
mem = MemoryStore(initial_entries=[
ScopedMemoryEntry(key="deployment_policy", value="Review migrations before rollout", scope="agent"),
ScopedMemoryEntry(key="primary_region", value="eu-west-1", scope="project"),
])

# Store new memories
mem.store("rollback_check", "Verify the previous image remains available", scope="agent")
mem.store("service_owner", "platform-team", scope="project")

# Retrieve by key, scope
policy = mem.get("deployment_policy", scope="agent")

# Substring search across scopes
for entry in mem.search("budget"):
print(f"[{entry.scope}] {entry.key} = {entry.value}")

The constructor stays in-memory unless persistence is explicitly supplied. Durable stores use the same SQLite WAL or StorageConnector backend as agent traces. Recreate a store with the same namespace after restart to recover its entries. Context entries and memories are stored separately even when they share one persistence handle.


Find saved entries​

search(query, scope?) performs case-insensitive substring matching against each entry's key and content. A scope argument limits results to that exact scope; results follow insertion order. Short queries work, but search does not rank by meaning or infer related terms. Store concise notes with useful keys, then search with words likely to appear in the note. For passage retrieval over larger documents, use DocumentIndex instead.


Keep memories separate by scope​

scope is a free-form category matched by exact equality, defaulting to "agent" when omitted. It separates categories of knowledge, not access or retention: persistence is controlled by the configured backend and authorization remains the application's responsibility. Two entries may share the same key under different scopes without overwriting one another. A lookup without a scope returns the earliest matching entry.

ScopeCommon use
agentAgent-owned operating rules, learned tool notes, and reusable heuristics. This is the default scope.
projectDecisions, environment facts, and conventions shared by agents working on one project.
domainKnowledge reused by agents with the same specialist role.
globalShared rules or constants when broad access is intentional.
userIndividual preferences when an application explicitly needs user-specific memory.

Isolate Memory Namespaces​

Use a distinct namespace for each agent or project that must have isolated long-term memory. scope is an exact-match category inside one namespace; it is not an authorization boundary. Enforce tenant access in the application and use distinct namespaces when separate tenants must not see one another's memory.


Memory APIs Across SDKs​

Notice that Rust's MemoryStore::store(scope, key, content) takes scope first, whereas Python, TypeScript, Go, C#, and Java take (key, value/content, scope).

An unscoped get(key) returns the value from the most recently written scope for that key. Use get(key, scope=...) when the exact scope matters; scoped lookups never fall through to another scope.


Agent Memory Tools​

When attached to an agent, MemoryStore avoids injecting full entry payloads into every prompt. Instead, get_system_prompt_block() emits only a compact index header:

[Available Memories (N items): \key1`, `key2`. Use `recall_memory` to fetch details.]`

Two Rust-backed tools let the agent manage reusable knowledge during a run:

  • recall_memory(query, scope?) (read_only = true, parallel_safe = true): Runs recall_formatted(query, scope) and returns [SCOPE] key: content lines joined by newlines.
  • persist_memory(key, content, scope?) (mutates = true, parallel_safe = false): Writes into MemoryStore under scope (or default_scope, "agent") and updates the trigram index immediately.

This makes long-term memory agent-managed in normal use: the model can recall relevant notes and proactively save stable findings without a person manually writing each entry. Saving still happens through an explicit tool call. The runtime does not silently turn every message into a memory, and durable storage still requires a persistence store and namespace. The model may decide not to save a particular fact, so provide clear instructions or approval rules when your application requires predictable memory updates.

Python (Agent(..., memory=mem)) and Rust (Agent::builder(model).memory(mem)) attach both tools and the prompt key-list block automatically; TypeScript, Go, C#, and Java wire agentTools() / attachTools(...) alongside systemPromptBlock() on the agent or request.

import asyncio

from ish import Agent, MemoryStore


async def main() -> None:
mem = MemoryStore()
mem.store("deployment_policy", "Review migrations before rollout", scope="agent")

agent = Agent(
name="assistant",
model="gpt-4o-mini",
instructions="Use saved deployment guidance. Save stable new rollout rules when useful.",
memory=mem,
)
try:
result = await agent.run("What should be checked before deployment?")
print(result.content)
finally:
agent.close()
mem.close()


asyncio.run(main())