Skip to main content

Installation

pip install memproof

1. Create a Policy File

Create memproof.yaml in your project root:
version: 0.1.0
mode: enforce

defaults:
  on_policy_miss: deny
  on_adapter_error: quarantine
  require_idempotency: true

risk_thresholds:
  low_max: 0.30
  medium_max: 0.60
  high_max: 0.80
  critical_max: 1.00

rules:
  - id: allow_trusted_reads
    description: Allow reads from trusted sources.
    enabled: true
    priority: 100
    action: allow
    reason_codes: [SAFE_READ]
    match: all
    when:
      - field: operation_type
        operator: in
        value: [search, get]
      - field: context.source
        operator: in
        value: [langgraph, openai_sessions, mcp]

  - id: allow_low_risk_writes
    description: Allow low-risk writes from trusted sources.
    enabled: true
    priority: 150
    action: allow
    reason_codes: [TRUSTED_LOW_RISK]
    match: all
    when:
      - field: operation_type
        operator: in
        value: [remember, update, forget]
      - field: context.source
        operator: in
        value: [langgraph, openai_sessions, mcp]
      - field: risk_level
        operator: in
        value: [low, medium]

  - id: approve_high_risk
    description: Require approval for high-risk mutations.
    enabled: true
    priority: 200
    action: require_approval
    reason_codes: [HIGH_RISK]
    match: all
    when:
      - field: operation_type
        operator: in
        value: [remember, update, forget]
      - field: risk_level
        operator: in
        value: [high, critical]

2. Initialize Memproof

import asyncio
from memproof import Memproof

async def main():
    mp = Memproof(policy="./memproof.yaml")

    # Create a memory
    result = await mp.remember(
        content="user prefers dark mode",
        scope={
            "tenant_id": "acme-corp",
            "project_id": "chatbot",
            "agent_id": "assistant-1",
        },
        context={
            "actor_type": "agent",
            "actor_id": "assistant-1",
            "source": "langgraph",
            "timestamp": "2026-01-15T10:30:00Z",
        },
        tags=["preference", "ui"],
    )

    print(f"Status: {result.status}")           # committed
    print(f"Decision: {result.decision.action}") # allow
    print(f"Risk: {result.risk_assessment.level}")  # low
    print(f"Memory ID: {result.memory.memory_id}")

    # Read it back
    memory = await mp.get(result.memory.memory_id)
    print(f"Content: {memory.content}")

    # Search
    search = await mp.search(
        query="dark mode",
        scope={"tenant_id": "acme-corp", "project_id": "chatbot", "agent_id": "assistant-1"},
        context={"actor_type": "agent", "actor_id": "assistant-1",
                 "source": "langgraph", "timestamp": "2026-01-15T10:31:00Z"},
    )
    print(f"Found {len(search.hits)} results")

asyncio.run(main())

3. Handle Policy Decisions

from memproof import Memproof, PolicyDeniedError, QuarantinedError

mp = Memproof(policy="./memproof.yaml")

try:
    result = await mp.remember(
        content="some sensitive data",
        scope=scope,
        context=context,
    )
    if result.status == "pending_approval":
        print(f"Waiting for approval: {result.operation_id}")
        # Later: await mp.approve(result.operation_id, actor_id="admin-1")
    else:
        print(f"Committed: {result.memory.memory_id}")

except PolicyDeniedError as e:
    print(f"Blocked by policy: {e.code}")

except QuarantinedError as e:
    print(f"Quarantined for review: {e.details}")

4. Use a Real Adapter

# LangGraph
mp = Memproof(
    policy="./memproof.yaml",
    adapter="langgraph",
    langgraph_url="http://localhost:8123",
    langgraph_api_key="your-key",
)

# OpenAI Sessions
mp = Memproof(
    policy="./memproof.yaml",
    adapter="openai_sessions",
    openai_api_key="sk-...",
)

# MCP Memory Server
mp = Memproof(
    policy="./memproof.yaml",
    adapter="mcp",
    mcp_server_url="http://localhost:3333",
)

Next Steps