Memory Tools (Mem0)
Spoon-toolkit provides Mem0-powered tools that plug into spoon-core agents for long-term memory. They wrap spoon_ai.memory.mem0_client.SpoonMem0 and expose a consistent tool interface. ToolResult.output carries the raw Mem0 response (not a formatted string); validation failures or an unready client return ToolResult.error (e.g., “Client not ready”, “No content provided.”) instead of raising.
Available tools
AddMemoryTool— store text or conversation snippets.SearchMemoryTool— semantic/natural-language search over stored memories.GetAllMemoryTool— list memories (with paging/filters).UpdateMemoryTool— update an existing memory by memory_id.DeleteMemoryTool— delete a memory by memory_id.
All tools accept mem0_config (api_key/user_id/collection/metadata/filters/etc.) or an injected SpoonMem0 client. If mem0ai or MEM0_API_KEY is missing, client initialization may fail; otherwise an unready client yields ToolResult.error rather than an exception.
Parameter merging behavior:
user_iddefaults tomem0_config.user_id/agent_id(or the client’s user_id) and is also injected into filters if missing.collection,metadata, andfiltersfrom the injected client/config are merged into each call; per-call metadata/filters override on conflict.async_modeis only forwarded when passed toAddMemoryTool.execute; it is not auto-propagated frommem0_configby these wrappers (rely onSpoonMem0defaults if needed).
Quick usage (agent-side)
from spoon_ai.agents.spoon_react import SpoonReactAI
from spoon_ai.chat import ChatBot
from spoon_ai.tools.tool_manager import ToolManager
from spoon_toolkits.memory import (
AddMemoryTool, SearchMemoryTool, GetAllMemoryTool, UpdateMemoryTool, DeleteMemoryTool
)
MEM0_CFG = {
"user_id": "defi_user_001",
"metadata": {"project": "demo"},
"async_mode": False, # sync writes to avoid read-after-write delays
"collection": "demo_mem",
}
class MemoryAgent(SpoonReactAI):
mem0_config = MEM0_CFG
available_tools = ToolManager([
AddMemoryTool(mem0_config=MEM0_CFG),
SearchMemoryTool(mem0_config=MEM0_CFG),
GetAllMemoryTool(mem0_config=MEM0_CFG),
UpdateMemoryTool(mem0_config=MEM0_CFG),
DeleteMemoryTool(mem0_config=MEM0_CFG),
])
agent = MemoryAgent(
llm=ChatBot(llm_provider="openrouter", model_name="anthropic/claude-3.5-sonnet", enable_long_term_memory=False),
)
Demo flow (from spoon-core/examples/mem0_tool_agent.py)
The example walks through capture → recall → update → delete:
- Capture:
add_memorywith user preferences. Immediately verify withget_all_memory(sameuser_id) and thensearch_memoryto show stored content. - Recall: Build a fresh agent with the same
mem0_configandsearch_memoryto retrieve prior preferences. - Update:
add_memorynew preference +update_memorya specific record (by id) to reflect the pivot; thensearch_memoryagain to confirm recency. - Delete:
delete_memorythe updated record;get_all_memoryto show remaining memories.
Run the example:
python spoon-core/examples/mem0_tool_agent.py
Tool parameters (summary)
- Shared:
user_id,metadata,filters,collectioninherited viamem0_configor passed per-call. - Add:
contentormessages,role(defaultuser),async_mode. - Search:
query,limit/top_k, optional filters. - GetAll:
page,page_size/limit/top_k, filters. - Update:
memory_id(required),text,metadata. - Delete:
memory_id(required).
Best practices
- Keep a stable
user_id/collectionfor scoping; pass it explicitly on each call for consistency. - Use
async_mode=Falsewhen you need immediate read-after-write in demos/tests. - If you pass a custom
SpoonMem0viamem0_client, you can reuse a single client across tools to avoid repeated pings/initialization.