Skip to main content
Add persistent, per-user memory to a Semantic Kernel application with a plugin. Both kernel functions (search_memory and store_memory) are auto-invokable, so the kernel can decide when to query or persist memories on its own. An agent thread and a filter report each turn and tool call to Synap, and send the model Synap’s compaction in place of the earlier turns.
Requires Python 3.11+ and Semantic Kernel 1.37 or later.

Overview

This guide shows how to add Synap to a Semantic Kernel application to build kernels that:
  • Recall user-specific facts, preferences, and past conversations
  • Persist new information surfaced during a conversation
  • Auto-invoke memory functions when the model decides they are relevant
  • Send the model Synap’s compacted history instead of the earlier turns

Setup

Install the package alongside Semantic Kernel:
Installing the package also installs maximem-synap (0.5.3 or later), maximem-synap-integrations-common (0.2.3 or later) and semantic-kernel (1.37 or later).
The pip package is maximem-synap-semantic-kernel, but the import drops the maximem- prefix and uses underscores: from synap_semantic_kernel 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 adds SynapPlugin to a kernel and invokes a prompt that references the plugin’s functions:
The scoping triple (user_id, optional customer_id) is bound when you construct the plugin. The kernel functions only ever see query, content, and other model-supplied parameters. This prevents prompts (or prompt injections) from spoofing scope. For automatic invocation (the kernel chooses when to call memory functions), enable auto function calling. See “Auto function calling” below.

Core concepts

SynapPlugin

SynapPlugin is a regular Semantic Kernel plugin class with @kernel_function annotated methods. Adding it to the kernel registers both functions under the plugin name you choose:
After registration, the kernel can invoke the two functions through prompt templating, manual invocation, or auto function calling.
The same function has two spellings. In prompt templates and manual invocation it is synap.search_memory (plugin, dot, function). In auto function calling, the name the model sees and must call is synap-search_memory (plugin, hyphen, function), so any instruction that tells the model which tool to use has to use the hyphenated form. The same applies to synap-store_memory.

search_memory

Function signature exposed to the kernel:
Runs an accurate-mode fetch scoped to the bound user and customer, and returns the formatted context as a string suitable for direct interpolation into prompt templates. When nothing matches it returns "No relevant memories found.". Search failures surface: an SDK error is logged and raised as SynapIntegrationError.

store_memory

Function signature exposed to the kernel:
Returns "Memory stored (ingestion_id: ...)" on success. Store failures surface explicitly: the function raises SynapIntegrationError so the kernel (and caller) know if persistence failed.

Complete example: chat function with auto-invoked memory

The following kernel auto-invokes synap-search_memory and synap-store_memory whenever the model decides they are relevant. The application just adds messages to a ChatHistory and the kernel handles the rest:
Three things to notice in this pattern:
  1. FunctionChoiceBehavior.Auto() lets the model drive memory. No prompt-template plumbing; the model calls the right function when it’s the right time.
  2. Scope is per-kernel. build_kernel(...) constructs a fresh kernel per user so concurrent requests cannot bleed scope.
  3. The system message is the policy. Tell the model when to search and when to store; the plugin just enforces it.

Advanced patterns

Multi-tenant scoping

SynapPlugin accepts the standard scoping triple: user_id (required), customer_id (B2B instances only), optional conversation_id. 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.
For multi-tenant services, build the plugin (and kernel) per request rather than sharing one across users.

Prompt-template invocation

Outside of auto function calling, you can interpolate plugin calls directly in prompt templates:
This pattern is useful when you want deterministic recall (the function always runs) rather than letting the model decide.

Report turns and use Synap’s compaction

The plugin gives the model tools. To report the conversation itself, and to send the model Synap’s compaction, use SynapAgentThread and attach_synap_filters with a ChatCompletionAgent:
conversation_id and user_id are required on the thread and on the filter. Use the same ids on both so the turns and the tool calls land in one conversation. Make conversation_id a UUID: Synap rejects any other value when it reads the compacted conversation.
  • Turns. The thread reports each user and assistant message. A message that carries a tool call is one lap of the function-calling loop, not the end of the turn, so it is not reported as a turn.
  • Tool calls and results. The filter reports the call before the tool runs and the result after, with the same call id, so anticipation sees what the agent did. A tool result is usually your own customer’s data and it leaves your process.
  • Compaction. The agent reads its history through the thread. Once Synap has compacted the conversation, the model is sent Synap’s compaction block (in <synap_conversation_history> tags) and the turns after it, in place of every turn before the current one. The current turn is untouched. Before the first compaction, or if Synap cannot answer, the model is sent Semantic Kernel’s own messages. Semantic Kernel’s own reducers stay in place as the fallback.
You do not call listen(). The first turn opens the real-time stream. If it cannot open, each turn is written over REST instead, never both; tool calls and reasoning exist only on the stream and are dropped in that case. Set SYNAP_AUTO_LISTEN=false to stay on REST, or open the stream yourself and the integration uses it as is. See gRPC vs REST. By default the thread’s history is a SynapChatHistoryReducer, which Synap’s compaction replaces turns in. If you pass your own chat_history to SynapAgentThread, it stays as it is: the model is still sent Synap’s compaction in place of the earlier turns, but your history is not rewritten. Outside an agent thread, call await history.reduce() on a SynapChatHistoryReducer before each model call to get the same replacement.

Add the compaction to a system message

synap_st_chat_message is the lighter option for a plain chat-completion loop. It returns an async factory that builds a system message from Synap’s short-term context and your own prompt:
The factory fetches Synap’s short-term context for the conversation, which includes the recent turns, wraps it in <synap_short_term_context> tags and places it above your system text. Pass the history you are about to send: when it already holds user or assistant turns, the factory returns only your system text, so the turns are not sent twice. That makes it the right fit for a loop that starts from an empty history, for example after a restart. conversation_id is required and must be a UUID.

Failure semantics

The plugin and the reporting pieces fail differently:
  • The plugin raises. search_memory and store_memory log the error and raise SynapIntegrationError, so a failed search is never mistaken for an empty memory and a failed store is never mistaken for a stored one.
  • Reporting never raises. SynapAgentThread updates the thread’s own history first and then reports, so a Synap problem cannot cost you a message. The filter and the reducer log and carry on.
  • No compaction, or an error, leaves the history alone. The model is sent the messages Semantic Kernel built.
  • synap_st_chat_message falls back by default. On an SDK error it returns your system text. Pass on_error="raise" to get SynapIntegrationError instead.

Going further


Next steps

Microsoft Agent Framework

Context and history providers for MAF agents.

OpenAI Agents

Function tools for the OpenAI Agents SDK.

Context Fetch

The retrieval API behind search_memory: modes, scopes, and response shapes.

Memory Scopes

How user_id and customer_id interact across reads and writes.