> ## Documentation Index
> Fetch the complete documentation index at: https://memproof.kyberon.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# mem0 Integration

> Using mem0 as a memory backend with Memproof's control layer

[mem0](https://mem0.ai) is a memory storage and retrieval system for AI applications. Memproof can sit in front of mem0, adding policy evaluation, risk assessment, content redaction, and audit logging to every memory operation. Your agents continue to use mem0 for storage, but Memproof enforces governance before anything is persisted or retrieved.

## Configuration

Set the adapter to `mem0` and provide your mem0 credentials. Memproof will route all memory operations through the mem0 API while applying the full control path.

<Tabs>
  <Tab title="Python">
    ```python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
    from memproof import Memproof

    mp = Memproof(
        policy="./memproof.yaml",
        adapter="mem0",
        mem0_api_key="your-api-key",
        mem0_org_id="your-org-id",
        mem0_project_id="your-project-id",
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
    import { Memproof } from "@kyberon/memproof";

    const mp = new Memproof({
        policy: "./memproof.yaml",
        config: {
            adapter: "mem0",
            mem0ApiKey: "your-api-key",
            mem0OrgId: "your-org-id",
            mem0ProjectId: "your-project-id",
        },
    });
    ```
  </Tab>
</Tabs>

## How It Works

When you call `mp.remember()`, `mp.search()`, or any other memory operation, Memproof executes the following pipeline before reaching mem0:

<Steps>
  <Step title="Auth check">
    If an auth hook is configured, it validates the caller before anything else runs.
  </Step>

  <Step title="Content redaction">
    PII and secrets are stripped from the content using the `ContentRedactor`.
  </Step>

  <Step title="Risk assessment">
    The risk engine scores the operation based on content signals, scope, and operation type.
  </Step>

  <Step title="Policy evaluation">
    The policy engine evaluates rules against the operation context. The result is `allow`, `deny`, `require_approval`, or `quarantine`.
  </Step>

  <Step title="mem0 execution">
    If the policy allows the operation, Memproof calls the mem0 API to persist or retrieve the memory.
  </Step>

  <Step title="Audit logging">
    An immutable event is recorded in the Trailproof audit trail with the full decision trail.
  </Step>
</Steps>

## Usage Example

Once configured, use Memproof exactly as you would with any other adapter. The mem0 backend is transparent to the caller.

<Tabs>
  <Tab title="Python">
    ```python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
    import asyncio
    from memproof import Memproof

    async def main():
        mp = Memproof(
            policy="./memproof.yaml",
            adapter="mem0",
            mem0_api_key="your-api-key",
            mem0_org_id="your-org-id",
            mem0_project_id="your-project-id",
        )

        # Store a memory -- Memproof evaluates policy, then persists via mem0
        result = await mp.remember(
            content="User prefers dark mode and metric units",
            scope={"tenant_id": "acme", "project_id": "assistant", "agent_id": "pref-bot"},
            context={"actor_type": "agent", "actor_id": "pref-bot",
                     "source": "api", "timestamp": "2026-01-15T10:00:00Z"},
        )
        print(result.status)  # "committed"

        # Search memories -- policy is evaluated on reads too
        hits = await mp.search(
            query="user display preferences",
            scope={"tenant_id": "acme", "project_id": "assistant", "agent_id": "pref-bot"},
            context={"actor_type": "agent", "actor_id": "pref-bot",
                     "source": "api", "timestamp": "2026-01-15T10:01:00Z"},
        )
        for hit in hits:
            print(hit.content, hit.score)

    asyncio.run(main())
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
    import { Memproof } from "@kyberon/memproof";

    const mp = new Memproof({
        policy: "./memproof.yaml",
        config: {
            adapter: "mem0",
            mem0ApiKey: "your-api-key",
            mem0OrgId: "your-org-id",
            mem0ProjectId: "your-project-id",
        },
    });

    const result = await mp.remember({
        content: "User prefers dark mode and metric units",
        scope: { tenantId: "acme", projectId: "assistant", agentId: "pref-bot" },
        context: { actorType: "agent", actorId: "pref-bot",
                   source: "api", timestamp: "2026-01-15T10:00:00Z" },
    });
    console.log(result.status); // "committed"
    ```
  </Tab>
</Tabs>

## Custom Mem0 Client

If you need to customize the HTTP client, connection pooling, or retry behavior, you can provide your own `Mem0Client` implementation and inject it into the adapter.

```python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
from memproof import Memproof
from memproof.config import MemproofConfig
from memproof.adapters.mem0 import Mem0Adapter, Mem0Client

class CustomMem0Client(Mem0Client):
    """Custom client with retry logic and connection pooling."""

    async def add(self, content: str, metadata: dict) -> dict:
        # Your custom implementation
        ...

config = MemproofConfig(policy_path="./memproof.yaml", adapter="mem0")
mp = Memproof(config=config)
mp._adapter = Mem0Adapter(client=CustomMem0Client(
    api_key="your-api-key",
    org_id="your-org-id",
    project_id="your-project-id",
))
```

<Warning>
  When providing a custom `Mem0Client`, ensure your implementation raises `AdapterError` with canonical error codes. The orchestrator depends on these codes to decide whether to quarantine, retry, or surface the error.
</Warning>

<Tip>
  The mem0 adapter maps Memproof scopes to mem0 metadata fields. `tenant_id`, `project_id`, and `agent_id` are stored as mem0 metadata so that scoped searches work correctly across both systems.
</Tip>
