Skip to content
All posts

Introducing MongoDB memory for Strands Agents

Use MongoDB with Strands to resume agent sessions, retain useful facts across conversations, and retrieve long-term memories with semantic search.

Starting a new conversation with an agent shouldn’t mean having to explain your project all over again.

Decisions and lessons from previous tasks should carry forward, and work in progress should survive a restart. That’s why agents need memory, and there are several ways to provide it. Saving conversation history lets an agent pick up where it left off, while useful facts can carry across tasks through searchable long-term memory. The right mix of memory strategies and storage options depends on what your agent needs to remember and how it will use that information.

Strands provides pluggable interfaces for sessions and long-term memory, so you can use your preferred provider and configure memory to fit your agent’s use case.

If you already use MongoDB, you can now use it for memory in your Strands agents too. The MongoDB team built the strands-mongodb package, a Python integration that connects MongoDB to Strands for persistent sessions and searchable long-term memory. With this integration, MongoDB brings together session persistence for resuming conversations and complex tasks after a restart or on a different instance, memory storage for long-term facts and lessons that carry across conversations, and Vector Search for retrieving relevant past interactions by meaning to ground agent responses. Application data, session continuity, and memory retrieval can then share a single data platform rather than being split across separate systems.

Session memory and long-term memory

Session memory holds the conversation and agent state for the task in progress, so saving it lets the agent resume that conversation after a restart. Long-term memory keeps useful facts and lessons across conversations. When you start a new task, the agent can look up what it needs without loading every past conversation.

Suppose you tell an agent that your project’s integration tests use a separate database. Session memory preserves that exchange, and long-term memory can retain the database requirement for the next time you’re writing tests. Semantic search lets the agent find that fact even if you ask about “test setup” rather than repeating the original wording.

Strands handles those jobs through separate interfaces:

JobMongoDB backendHow the agent uses it
Resume a conversationMongoDBStorage, used by SnapshotSessionManagerLoads saved session state using a session ID.
Recall knowledge across conversationsMongoDBMemoryStore, used by MemoryManagerSearches stored facts within a scope, such as a project or user.

You can use either backend on its own, or point both to the same MongoDB deployment to share sessions and long-term storage.

Getting started

This example uses MongoDB Atlas, MongoDB’s managed cloud database service, to store sessions and search long-term memories.

Follow MongoDB’s get-started guide to install the packages and configure Atlas and your model credentials. Once that’s ready, save this example as mongodb_memory.py:

"""Persist Strands sessions and long-term memory in MongoDB."""
import asyncio
import os
from strands import Agent
from strands.memory import MemoryManager
from strands.models import BedrockModel
from strands.session import SnapshotSessionManager
from strands_mongodb import MongoDBMemoryStore, MongoDBStorage
MEMORY_SCOPE = "project-123"
DEFAULT_PROMPT = (
"Remember: this project uses pytest, and integration tests "
"use a separate database."
)
async def main():
session_id = os.getenv("SESSION_ID", "conversation-001")
prompt = os.getenv("PROMPT", DEFAULT_PROMPT)
# Session state and long-term memory share this deployment.
config = {
"connection_string": os.environ["MONGODB_URI"],
"database": "agents",
"search_type": "vector",
}
storage = MongoDBStorage(config, collection="sessions")
memories = MongoDBMemoryStore(
name="project_notes",
config=config,
scope=MEMORY_SCOPE, # Keep stable to share facts across conversations.
writable=True,
extraction=True, # Extract facts into long-term memory.
)
try:
async with Agent(
model=BedrockModel(
model_id="us.anthropic.claude-haiku-4-5-20251001-v1:0",
region_name="us-east-1",
),
storage=storage,
session_manager=SnapshotSessionManager(
session_id=session_id, # Reuse to resume; change to start fresh.
save_latest_on="message", # Save after every message.
),
# Adds memory search and injects relevant memories into context.
memory_manager=MemoryManager(stores=[memories]),
) as agent:
response = await agent.invoke_async(prompt)
print(response)
# Leaving the context flushes pending memory extraction.
finally:
await memories.close()
await storage.close()
if __name__ == "__main__":
asyncio.run(main())

When you run the example, conversation-001 identifies the saved conversation and project-123 groups the project’s long-term memories. With extraction=True, Strands uses a model call to extract useful facts from the conversation and save them as memories. Leaving the async with block waits for that extraction work to finish before the connections close.

To try recalling those facts, start a new session with the same memory scope and ask, “What test setup does this project require?” The agent should mention the separate database even though you haven’t repeated that requirement in the new conversation. MongoDB’s get-started guide includes the command to run this check.

Choosing how your agent searches memory

You configure how the agent searches memory when you set up the store. The agent decides what to search for, while the store uses the search mode you’ve configured. This example uses Vector Search, which finds memories by meaning: a search for “test setup” retrieves the fact that integration tests use a separate database, even though they share no words. Full-text search matches terms in stored text, such as “pytest.” Hybrid search combines both, so a search for “pytest setup for integration tests” matches the exact term “pytest” and also surfaces the note about the test database.

To make that semantic search work, Atlas automated embedding turns stored memories and queries into numerical representations that can be compared by meaning. You don’t need to write separate embedding code in your agent.

Keeping memory separate for users and projects

Now suppose you use the same agent for another project with a different test setup. Both projects have relevant memories about testing, so how does the agent know which project’s knowledge to search?

That’s what the project-123 memory scope in the example is for. A scope is the identifier that groups memories, and the integration filters searches to that group. Give the second project its own scope, and its memories can live alongside the first project’s without appearing in the same search.

The same approach works for an app serving multiple users. You can give each person a scope for their own information, or share a project scope when a team should draw on the same knowledge. Your app chooses the scope based on who’s signed in and which project they have access to.

As those conversations continue, you also need to manage what accumulates in each scope. The integration deduplicates identical memory entries within a store and scope, so repeated writes don’t fill it with copies. Some memories may only be useful for a while, such as notes for a temporary test environment. For a store that holds those memories, set ttl_seconds to expire them.

Thanks to the MongoDB team for building the integration.

Try the example with MongoDB’s get-started guide, and join us in the Strands Discord if you have questions or want to share what you’re building.