Skip to content
weego edited this page May 28, 2026 · 1 revision

Memory

LightAgent supports external memory backends through a minimal protocol. The framework does not force a specific vector database or memory provider.

Memory Protocol

A memory object should implement:

class CustomMemory:
    def store(self, data: str, user_id: str):
        ...

    def retrieve(self, query: str, user_id: str):
        ...

Then pass it to LightAgent:

agent = LightAgent(
    model="gpt-4.1",
    api_key="your_api_key",
    base_url="https://api.openai.com/v1",
    memory=CustomMemory(),
)

print(agent.run("Remember that I prefer short answers.", user_id="user_01"))
print(agent.run("How should you answer me?", user_id="user_01"))

Use a stable user_id. Memory quality and isolation depend on it.

Mem0 Example

Install Mem0:

pip install mem0ai

Adapter shape:

from mem0 import Memory

class Mem0Memory:
    def __init__(self):
        self.memory = Memory.from_config(config_dict={"version": "v1.1"})

    def store(self, data: str, user_id: str):
        return self.memory.add(data, user_id=user_id)

    def retrieve(self, query: str, user_id: str):
        return self.memory.search(query, user_id=user_id)

MemoryPolicy

For shared backends, use MemoryPolicy to scope memory calls and filter retrieved records that expose provenance:

from LightAgent import LightAgent, MemoryPolicy

agent = LightAgent(
    model="gpt-4.1",
    api_key="your_api_key",
    base_url="https://api.openai.com/v1",
    memory=memory_backend,
    memory_policy=MemoryPolicy(
        namespace="prod-tenant-a",
        allow_unattributed_results=False,
    ),
)

namespace prefixes the user_id sent to the backend. If retrieved memory items include user_id, userId, metadata.user_id, or metadata.userId, LightAgent filters out records that do not match the current user.

You can also use the shortcut:

agent = LightAgent(
    model="gpt-4.1",
    api_key="your_api_key",
    base_url="https://api.openai.com/v1",
    memory=memory_backend,
    memory_namespace="prod-tenant-a",
)

Production Guidance

  • Use separate namespaces per user, tenant, agent, and environment.
  • Do not mix trusted system facts and untrusted user claims in the same namespace.
  • Keep provenance metadata for memory writes.
  • Filter retrieval by user, tenant, agent, and trust level.
  • Treat retrieved memory as context, not as guaranteed truth.
  • For finance, healthcare, legal, or enterprise automation, require trusted source verification before making high-impact recommendations.

More detail: Memory Security Guidance.

Clone this wiki locally