For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
DiscordPlatform
DocumentationIntegrationsAPI referenceSDKsChangelog
DocumentationIntegrationsAPI referenceSDKsChangelog
  • Integrations
    • Overview
      • Mem0
      • Hyperspell
      • Cognee
LogoLogo
DiscordPlatform
On this page
  • Setup
  • Add memory with the OpenAI SDK
  • Search memories
  • Use the Respan SDK for type-safe params
IntegrationsMemory

Mem0

Was this page helpful?
Previous

Hyperspell

Next
Built with

Mem0 is a self-improving memory layer for LLM applications, enabling personalized AI experiences. Use it with Respan to add memory to your AI product and route LLM calls through the Respan gateway for full observability over every memory add, search, and generation.

Set up Respan

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

Run npx @respan/cli setup to set up with your coding agent.

Gateway
Mem0 doesn’t expose its own SDK-level tracing instrumentor; route all calls through the Respan gateway to get full observability automatically.

Setup

1

Prerequisites

  • A Respan API key
  • A Mem0 API key
2

Install Mem0

$pip install mem0ai
3

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export MEM0_API_KEY="YOUR_MEM0_API_KEY"
4

Add memory with Mem0 SDK

Configure Mem0’s LLM provider to use the Respan gateway.

1import os
2from mem0 import Memory
3
4config = {
5 "llm": {
6 "provider": "openai",
7 "config": {
8 "model": "gpt-4.1-nano",
9 "temperature": 0.0,
10 "api_key": os.environ["RESPAN_API_KEY"],
11 "openai_base_url": "https://api.respan.ai/api/",
12 },
13 }
14}
15
16m = Memory.from_config(config_dict=config)
17
18result = m.add(
19 "I like to take long walks on weekends.",
20 user_id="alice",
21 metadata={"category": "hobbies"},
22)
23print(result)

Add memory with the OpenAI SDK

Use the OpenAI SDK pointed at the Respan gateway and pass mem0_params via extra_body to add or search memories alongside the LLM call.

1import os
2from openai import OpenAI
3
4client = OpenAI(
5 api_key=os.environ["RESPAN_API_KEY"],
6 base_url="https://api.respan.ai/api/",
7)
8
9messages = [
10 {"role": "system", "content": "You are a helpful assistant."},
11 {"role": "user", "content": "Context: I like eating carrots, and I like to play basketball."},
12]
13
14response = client.chat.completions.create(
15 model="gpt-4.1-nano",
16 messages=messages,
17 extra_body={
18 "mem0_params": {
19 "user_id": "user_1",
20 "org_id": "org_1",
21 "api_key": os.environ["MEM0_API_KEY"],
22 "add_memories": {"messages": messages},
23 }
24 },
25)
26print(response.choices[0].message.content)

Search memories

Pass search_memories to retrieve relevant memories before generation.

1response = client.chat.completions.create(
2 model="gpt-4.1-nano",
3 messages=[
4 {"role": "system", "content": "You are a helpful assistant."},
5 {"role": "user", "content": "Based on {{mem0_search_memories_response}}, what is the user's favorite food?"},
6 ],
7 extra_body={
8 "mem0_params": {
9 "user_id": "user_1",
10 "org_id": "org_1",
11 "api_key": os.environ["MEM0_API_KEY"],
12 "search_memories": {
13 "query": "What is the user's favorite food?",
14 "top_k": 1,
15 "fields": ["text"],
16 "rerank": True,
17 "keyword_search": True,
18 "filter_memories": True,
19 },
20 }
21 },
22)

Use the Respan SDK for type-safe params

$pip install respan-sdk
1import os
2from openai import OpenAI
3from respan_sdk.respan_types.services_types.mem0_types import (
4 Mem0Params, AddMemoriesParams, SearchMemoriesParams,
5)
6
7client = OpenAI(
8 api_key=os.environ["RESPAN_API_KEY"],
9 base_url="https://api.respan.ai/api/",
10)
11
12memory_msgs = [
13 {"role": "system", "content": "You are a helpful assistant."},
14 {"role": "user", "content": "Context: I like eating carrots."},
15]
16
17response = client.chat.completions.create(
18 model="gpt-4.1-nano",
19 messages=memory_msgs,
20 extra_body={
21 "mem0_params": Mem0Params(
22 user_id="user_1",
23 org_id="org_1",
24 api_key=os.environ["MEM0_API_KEY"],
25 add_memories=AddMemoriesParams(messages=memory_msgs),
26 ).model_dump(exclude_none=True),
27 },
28)