Skip to main content
Add persistent, per-user memory to a Google ADK agent. Synap exposes itself as two FunctionTool instances, and the agent decides when to recall or remember. A second factory reports each run to Synap and replaces ADK’s earlier turns with Synap’s compaction once a conversation has been compacted.
Requires Python 3.11+.

Overview

This guide shows how to add Synap to a Google ADK application to build agents that:
  • Recall user-specific facts, preferences, and past conversations
  • Persist new information surfaced during a conversation
  • Stay multi-user-safe by binding scope at tool construction
  • Send the model Synap’s compacted history instead of ADK’s earlier turns

Setup

Install the package alongside the Google ADK:
Installing the package also installs maximem-synap (0.5.3 or later), maximem-synap-integrations-common (0.2.3 or later) and google-adk (1.0 or later).
The pip package is maximem-synap-google-adk, but the import drops the maximem- prefix and uses underscores: from synap_google_adk 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 calls create_synap_tools and passes the result straight into an Agent. The factory returns a list, so it fits the tools= parameter without unpacking:
The factory binds user_id and customer_id into the closures it returns. The model only ever sees query and content, never the user identity. This prevents prompt-injection attempts from spoofing scope.

Core concepts

create_synap_tools

create_synap_tools returns a two-element list: [search_memory, store_memory]. Both are ADK FunctionTool instances and can be passed directly to Agent(tools=...):

search_memory

Tool signature exposed to the model:
Runs an accurate-mode fetch scoped to the bound user and customer, and returns the formatted context as one string. When nothing matches it returns "No relevant memories found.". Search failures surface: an SDK error is logged and raised as SynapIntegrationError, so a failed search is never mistaken for an empty memory.

store_memory

Tool signature exposed to the model:
Returns "Memory stored (ingestion_id: ...)" on success. Store failures surface explicitly: the tool raises SynapIntegrationError so the agent (and you) know if persistence failed.

Complete example: per-user memory agent

In a multi-user service, build a fresh tool set per request. Each agent run gets a scope-bound tool list, so two concurrent users cannot see each other’s memories:
Three things to notice in this pattern:
  1. Per-request agent construction is the safety pattern. Each request gets its own scope-bound tool list, so no shared state can leak between users.
  2. The instruction is the policy. Telling the model to always call search_memory first is what produces recall behavior; the tools are just plumbing.
  3. Failures are never silent. Both tools raise SynapIntegrationError when the SDK call fails, so a failed store cannot pass for a stored one and a failed search cannot pass for an empty memory.

Advanced patterns

Multi-tenant scoping

create_synap_tools 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.

Report the run and use Synap’s compaction

create_synap_callbacks returns the keyword arguments an agent takes for its callbacks. Splat the dict into the agent next to the tools:
The dict holds four callbacks: before_model_callback, after_model_callback, before_tool_callback and after_tool_callback.
  • Turns. The user’s message is written once per invocation, and the model’s final reply is written when it arrives. A reply that calls a tool, a partial streamed chunk and a reasoning part are not turns, so they are not written.
  • Tool calls and results. Each tool call and its result are reported to Synap, so anticipation sees what the agent did, not only what it said. Pass report_tool_results=False to keep results out; a tool result is usually your own customer’s data, and it leaves your process.
  • Compaction. Once Synap has compacted the conversation, every turn before the current one is replaced by Synap’s compaction block, placed in the system instruction, followed by the turns after the compaction. The current turn is untouched. Before the first compaction, or if Synap cannot answer, ADK’s own contents are sent as they are. ADK’s own compaction stays installed as the fallback.
Pass conversation_id, and make it a UUID: Synap rejects any other value when it reads the compacted conversation. The replacement only happens for a known conversation, and the REST fallback cannot write a turn without one. The first callback opens the real-time stream for you, so you do not call listen(). If the stream cannot open, each turn is written over REST instead, never both; tool calls and results 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. create_synap_callbacks takes the before_model_callback slot. If you have your own, pass a list after the splat:
If a run does not use the callbacks, report_user_turn(sdk, content, conversation_id=..., user_id=...) writes the user’s turn yourself. When you use both, the turn is written once.

Add the compaction to the instruction

synap_st_instruction is the lighter option when you only want the compaction in the prompt. It returns an async callable that ADK runs before each model call:
It prepends Synap’s compaction of the conversation, inside <synap_short_term_context> tags, to your instruction. Before the first compaction it adds nothing, because ADK already sends the recent turns. conversation_id is required and must be a UUID.

Multi-agent setups

Each Agent can carry its own tool list. Give a “memory-keeper” agent both tools and a “consumer” agent only search_memory to keep the write surface small:

Failure semantics

The two parts of the integration fail differently:
  • The tools raise. search_memory and store_memory log the error and raise SynapIntegrationError, with the original exception as __cause__.
  • The callbacks never raise. They run inside ADK’s own loop, so every failure is logged and swallowed, and each callback returns None so ADK never treats it as an override of the model or tool result.
  • synap_st_instruction falls back by default. On an SDK error it returns your static instruction unchanged. Pass on_error="raise" to get SynapIntegrationError instead.
Silent write failures would let the memory drift away from reality, so the tools make them visible. Telemetry must not end a user-facing run, so the callbacks stay quiet.

Going further


Next steps

Haystack

Pipeline components for Haystack.

Agno

InMemoryDb replacement for Agno agents.

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.