Skip to main content
Give an OpenAI Agent persistent, per-user memory by exposing two function tools: one that searches Synap and one that stores new memories. The agent decides when to call each, with no hidden injection and no chain modifications.
Requires Python 3.11+.

Overview

This guide shows how to add Synap to an OpenAI Agents SDK application to build agents that:
  • Recall user-specific facts, preferences, and past conversations
  • Persist new information surfaced during a conversation for future runs
  • Stay fully in control of when memory is queried or written
The Synap OpenAI Agents integration ships two factory functions, each of which returns an async callable you wrap with the function_tool(...) helper from agents (the name_override argument lives on function_tool, not on the FunctionTool class).

Setup

Install the package alongside the OpenAI Agents SDK:
The pip package is maximem-synap-openai-agents, but the import drops the maximem- prefix and uses underscores: from synap_openai_agents import ....
Configure your API key. Generate one from the Synap Dashboard.
.env
Initialize the SDK once at application startup:
See SDK Initialization for the full lifecycle and configuration options.

Basic integration

The smallest useful integration registers both tools on an agent and runs a query. The agent reads its tool descriptions, decides whether to search or store, and calls the matching function:
The scoping triple (user_id, optional customer_id) is bound when you construct the tool: the agent only ever sees the query and content parameters, never the user identity. This keeps the model from leaking or spoofing user IDs.

Core concepts

Search tool

create_search_tool returns an async callable that takes a natural-language query and returns a list of memory objects.
Tool signature exposed to the model:
Each result has the shape {"content": "...", "type": "fact", "confidence": 0.91}. The agent sees the JSON list and can quote or reason over the entries directly. Search failures degrade gracefully: the tool returns an empty list and logs an error, so the agent continues without recall rather than aborting.

Store tool

create_store_tool returns a companion callable that ingests a new memory.
Tool signature exposed to the model:
Returns {"status": "stored", "id": "..."} on success. Store failures surface explicitly: the tool raises SynapIntegrationError so the agent (and you) know if persistence failed.

Complete example: assistant with explicit memory control

The following agent is told to consult its memory before answering and store anything the user shares. The Synap calls only happen when the model elects to invoke the tools:
Three things to notice in this pattern:
  1. The agent owns the memory loop. Search and store calls are model-driven, not framework-driven; the prompt steers the behavior.
  2. Scope is bound at construction. The model never sees user_id or customer_id, so even a prompt-injection attempt cannot make it query memories for someone else.
  3. Failure modes split. Search is best-effort (empty list on failure); store is strict (raises on failure) so silent data loss is impossible.

Advanced patterns

Multi-tenant scoping

Both factories accept the same scoping triple: user_id (required), optional customer_id. customer_id is required on B2B Synap instances and ignored on single-tenant ones. See Memory Scopes.

Per-request scoping

If your service handles many users in one process, build a fresh pair of tools per request rather than caching them; each agent run should have its scope baked in to prevent cross-user leakage:

Failure semantics

The integration follows the Synap-wide contract:
  • Search failures degrade gracefully: synap_search returns [] and logs an error so the agent can continue.
  • Store failures surface explicitly: synap_store raises SynapIntegrationError so the agent (and caller) know persistence failed.
This is by design: a transient outage shouldn’t break a user-facing answer, but a silent write failure would let memory drift away from reality.

Going further


Next steps

Pydantic AI

Type-safe deps and tools for Pydantic AI agents.

AutoGen

BaseTool implementations for AutoGen agents.

Context Fetch

The retrieval API that powers synap_search: modes, scopes, and response shapes.

Memory Scopes

How user_id and customer_id interact across reads and writes.