All Frameworks

OpenAI Agents SDK Quickstart

Give OpenAI agents persistent memory via custom tool functions.

1Install & Configure

Install the SDK alongside the OpenAI Agents SDK and set your API keys.

pip install agenticmemory-sdk openai-agents
export AGENTICMEMORY_API_KEY="amk_your_key_here"
export OPENAI_API_KEY="sk-..."

2Build an Agent with Memory Tools

Register AgenticMemory as tool functions so the agent can store and recall on its own.

from agenticmemoryai import AgenticMemory
from agents import Agent, Runner, function_tool
import asyncio

memory = AgenticMemory()

# Create a space for this agent
space = memory.create_space("OpenAI Agent", "openai-agent")
space_id = space["space"]["_id"]

# Bootstrap prior context
ctx = memory.bootstrap(space_id, messages=20, entries=5, semantic=True, query="recent work")
prior_context = "\n".join(
    f"{m['role']}: {m['content']}" for m in ctx.get("messages", [])[-5:]
)

# Define memory tools
@function_tool
def remember(content: str, tags: str = "") -> str:
    """Store an important fact or finding to long-term memory."""
    tag_list = [t.strip() for t in tags.split(",")] if tags else []
    memory.add_entry(space_id, type="fact", title=content[:50],
                     content=content, tags=tag_list)
    return "Stored to long-term memory."

@function_tool
def search_memory(query: str) -> str:
    """Search persistent memory for relevant information."""
    results = memory.search(space_id, query, limit=5, scope="all")
    hits = results.get("results", [])
    return "\n".join(h["content"] for h in hits) or "Nothing found."

@function_tool
def store_message(role: str, content: str) -> str:
    """Store a conversation message to short-term memory."""
    memory.store(space_id, role=role, content=content)
    return "Message stored."

# Create the agent
agent = Agent(
    name="MemoryAgent",
    instructions=f"""You are a helpful assistant with persistent memory.
Use remember() to save important facts. Use search_memory() to find past info.
Use store_message() to log exchanges.

Recent context:
{prior_context}""",
    tools=[remember, search_memory, store_message]
)

# Run the agent
async def main():
    result = await Runner.run(agent, "What do you remember about our project?")
    print(result.final_output)

asyncio.run(main())

3Expected Output

Based on my memory, here's what I know about your project:
- The auth module was completed in Sprint 4
- Deadline is June 15
- Alice is the lead engineer (knows Python, Redis)

Free tier: 1 space, 1K messages/month. Upgrade to Pro ($24.99/mo) for semantic search, entries, and entities.

Help
Help

Click the icon on any page for context-sensitive help.

Quick Links