How to Give Your AI Agent Memory A tutorial demonstrates building an AI agent with persistent long-term memory by combining the OpenAI Agents SDK with Mem0, installing both via `pip install openai-agents mem0ai` and giving the agent `search_memory` and `save_memory` function tools that query Mem0's semantic search with a per-user `USER_ID` and retrieve the top 5 results. The same memory system is then connected to the OpenCode coding agent so it retains preferences and context across sessions. The approach keeps prompts small by retrieving only memories relevant to the current request rather than replaying prior conversations. How to Give Your AI Agent Memory Learn how to give AI agents long-term memory using the OpenAI Agents SDK, Mem0, and MCP to remember preferences, context, and important information across sessions. AI agents can reason, use tools, and complete complex tasks. But there is one problem: most of them forget everything once the session ends. That means every new conversation can feel like starting from scratch. Agent memory gives an AI system a way to remember useful information across sessions , such as your preferences, previous decisions, goals, or important context. In this tutorial, we will build an AI agent with long-term memory using the OpenAI Agents SDK https://openai.github.io/openai-agents-python/ and Mem0 https://mem0.ai/ . We will then connect the same memory system to OpenCode https://opencode.ai/ so your coding agent can remember information across sessions too. How AI Agent Memory Works A simple agent memory system follows a straightforward loop: the user sends a message, the agent searches its memory for relevant context, uses that information while reasoning, and then saves anything useful for future conversations. There are two main types of memory: - Short-term memory: Information from the current conversation or task, such as recent messages and temporary state. - Long-term memory: Information that persists across sessions, such as preferences, decisions, goals, and previous experiences. The key part is retrieval . Instead of adding every previous conversation to the prompt, the agent searches its memory and brings back only the information that is relevant to the current request. This keeps the context smaller while still allowing the agent to behave as if it remembers previous interactions. Building an Agent with Long-Term Memory In this section, we will build a simple agent that can search, save, and reuse information across conversations. Start by installing the OpenAI Agents SDK and Mem0: pip install openai-agents mem0ai Next, set the API keys: export OPENAI API KEY="your-openai-key" export MEM0 API KEY="your-mem0-key" Then import the required classes and create a Mem0 client: python from agents import Agent, Runner, function tool from mem0 import MemoryClient memory = MemoryClient USER ID = "abid" The MemoryClient gives our agent access to Mem0, while USER ID tells Mem0 whose memories to store and retrieve. Using a separate identifier for each user keeps their memory isolated. Creating a Memory Search Tool Now we need to give the agent a way to look up relevant information from previous conversations. We do this by creating a search memory tool that sends the current query to Mem0 and returns the most relevant stored memories. php @function tool def search memory query: str - str: """Search for relevant memories about the user.""" results = memory.search query=query, filters={"user id": USER ID}, top k=5, memories = results.get "results", if not memories: return "No relevant memories found." return "\n".join f"- {item 'memory' }" for item in memories Mem0 uses semantic search, so the wording does not have to match exactly. As long as the meaning is similar, the agent can still retrieve the right memory. Creating a Memory Writing Tool Next, we need a way for the agent to save useful information for future conversations. We do this with a save memory tool that sends important facts, preferences, or decisions to Mem0. php @function tool def save memory content: str - str: """Save useful long-term information about the user.""" memory.add messages= { "role": "user", "content": content, } , user id=USER ID, return "Memory saved." Mem0 can then process that information and store the parts that are useful as long-term memory, rather than saving the entire conversation as one large block. Creating the Agent Now we can connect everything by creating the agent and giving it access to both memory tools. The agent is instructed to check for relevant memories before answering and to save useful information that should be remembered later. agent = Agent name="Memory Assistant", model="gpt-5.6-luna", instructions=""" You are a personal assistant with long-term memory. Always call search memory before answering, even for general questions. When a relevant memory is found, acknowledge it naturally at the start, for example: "Oh, I remember you prefer...", then use it. Save durable preferences, goals, and facts; confirm each save in one sentence. Skip greetings, temporary info, and duplicates. """, tools= search memory, save memory, , Here, the instructions control how the agent uses memory. They tell it what to retrieve, what to save, and what kinds of information should be ignored. Finally, create a small chat function to run the agent: python def chat message : result = Runner.run sync agent, message, return result.final output Each time chat is called, the message is passed to the agent, which can use the memory tools as part of its response. Testing the Memory Now let's check that the agent can actually remember something across interactions. First, give it a preference worth saving: print chat "Remember that I prefer Python and use uv " "for managing my projects." Output: I'll remember that you prefer Python and use uv to manage your projects. Next, ask a related question: print chat "How should I set up my next Python project?" Output: I remember you prefer Python and use uv for project management. A solid setup: bash uv init my-project cd my-project uv add