Skip to main content

Privacy-first memory API for LLMs

Project description

rec0 — memory for any LLM

Give your AI a permanent memory in 3 lines of code.

PyPI version Python 3.9+ License: MIT

Install

pip install rec0

Quickstart

from rec0 import Memory

mem = Memory(api_key="r0_xxx", user_id="user_123")
mem.store("User prefers Python and dark mode")
context = mem.context("user preferences")
# inject context into your LLM prompt — done

That's it. context returns a bullet-list string ready to prepend to any system prompt.


Why rec0

rec0 Mem0
Privacy Data never leaves your servers Processed externally
Cost $0.002 / 1K ops ~$0.10 / 1K ops
Setup 3 lines OAuth + config
LLM support Any model OpenAI-first
GDPR 1 API call Manual
  • Privacy-first: embeddings and summaries run on YOUR infrastructure — no user data touches third-party APIs
  • LLM-agnostic: works with OpenAI, Anthropic, Gemini, Llama, Mistral — anything that takes a string
  • Memory lifecycle: automatic importance scoring, recall-count boosting, and time-based decay
  • GDPR compliant: right-to-erasure in one call (mem.delete_user())

Full API reference

Memory(user_id, api_key, app_id, base_url)

Parameter Type Default Description
user_id str required Your end-user identifier
api_key str $REC0_API_KEY Your rec0 API key
app_id str "default" Namespace for multi-app isolation
base_url str prod URL Override for self-hosting

Methods

mem.store(content)MemoryObject

Store a new memory. Auto-generates embedding and summary server-side.

m = mem.store("User is building a SaaS product in Python")
print(m.id)          # UUID
print(m.importance)  # starts at 1.0, increases with each recall

mem.context(query, limit=5)str

The most-used method. Returns a bullet-list string to inject into your LLM prompt.

context = mem.context("what does the user like", limit=5)
# "- User prefers Python and dark mode\n- User is building a SaaS product"

# Typical usage with OpenAI:
messages = [
    {"role": "system", "content": f"User context:\n{context}"},
    {"role": "user", "content": user_message},
]

mem.recall(query, limit=5)List[MemoryObject]

Returns memories ranked by semantic similarity. Use when you need scores or metadata.

memories = mem.recall("programming preferences", limit=3)
for m in memories:
    print(f"{m.content}  (score: {m.relevance_score})")

mem.list()List[MemoryObject]

All active memories for this user, ordered by creation time.

mem.delete(memory_id)None

Soft-delete a specific memory (retained for audit trail).

mem.delete_user()dict

GDPR right-to-erasure. Removes all memories for this user.

mem.export()dict

GDPR data export. Returns all memory data as a dictionary.

mem.ping()bool

Connectivity check. Returns True if the API is reachable.

if not mem.ping():
    print("rec0 API unreachable — check your key")

Error handling

from rec0 import Memory, Rec0Error, AuthError, RateLimitError, NotFoundError

mem = Memory(api_key="r0_xxx", user_id="user_123")

try:
    mem.store("User loves rec0")
except AuthError:
    print("Invalid API key — check REC0_API_KEY")
except RateLimitError as e:
    print(f"Rate limited — retry in {e.retry_after}s")
except NotFoundError:
    print("Memory not found")
except Rec0Error as e:
    print(f"Unexpected error: {e}")

Rate limits are handled automatically: rec0 will wait retry_after seconds and retry once before raising.


Async usage

Every method has an async equivalent via AsyncMemory:

import asyncio
from rec0 import AsyncMemory

async def main():
    mem = AsyncMemory(api_key="r0_xxx", user_id="user_123")
    await mem.store("User is a night-owl developer")
    context = await mem.context("when does the user work")
    print(context)

asyncio.run(main())

AsyncMemory uses httpx under the hood and is safe to use in FastAPI, Django async views, and any asyncio application.


Environment variables

Variable Description
REC0_API_KEY Your rec0 API key (used automatically if api_key= not passed)
REC0_BASE_URL Override the API base URL (optional, for self-hosting)
export REC0_API_KEY=r0_your_key_here
# api_key is now auto-loaded — no need to hardcode it
mem = Memory(user_id="user_123")

MemoryObject fields

Field Type Description
id str UUID
content str The original memory text
summary str | None Auto-generated summary
importance float 1.0–10.0; increases with recall
recall_count int Times this memory was recalled
relevance_score float | None Similarity score (recall only)
created_at datetime When stored
is_active bool False if deleted

Self-hosting

rec0 is open-source. Deploy your own instance on Railway, Fly, or any server:

git clone https://github.com/patelyash2511/memorylayer
# See README for Railway deployment instructions

Then point the SDK at your instance:

mem = Memory(
    api_key="your_key",
    user_id="user_123",
    base_url="https://your-instance.up.railway.app",
)

rec0.ai · docs · discord · twitter

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

memorylayer_py-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

memorylayer_py-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file memorylayer_py-0.1.0.tar.gz.

File metadata

  • Download URL: memorylayer_py-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for memorylayer_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e816c0ff63da7001ae906b4383c5bbac36cf355af06c77a5e4310e98d76cee1f
MD5 232d3bcad6e3d648f03f3c78693286e7
BLAKE2b-256 372153123947a9dc29a048351f5584d60bb1b8f34b4651d8763095869a812a53

See more details on using hashes here.

File details

Details for the file memorylayer_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: memorylayer_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for memorylayer_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48d1cb72aa923336daec6e0c4b57648afef82cebb5ed2b4d10ec45238971c137
MD5 18764930d2d5be79f0f90749ad26c63f
BLAKE2b-256 1bac54d805f622fbebcc0f20e25672d9fc91ab19468eea331a929ec6230f77be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page