Skip to main content
Give your OpenAI Agents SDK agents structured memory that persists across sessions.

Why Use This Integration

  • Persistent Memory: Knowledge survives agent restarts and session boundaries
  • Advanced Retrieval: Natural language queries backed by graph + vector search
  • Sessionized tools: Built in session data organization in memory
  • Native SDK Tools: Works directly with OpenAI’s function_tool pattern
  • Async-First: Built for high-performance applications with proper concurrency handling

Installation

pip install cognee-integration-openai-agents

Quick Start

Configure your environment variables:
export OPENAI_API_KEY="your-openai-api-key-here"  # for OpenAI Agents SDK
export LLM_API_KEY="your-openai-api-key-here"     # for cognee
Add memory tools to your agent:
import asyncio
from agents import Agent, Runner
from cognee_integration_openai_agents import add_tool, search_tool

async def main():
    # Create agent with memory
    agent = Agent(
        name="assistant",
        instructions="You are a helpful assistant with access to a knowledge base.",
        tools=[add_tool, search_tool],
    )

    # Store information
    await Runner.run(
        agent,
        "Remember: Acme Corp, healthcare, $1.2M contract"
    )

    # Retrieve information
    result = await Runner.run(
        agent,
        "What healthcare contracts do we have?"
    )
    print(result.final_output)

asyncio.run(main())

Cross-Session Persistence

Memory persists across different agent instances:
from agents import Agent, Runner
from cognee_integration_openai_agents import get_sessionized_cognee_tools

# Session 1: Store information
add_tool, search_tool = get_sessionized_cognee_tools("user-123")

agent_1 = Agent(
    name="assistant",
    instructions="You are a helpful assistant.",
    tools=[add_tool, search_tool],
)
await Runner.run(agent_1, "I'm working on authentication")

# Session 2: Different instance, same memory
add_tool, search_tool = get_sessionized_cognee_tools("user-123")

agent_2 = Agent(
    name="assistant",
    instructions="You are a helpful assistant.",
    tools=[add_tool, search_tool],
)
result = await Runner.run(agent_2, "What was I working on?")
# Returns: "authentication"

Custom Session Management

Control session isolation with custom session IDs:
# User-specific memory
add_tool, search_tool = get_sessionized_cognee_tools(session_id="user_123")

# Org-specific memory
add_tool, search_tool = get_sessionized_cognee_tools(session_id="org_acme")

# Generate unique session automatically
add_tool, search_tool = get_sessionized_cognee_tools()  # Uses UUID-based session ID
Sessionized tools scope data to their session ID. Use non-sessionized add_tool and search_tool to access data across all sessions.

How It Works

  1. Add Tool: Stores data in cognee’s knowledge graph with embeddings
  2. Search Tool: Retrieves relevant information via semantic search
  3. Auto-Processing: cognee extracts entities, relationships, and context automatically
  4. Session Scoping: Sessionized tools filter by session; non-sessionized tools access everything

Use Cases

Pre-load documents into cognee before creating agents:
import cognee
from agents import Agent, Runner
from cognee_integration_openai_agents import search_tool

# Pre-load documents
for doc_path in document_paths:
    with open(doc_path, 'r') as f:
        await cognee.add(f.read())
await cognee.cognify()

# Query across all documents
agent = Agent(
    name="analyst",
    instructions="You have access to our knowledge base.",
    tools=[search_tool]
)
result = await Runner.run(agent, "Find information about contract terms")
Maintain user context across work sessions:
# Monday
await Runner.run(agent, "Debugging payment flow")

# Wednesday
result = await Runner.run(agent, "What was I debugging?")
Isolate data per user or organization:
add_tool, search_tool = get_sessionized_cognee_tools(session_id=user_id)

agent = Agent(
    name="assistant",
    instructions="You are a helpful assistant.",
    tools=[add_tool, search_tool]
)
Share knowledge between specialized agents:
from cognee_integration_openai_agents import add_tool, search_tool

# Data collector agent
data_agent = Agent(
    name="data_collector",
    instructions="You collect and store important information.",
    tools=[add_tool]
)

# Research agent
research_agent = Agent(
    name="researcher",
    instructions="You search and analyze the knowledge base.",
    tools=[search_tool]
)
Route requests to specialized agents with shared memory:
storage_agent = Agent(
    name="storage_specialist",
    instructions="You specialize in storing information.",
    tools=[add_tool]
)

search_agent = Agent(
    name="search_specialist", 
    instructions="You specialize in finding information.",
    tools=[search_tool]
)

triage_agent = Agent(
    name="triage",
    instructions="Route to storage_specialist for saving, search_specialist for queries.",
    handoffs=[storage_agent, search_agent]
)

result = await Runner.run(triage_agent, "Find our healthcare contracts")