What We’re Building
The agent follows a simple loop on every message:- User sends a message — a support question, a preference, a fact about themselves.
- Agent loads memory context —
client.context()fetches the most relevant memories for this user before the agent says a word. - Agent responds — the LLM generates a reply with full context of who this person is.
- Agent saves what it learned —
client.remember()stores the user’s message as an episodic memory. - Next session — the agent already knows the user. No re-introduction needed.
Prerequisites
Install the required packages:.env file with your API keys:
Get your Remem API key from the dashboard. See the authentication guide for details on key types and scopes.
Steps
1
Set up environment variables
Load your API keys from the
.env file using python-dotenv. Never hard-code keys in source files.2
Initialize both clients
Create a
RememClient for memory and an OpenAI client for the LLM. You also need an AGENT_ID — a stable string that scopes memories to this specific agent so different bots don’t share memories.3
Load context at session start
Before generating a response, call
client.context() to fetch the most relevant memories for this user. Passing current_message lets Remem use semantic search to surface memories most relevant to what the user just said.4
Build the system prompt with memory
Format the returned memories as bullet points and inject them into the system prompt. If there are no memories yet, tell the agent this is a first conversation so it doesn’t fabricate context.
5
Call the LLM
Pass the memory-enriched system prompt and the user’s message to the model:
6
Save what the user told you
After responding, store the user’s message as an episodic memory. It will be available in all future sessions.
Complete Example
Here’s the full agent in a single file:Test It Out
Run the script twice to see memory persistence in action. On the first run you introduce yourself:Improving Your Agent
Save important facts separately as semantic memories
Save important facts separately as semantic memories
When the agent detects a key fact about the user, store it as semantic memory with high importance so it always surfaces in future sessions:
Update memories when facts change
Update memories when facts change
When a user corrects something, find the old memory and update it in place rather than storing a contradiction:
Use TTL for temporary context
Use TTL for temporary context
Session-specific context — like an order number the user is asking about — should expire automatically rather than cluttering long-term memory:
What’s Next
LangGraph Integration
Add persistent memory to a LangGraph agent with two extra nodes
Error Handling
Handle Remem errors gracefully so your agent never crashes in production
Store API
Full reference for the remember endpoint — all parameters and response fields
Context API
Full reference for the context endpoint — ranked memory retrieval