← All Cookbooks
OpenAI Agents SDKBeginner10 min

OpenAI Agents SDK + HatiData: Agent Memory

Add persistent memory to OpenAI Agents SDK agents. Store conversation context and retrieve it with semantic search across sessions.

What You'll Build

An OpenAI Agents SDK agent with persistent memory backed by HatiData. The agent stores interactions and retrieves relevant past context using semantic search.

Prerequisites

$pip install openai-agents hatidata-agent

$hati init

$OpenAI API key

Architecture

┌──────────────┐    ┌──────────────┐    ┌──────────────┐
│  OpenAI      │───▶│  HatiData    │───▶│   Engine     │
│  Agents SDK  │    │  Memory API  │    │  + Vectors   │
└──────────────┘    └──────────────┘    └──────────────┘
     Persistent memory via SQL + vector search

Key Concepts

  • Memory-augmented agents: enrich agent prompts with relevant past context retrieved via semantic search from HatiData
  • Namespace scoping: organize memories by namespace (customer-context, conversations, etc.) to keep different knowledge domains separate
  • Semantic retrieval: semantic_match() and semantic_rank() find contextually relevant memories — not just keyword matches
  • Framework-agnostic: HatiData's SQL interface works with any agent framework — the same memory layer serves LangChain, CrewAI, AutoGen, and OpenAI agents

Step-by-Step Implementation

1

Install Dependencies

Install the OpenAI Agents SDK and the HatiData agent SDK.

Bash
pip install openai-agents hatidata-agent
hati init

Note: The OpenAI Agents SDK provides agent orchestration. HatiData provides persistent memory.

2

Add Memory to Your Agent

Create an agent that stores interactions in HatiData and retrieves relevant past context.

Python
from hatidata_agent import HatiDataAgent

hati = HatiDataAgent(host="localhost", port=5439, agent_id="openai-agent")

# Store memories from past interactions
hati.execute("""
    SELECT store_memory(
        'Customer acme-corp prefers quarterly billing and has a dedicated account manager',
        'customer-context'
    )
""")

hati.execute("""
    SELECT store_memory(
        'acme-corp renewed their enterprise contract in January 2026 for 3 years',
        'customer-context'
    )
""")

# Retrieve relevant context for a new interaction
context = hati.query("""
    SELECT content, created_at
    FROM _hatidata_memory.memories
    WHERE namespace = 'customer-context'
      AND semantic_match(embedding, 'acme-corp billing preferences', 0.65)
    ORDER BY semantic_rank(embedding, 'acme-corp billing preferences') DESC
    LIMIT 3
""")

print(f"Retrieved {len(context)} relevant memories:")
for c in context:
    print(f"  - {c['content']}")
Expected Output
Retrieved 2 relevant memories:
  - Customer acme-corp prefers quarterly billing and has a dedicated account manager
  - acme-corp renewed their enterprise contract in January 2026 for 3 years
3

Build the Full Agent

Wire memory retrieval into an OpenAI Agents SDK agent that automatically enriches prompts with past context.

Python
from openai_agents import Agent, Runner
from hatidata_agent import HatiDataAgent

hati = HatiDataAgent(host="localhost", port=5439, agent_id="support-agent")

def get_memory_context(query: str) -> str:
    memories = hati.query(f"""
        SELECT content FROM _hatidata_memory.memories
        WHERE semantic_match(embedding, '{query}', 0.65)
        ORDER BY semantic_rank(embedding, '{query}') DESC
        LIMIT 3
    """)
    return "\n".join(m["content"] for m in memories)

agent = Agent(
    name="Support Agent",
    instructions="You are a helpful support agent. Use provided context from memory.",
)

# Enrich the prompt with memory context
user_query = "What billing plan does acme-corp use?"
context = get_memory_context(user_query)

result = Runner.run_sync(
    agent,
    f"Memory context:\n{context}\n\nUser question: {user_query}"
)
print(result.final_output)
Expected Output
Based on our records, acme-corp is on quarterly billing with a dedicated account manager. They renewed their 3-year enterprise contract in January 2026.

Ready to build?

Install HatiData locally and start building with OpenAI Agents SDK in minutes.

Join Waitlist