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
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. BecauseCodeAgent writes its actions as Python, search_memory / store_memory compose naturally into the code it generates.
Core concepts
create_synap_tools
Returnssearch_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:
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 readsagent.taskand reports it once per run, at the end of the first step rather than before it. Passreport_user_task=Falseand write the turn yourself withrecord_turnfromsynap_integrations_commonif you need it earlier. - Parallel tool calls share one result.
ToolCallingAgentmerges every tool’s output for a step into oneobservationsstring. 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=Falseto stop them from being reported.
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:
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
Advanced patterns
Synchronous framework, async SDK
Smolagents runs synchronously and its tools do not supportasync, 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_memorytool degrades: a Synap blip returns a not-found message. - The
store_memorytool raisesSynapIntegrationErroron failure (model-driven, user-initiated). - The recorder logs and never raises. It runs inside the agent loop, in a
finallywith 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_idandcustomer_idflow 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.