Standalone SurrealDB Memory Operations

Add, delete, and replace user memories directly in SurrealDB with a standalone MemoryManager.

standalone_memory_surreal.py
"""
Standalone SurrealDB Memory Operations
"""

from agno.db.surrealdb import SurrealDb
from agno.memory import MemoryManager, UserMemory
from rich.pretty import pprint

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
SURREALDB_URL = "ws://localhost:8000"
SURREALDB_USER = "root"
SURREALDB_PASSWORD = "root"
SURREALDB_NAMESPACE = "agno"
SURREALDB_DATABASE = "memories"

creds = {"username": SURREALDB_USER, "password": SURREALDB_PASSWORD}
db = SurrealDb(None, SURREALDB_URL, creds, SURREALDB_NAMESPACE, SURREALDB_DATABASE)


# ---------------------------------------------------------------------------
# Create Memory Manager
# ---------------------------------------------------------------------------
memory = MemoryManager(db=db)


# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------
def run_example() -> None:
    # Add a memory for the default user
    memory.add_user_memory(
        memory=UserMemory(memory="The user's name is John Doe", topics=["name"]),
    )
    print("Memories:")
    pprint(memory.get_user_memories())

    # Add memories for Jane Doe
    jane_doe_id = "jane_doe@example.com"
    print(f"\nUser: {jane_doe_id}")
    memory_id_1 = memory.add_user_memory(
        memory=UserMemory(memory="The user's name is Jane Doe", topics=["name"]),
        user_id=jane_doe_id,
    )
    memory_id_2 = memory.add_user_memory(
        memory=UserMemory(memory="She likes to play tennis", topics=["hobbies"]),
        user_id=jane_doe_id,
    )
    memories = memory.get_user_memories(user_id=jane_doe_id)
    print("Memories:")
    pprint(memories)

    # Delete a memory
    print("\nDeleting memory")
    assert memory_id_2 is not None
    memory.delete_user_memory(user_id=jane_doe_id, memory_id=memory_id_2)
    print("Memory deleted\n")
    memories = memory.get_user_memories(user_id=jane_doe_id)
    print("Memories:")
    pprint(memories)

    # Replace a memory
    print("\nReplacing memory")
    assert memory_id_1 is not None
    memory.replace_user_memory(
        memory_id=memory_id_1,
        memory=UserMemory(memory="The user's name is Jane Mary Doe", topics=["name"]),
        user_id=jane_doe_id,
    )
    print("Memory replaced")
    memories = memory.get_user_memories(user_id=jane_doe_id)
    print("Memories:")
    pprint(memories)


if __name__ == "__main__":
    run_example()

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno surrealdb

Run SurrealDB

Install and start Docker first. This local demonstration uses the agno namespace and memories database with development credentials.

docker run -d --rm --name surrealdb --pull always -p 8000:8000 surrealdb/surrealdb:latest start --user root --pass root

Run the example

Save the code above as standalone_memory_surreal.py, then run:

python standalone_memory_surreal.py

Full source: cookbook/integrations/surrealdb/standalone_memory_surreal.py