ChatContext before the first turn, reports every committed turn and tool call during the session, sends the model Synap’s compaction instead of a long call’s earlier turns, and exposes search/store function tools the LLM can call mid-conversation.
Requires Python 3.11+.
Overview
This guide shows how to add Synap to a LiveKit Agents application to build voice agents that:- Start every call already aware of the user’s history, no warm-up turn needed
- Report every committed turn and tool call back to Synap so memory grows with each call
- Send the model Synap’s compacted history instead of every earlier turn of a long call
- Search and store memories mid-conversation when the model decides it’s relevant
Setup
Install the package alongside LiveKit Agents. It also installsmaximem-synap (0.5.3 or later) and maximem-synap-integrations-common (0.2.3 or later), and needs livekit-agents 1.0 or later:
The pip package is
maximem-synap-livekit-agents, but the import drops the maximem- prefix and uses underscores: from synap_livekit_agents import .....env
Basic integration
The smallest useful integration preloads memory before the session starts and attaches recording during it. The agent now opens with knowledge of the user’s history and grows its memory with every committed turn:Core concepts
preload_synap_context
Loads the user’s long-term memories as system messages in theChatContext before the session starts. This gives the LLM awareness of the user’s history from the very first turn, with no tool call needed.
mode="fast" when the opening turn cannot wait; the default is "accurate" and max_results defaults to 20. Failures degrade gracefully. The session starts with empty context rather than raising.
attach_synap_recording
Subscribes to theAgentSession’s events and reports the whole call to Synap. Returns the conversation_id for the session:
- Turns. Each committed user and assistant message. An assistant item that only carries a tool call is not a turn.
- Tool calls and results. The call is reported the moment the tool is dispatched, which is the caller’s dead air, and the result when the tool finishes.
- The end of the call. When the session closes, the conversation’s Synap session is closed too, so it is not left to expire.
listen(). The first report opens the real-time stream, and the turns go out on it. 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.
Reporting happens out of band: it never blocks the audio path, and a failed report is logged and swallowed. Call attach_synap_recording before session.start(...).
Left out, conversation_id is a new random UUID for each session (0.2.4 or later; earlier versions generated an id that was not a UUID, which Synap rejected for reads and REST writes). Pass your own UUID to continue one conversation across sessions.
Compacted history
A LiveKitChatContext only grows, so a long call re-sends every turn on every LLM request. attach_synap_recording installs install_synap_history on the session’s agent, which wraps the agent’s llm_node. Once Synap has compacted the conversation, each LLM request 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, including tool calls and their outputs, is untouched. The replacement changes what the model reads for that request and not the history the agent keeps.
Before the first compaction, or when Synap cannot answer, the history is sent as LiveKit built it. A voice turn cannot wait on a slow read: if Synap takes longer than 1.5 seconds, that request goes through unchanged and the read is not tried again for 30 seconds. The wrapper follows the agent through a handoff, because the session’s current agent is wrapped on each event.
This does not apply to a session that uses a realtime model. There the provider holds the history, and the agent’s
llm_node is not called.install_synap_history(agent, sdk, conversation_id=...). To replace the turns in a ChatContext directly, await splice_synap_history(chat_ctx, sdk, conversation_id=...) does it in place and returns whether it changed anything.
Short-term context
preload_synap_st and refresh_synap_st add Synap’s short-term context of a conversation to the ChatContext: preload_synap_st once before session.start(...), and refresh_synap_st from a per-turn hook such as Agent.on_user_turn_completed, where it replaces the message it added last time.
conversation_id is required and must be a UUID. On an SDK error they leave the context untouched; pass on_error="raise" to get SynapIntegrationError.
Function tools
For mid-conversation lookups or saves, expose the search and store tools to the LLM. Thesynap_search_tool / synap_store_tool factories return LiveKit @function_tools, so the LiveKit LLM bridge picks them up as function calls automatically:
query parameter of the search tool and the content and category parameters of the store tool, never user_id. The search tool defaults to mode="accurate" and max_results=10.
Complete example: voice agent with full memory loop
The pattern below assembles the main exports into a single entrypoint. The agent preloads context, reports turns and tool calls, and can search or store memories mid-call:- Preload + record + tools form the loop. Preload reads memory in, record writes turns and tool calls out, and tools let the model do explicit lookups in between.
- Scope is per-call.
user_idandcustomer_idare pulled from room metadata, so each call has its own memory scope without any global state. - Failure modes are voice-friendly. Memory operations never block the audio path; they degrade silently so the caller hears no glitch.
Advanced patterns
Multi-tenant scoping
The helpers and tools takeuser_id (required) and customer_id (B2B instances only); attach_synap_recording also takes an optional conversation_id, and the short-term helpers require one. 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.
user_id / customer_id from room metadata or JWT claims at entrypoint time, never hardcode them.
Choosing preload vs. tools
- Preload only: the LLM sees the most relevant memories from turn one. Best when memory is small and you want zero per-turn latency overhead.
- Tools only: the LLM searches on demand. Best when memory is large and only a few queries need recall.
- Both: production setups where the opening greeting can reference long-term context AND the model can dig deeper mid-call.
Failure semantics
The integration follows the Synap-wide contract, adapted for voice latency:preload_synap_contextdegrades gracefully: empty context on failure, error logged.attach_synap_recordingnever raises into the call: a turn goes on the stream, or over REST when the stream cannot carry it; a failed report is logged and swallowed.- The compacted-history replacement leaves the history alone whenever Synap has no compaction, is slow or errors.
synap_search_tooldegrades gracefully: on failure it logs and returns the text"Synap memory is temporarily unavailable.", and when nothing matches it returns"No relevant long-term memory found for this query.".synap_store_toolsurfaces failures: raisesSynapIntegrationErrorso the model (and you) know if persistence failed.
Going further
- Voice agent pattern: the reference architecture for memory-backed voice agents.
- Voice concierge cookbook: an end-to-end worked voice example.
Next steps
Pipecat
Frame processors for Pipecat voice pipelines.
Claude Agent SDK
Hooks and MCP server for the Claude Agent SDK.
Context Fetch
The retrieval API behind
preload_synap_context and synap_search_tool.Memory Scopes
How
user_id, customer_id, and conversation_id interact across reads.