Skip to main content
Give a Smolagents CodeAgent or ToolCallingAgent persistent memory through Synap. Smolagents keeps its own fixed step log with no pluggable memory backend, so Synap plugs into the extension points it does expose: @tool functions, step_callbacks, the method that builds the model’s messages, and static instructions.
Requires Python 3.11+ and smolagents>=1.26.

Overview

Surfaces mapped onto Smolagents’ own extension points. Adopt only the ones you need. All of them take an already-constructed MaximemSynapSDK. Your app owns the SDK and its credentials.

Setup

Installing the package also installs maximem-synap (0.5.3 or later) and maximem-synap-integrations-common (0.2.3 or later). Set SYNAP_API_KEY in your environment; MaximemSynapSDK() reads it.

Basic integration

Give the agent memory tools it can call. Because CodeAgent writes its actions as Python, search_memory / store_memory compose naturally into the code it generates.

Core concepts

create_synap_tools

Returns search_memory and store_memory as Smolagents @tool functions. search_memory reads Synap’s long-term layer and returns formatted context; store_memory ingests a fact for future recall. Reads degrade to a not-found message; the write raises SynapIntegrationError so a failed store is observable.

synap_step_callbacks

synap_step_callbacks returns the step_callbacks mapping that reports each run to Synap. Pass it straight to the agent:
The callback is registered for MemoryStep, the base class of every step, so it receives planning steps, action steps and the final answer. Smolagents calls a callback for the step’s class and its base classes, so registering for ActionStep alone would skip planning steps and the FinalAnswerStep. Use synap_step_callbacks and the registration is right by construction. You do not call listen() yourself. The first report opens the gRPC stream. The stream gets up to 10 seconds to open. If it cannot open, the user and assistant turns are written over REST instead, never both, and the stream is tried once more a few seconds later, then no more than once a minute. Reasoning, tool calls and tool results exist only on the stream, so they are dropped while it is down. The REST write and the compaction lookup both need conversation_id to be a UUID. A stream you opened yourself is used as is. To keep the recorder on REST, set SYNAP_AUTO_LISTEN=false. See gRPC vs REST. Three details to know:
  • The user turn comes from agent.task. Smolagents never passes the task to a step callback, so the recorder reads agent.task and reports it once per run, at the end of the first step rather than before it. Pass report_user_task=False and write the turn yourself with record_turn from synap_integrations_common if you need it earlier.
  • Parallel tool calls share one result. ToolCallingAgent merges every tool’s output for a step into one observations string. A step with a single call reports its result under that call’s id. A step with several reports the merged text with no id, because attributing it to one call would be a guess.
  • Tool results leave your process. A tool result is usually your own customer’s data. Synap uses it only as an anticipation hint and never stores it as a long-term memory. Pass report_tool_results=False to stop them from being reported.
The turns reach long-term memory through the conversation: Synap extracts from the turns it holds. The recorder does not ingest each step as a separate document unless you pass ingest_steps=True, which also extracts the agent’s intermediate thoughts and, a second time, its final answer. Leave it off unless you want the step-by-step reasoning remembered. create_synap_recorder(sdk, user_id, conversation_id, ...) returns the bare callback with the same options, for when you build the step_callbacks mapping yourself. Register it for MemoryStep:

Synap’s compaction replaces the agent’s earlier steps

write_memory_to_messages is where Smolagents turns its system prompt and every memory step into the model’s messages, and every model call goes through it. attach_synap_history wraps it on one agent. Once Synap has compacted the conversation, the model sees the compaction block (in <synap_conversation_history> tags), the turns recorded after it, and the current task with its steps, in place of the earlier steps in agent.memory. Until then, and whenever Synap cannot answer, the messages pass through untouched. agent.memory itself is never edited, and Synap is read once per run, not once per step. The recorder attaches it for you on the first step it sees (compact_history=True is the default). That is after the first step of the first run, because Smolagents has no hook before a model call. To cover the first step of a resumed conversation too, attach it yourself right after you build the agent:
It is safe to call twice, and returns whether the agent is now wrapped.

synap_st_instructions

Smolagents’ instructions is a static string, so Synap’s compaction is folded into it once at construction, inside <synap_short_term_context> tags. Nothing is added until Synap has compacted the conversation. It is a snapshot, so use it only for an agent without the recorder: with the recorder, attach_synap_history sends the live compaction and the turns after it, and instructions is not needed.
conversation_id is required. Empty context is a no-op; SDK failures are swallowed by default (on_error="fallback"), or set on_error="raise" for strict environments.

Complete example: memory-augmented CodeAgent

The tools let the model search and store on demand. The callbacks report the run and, once Synap has compacted the conversation, send the model Synap’s compaction instead of the earlier steps.

Advanced patterns

Synchronous framework, async SDK

Smolagents runs synchronously and its tools do not support async, so every surface bridges to the async Synap SDK internally (via the shared run_async helper), on one background loop that stays open for the life of the process. Call agent.run as usual; initialize the SDK once with asyncio.run(sdk.initialize()) (or await sdk.initialize() in async code) before the first run.

Error policy

  • The search_memory tool degrades: a Synap blip returns a not-found message.
  • The store_memory tool raises SynapIntegrationError on failure (model-driven, user-initiated).
  • The recorder logs and never raises. It runs inside the agent loop, in a finally with no surrounding try/except, so a raising callback would abort the whole run; a failed report, history lookup or ingest is logged and swallowed, and Smolagents carries on with its own messages.

Going further

  • Scoping. user_id and customer_id flow straight to Synap; per-user isolation and B2C/B2B scope are derived server-side from the ids you pass.

Next steps

SDK: Ingestion

How memories.create extraction differs from conversation recording.

Integrations overview

Every framework Synap plugs into.