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. Add run hooks and Synap also records each run and sends the model a compacted history in place of the turns it covers.
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
  • Send the model Synap’s compaction of a long conversation instead of every earlier turn
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). It also ships run hooks.

How the integration connects

The tools call Synap over the SDK when the model invokes them. The run hooks use the gRPC stream, and you do not open it yourself. The first report opens it, and from then on the user’s turn, tool calls, tool results, reasoning and the reply go over it. Opening gets up to 10 seconds; if it fails, the hooks try once more a few seconds later, then no more than once a minute, and write each turn over REST in the meantime, never both. The hooks never raise inside your run.
  • A stream your own code opened with listen() is used as is.
  • To keep the hooks on REST, set SYNAP_AUTO_LISTEN=false in the environment, or call set_auto_listen(sdk, False) from synap_integrations_common.
  • Tool calls, tool results and reasoning exist only on the stream. Without one they are not reported, and the turns still are.
  • The REST fallback needs a conversation_id, so pass one to SynapRunHooks.
  • Initialize the SDK first (below). Until it is initialized the hooks cannot open the stream.
See gRPC vs REST for what the stream carries.

Setup

Install the package alongside the OpenAI Agents SDK. It requires openai-agents 0.2.9 or later, the first release with both the on_llm_start hook and call_model_input_filter that the run hooks use, and it installs maximem-synap and maximem-synap-integrations-common as dependencies.
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:
The tool searches in accurate mode and returns Synap’s formatted context as one string, or "No relevant memories found." when there is none. The agent reads the text and can quote or reason over it directly. Search failures surface: the tool logs the error and raises SynapIntegrationError. Like any function tool failure, the Agents SDK hands the model a generic error message, and the model can carry on without recall.

Store tool

create_store_tool returns a companion callable that ingests a new memory.
Tool signature exposed to the model:
Returns "Memory stored (ingestion_id: ...)" on success. Synap extracts memories from the content asynchronously, so a memory stored now may take a moment to become searchable. Store failures surface explicitly: the tool raises SynapIntegrationError so the agent (and you) know if persistence failed.

Run hooks and compacted history

SynapRunHooks records what the agent does and, through the run config, sends the model Synap’s compaction of the conversation. Pass both to Runner.run with run_kwargs():
The hooks report five things: the user’s turn (read from the input of the run’s first model call, once per run), each tool call, each tool result, the model’s reasoning when the provider returns a summary, and the reply. Tool results are usually your own customer’s data. They are anticipation hints and never become long-term memories, but they do leave your process. Pass report_tool_results=False to keep them out. Compaction is not on by default. A RunHooks object cannot change what the model is sent. With hooks=hooks alone the run is recorded, but the model still receives the Agents SDK’s own history. run_kwargs() returns hooks= plus a run_config= whose call_model_input_filter does the replacing. Once Synap has compacted the conversation, that filter replaces every item before the current turn with Synap’s compaction and the turns after it. The current turn, with its tool calls and outputs, is left as it is. Until then, and whenever Synap cannot answer, the SDK’s items go through untouched. It works the same with a Session or with an input list you manage. If you already build a RunConfig, pass its arguments to run_kwargs(), for example hooks.run_kwargs(workflow_name="support"). A call_model_input_filter of your own runs after Synap’s. Compaction does not apply when the history lives on OpenAI’s side (previous_response_id or conversation_id on the run): there the input holds only the new items, and a filter cannot replace what is not in it. synap_st_instructions(sdk, conversation_id, system="...") is the lighter option. It returns an instructions callable that adds the compaction above your own instructions and does not replace any turns. Use it when you cannot change the run config. If you use both, Synap’s compaction is sent once. Build one SynapRunHooks per conversation. It pairs each tool call with its result by tool name, which assumes the calls to one tool run one after another.

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 tool calls only happen when the model elects to invoke the tools. The hooks record every run and send the model Synap’s compaction once there is one:
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. The hooks own the record. Every run is reported whether or not the model called a tool, and run_kwargs() is what puts Synap’s compaction in front of the model.

Advanced patterns

Multi-tenant scoping

Both factories and SynapRunHooks accept the same scoping pair: user_id (required), and customer_id on B2B instances only. customer_id is required on B2B Synap instances and not accepted on B2C (single-tenant) ones, where a call carrying it is rejected with HTTP 400. See Memory Scopes.

Per-request scoping

If your service handles many users in one process, build a fresh pair of tools and a fresh SynapRunHooks 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:
  • Tool failures surface explicitly: synap_search and synap_store log the error and raise SynapIntegrationError, which the Agents SDK turns into a generic error message for the model.
  • Hooks never raise: a failed report or a failed history read is logged, and the run carries on with the SDK’s own input.
This is by design: telemetry and history must not end a user-facing answer, while a tool the model chose to call should say when it failed.

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.