Skip to main content
This guide walks you through every step needed to go from zero to a working MemLayer integration. By the end, your agent will be able to store memories about a user, retrieve the most relevant ones with a natural-language query, and inject them into a system prompt — all with just a few lines of Python.
1

Get your API key

Visit memlayer.online and create a free account. Activation is instant and no credit card is required. Once you’re in the dashboard, copy your API key — it looks like ml_live_xxx. Keep it somewhere safe; you’ll use it in the next step.
2

Install the SDK

Install the MemLayer Python SDK from PyPI using pip.
pip install memlayer-py
The SDK supports Python 3.10 and above and has no heavy dependencies. It exposes both a synchronous client (MemLayerClient) and an async client (AsyncMemLayerClient) from the same memlayer package.
3

Initialize the client

Import MemLayerClient and create an instance with your API key and the MemLayer base URL. Storing the key in an environment variable is strongly recommended — see the Authentication page for the full pattern.
import os
from memlayer import MemLayerClient

client = MemLayerClient(
    api_key=os.environ.get("MEMLAYER_API_KEY"),
    base_url="https://memlayer.online"
)
The client is stateless and thread-safe, so you can create it once at startup and reuse it across your application.
4

Store a memory

Call client.remember() to store a piece of information about a specific user and agent pairing. Pass a memory_type to tell MemLayer how to classify and score the memory, and an importance float between 0.0 and 1.0 to indicate how significant this memory is relative to others.
result = client.remember(
    "User prefers dark mode and concise replies",
    user_id="user_123",
    agent_id="support_bot",
    memory_type="semantic",
    importance=0.8
)

print(result.id)           # e.g. "mem_a1b2c3d4"
print(result.is_duplicate) # False — this is a new memory
The response includes:
  • id — the unique identifier for this memory, which you can use with update() or forget() later.
  • is_duplicateTrue when MemLayer detected that a near-identical memory already exists, so no new record was written. Use this flag to avoid noisy duplicate storage in high-frequency pipelines.
5

Retrieve memories

Call client.recall() with a natural-language query to fetch the memories most relevant to your current context. MemLayer scores candidates using hybrid semantic and importance ranking and returns them in descending order of relevance.
memories = client.recall(
    "what does this user prefer?",
    user_id="user_123",
    agent_id="support_bot"
)

for memory in memories:
    print(memory.content, memory.score)
# "User prefers dark mode and concise replies"  0.94
Each result exposes content (the original text), score (the combined relevance score), memory_type, and id. Use score to filter out low-confidence memories before injecting them into a prompt.
6

Load session context

Call client.context() at the start of every session to fetch the highest-scoring memories for a user and agent pairing. The response object exposes a .memories list that you can loop over to build your system prompt.
context = client.context(
    user_id="user_123",
    agent_id="support_bot",
    top_k=10,
    # Optionally pass the user's opening message for smarter retrieval
    current_message="I need help with my order",
)

# Build a personalised system prompt from the retrieved memories
memory_lines = "\n".join(f"- {m.content}" for m in context.memories)

system_prompt = (
    "You are a helpful support assistant.\n\n"
    "What you remember about this user:\n"
    + memory_lines
)

print(system_prompt)
context() ranks and selects the most relevant memories automatically, so you don’t need to filter recall() results yourself when building a prompt. Use recall() directly when you need access to raw scores or other memory metadata.
The free plan includes 500 memories and 100 requests per day at no cost. If you hit those limits, upgrade to the Pro plan (50,000 memories, 10,000 requests/day) or Enterprise plan for unlimited usage. View all plan details and manage billing at memlayer.online/billing.

Core Concepts

Understand episodic, semantic, and summary memory types and when to use each one in your agent.

Guides

Explore practical patterns for storing and retrieving memories in real agent pipelines.