AgentCore Memory Session Manager
Section titled “AgentCore Memory Session Manager”{{ community_contribution_banner }}
The AgentCore Memory Session Manager leverages Amazon Bedrock AgentCore Memory to provide advanced memory capabilities with intelligent retrieval for Strands Agents. It supports both short-term memory (STM) for conversation persistence and long-term memory (LTM) with multiple strategies for learning user preferences, facts, and session summaries.
Installation
Section titled “Installation”pip install 'bedrock-agentcore[strands-agents]'Basic Setup (STM)
Section titled “Basic Setup (STM)”Short-term memory provides basic conversation persistence within a session. This is the simplest way to get started with AgentCore Memory.
Creating the Memory Resource
Section titled “Creating the Memory Resource”!!! note “One-time Setup” The memory resource creation shown below is typically done once, separately from your agent application. In production, you would create the memory resource through the AWS Console or a separate setup script, then use the memory ID in your agent application.
import osfrom bedrock_agentcore.memory import MemoryClient
# This is typically done once, separately from your agent applicationclient = MemoryClient(region_name="us-east-1")basic_memory = client.create_memory( name="BasicTestMemory", description="Basic memory for testing short-term functionality")
# Export the memory ID as an environment variable for reusememory_id = basic_memory.get('id')print(f"Created memory with ID: {memory_id}")os.environ['AGENTCORE_MEMORY_ID'] = memory_idUsing the Session Manager with Existing Memory
Section titled “Using the Session Manager with Existing Memory”import uuidimport boto3from datetime import datetimefrom strands import Agentfrom bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfigfrom bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager
MEM_ID = os.environ.get("AGENTCORE_MEMORY_ID", "your-existing-memory-id")ACTOR_ID = "test_actor_id_%s" % datetime.now().strftime("%Y%m%d%H%M%S")SESSION_ID = "test_session_id_%s" % datetime.now().strftime("%Y%m%d%H%M%S")
agentcore_memory_config = AgentCoreMemoryConfig( memory_id=MEM_ID, session_id=SESSION_ID, actor_id=ACTOR_ID)
# Create session managersession_manager = AgentCoreMemorySessionManager( agentcore_memory_config=agentcore_memory_config, region_name="us-east-1")
# Create agent with session manageragent = Agent( system_prompt="You are a helpful assistant. Use all you know about the user to provide helpful responses.", session_manager=session_manager,)
# Use the agent - conversations are automatically persistedagent("I like sushi with tuna")agent("What should I buy for lunch today?")Long-Term Memory (LTM)
Section titled “Long-Term Memory (LTM)”Long-term memory provides advanced capabilities with multiple strategies for learning and storing user preferences, facts, and session summaries across conversations.
Creating LTM Memory with Strategies
Section titled “Creating LTM Memory with Strategies”!!! note “One-time Setup” Similar to STM, the LTM memory resource creation is typically done once, separately from your agent application. In production, you would create the memory resource with strategies through the AWS Console or a separate setup script.
Bedrock AgentCore Memory supports three built-in memory strategies:
summaryMemoryStrategy: Summarizes conversation sessionsuserPreferenceMemoryStrategy: Learns and stores user preferencessemanticMemoryStrategy: Extracts and stores factual information
import osfrom bedrock_agentcore.memory import MemoryClient
# This is typically done once, separately from your agent applicationclient = MemoryClient(region_name="us-east-1")comprehensive_memory = client.create_memory_and_wait( name="ComprehensiveAgentMemory", description="Full-featured memory with all built-in strategies", strategies=[ { "summaryMemoryStrategy": { "name": "SessionSummarizer", "namespaces": ["/summaries/{actorId}/{sessionId}"] } }, { "userPreferenceMemoryStrategy": { "name": "PreferenceLearner", "namespaces": ["/preferences/{actorId}"] } }, { "semanticMemoryStrategy": { "name": "FactExtractor", "namespaces": ["/facts/{actorId}"] } } ])
# Export the LTM memory ID as an environment variable for reuseltm_memory_id = comprehensive_memory.get('id')print(f"Created LTM memory with ID: {ltm_memory_id}")os.environ['AGENTCORE_LTM_MEMORY_ID'] = ltm_memory_idConfiguring Retrieval
Section titled “Configuring Retrieval”You can configure how the agent retrieves information from different memory namespaces:
Single Namespace Retrieval
Section titled “Single Namespace Retrieval”from datetime import datetimefrom bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig, RetrievalConfigfrom bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManagerfrom strands import Agent
MEM_ID = os.environ.get("AGENTCORE_LTM_MEMORY_ID", "your-existing-ltm-memory-id")ACTOR_ID = "test_actor_id_%s" % datetime.now().strftime("%Y%m%d%H%M%S")SESSION_ID = "test_session_id_%s" % datetime.now().strftime("%Y%m%d%H%M%S")
config = AgentCoreMemoryConfig( memory_id=MEM_ID, session_id=SESSION_ID, actor_id=ACTOR_ID, retrieval_config={ "/preferences/{actorId}": RetrievalConfig( top_k=5, relevance_score=0.7 ) })
session_manager = AgentCoreMemorySessionManager(config, region_name='us-east-1')ltm_agent = Agent(session_manager=session_manager)Multiple Namespace Retrieval
Section titled “Multiple Namespace Retrieval”config = AgentCoreMemoryConfig( memory_id=MEM_ID, session_id=SESSION_ID, actor_id=ACTOR_ID, retrieval_config={ "/preferences/{actorId}": RetrievalConfig( top_k=5, relevance_score=0.7 ), "/facts/{actorId}": RetrievalConfig( top_k=10, relevance_score=0.3 ), "/summaries/{actorId}/{sessionId}": RetrievalConfig( top_k=5, relevance_score=0.5 ) })
session_manager = AgentCoreMemorySessionManager(config, region_name='us-east-1')agent_with_multiple_namespaces = Agent(session_manager=session_manager)Configuration Options
Section titled “Configuration Options”Memory Strategies
Section titled “Memory Strategies”AgentCore Memory supports three built-in strategies:
summaryMemoryStrategy: Automatically summarizes conversation sessions for efficient context retrievaluserPreferenceMemoryStrategy: Learns and stores user preferences across sessionssemanticMemoryStrategy: Extracts and stores factual information from conversations
AgentCoreMemoryConfig Parameters
Section titled “AgentCoreMemoryConfig Parameters”The AgentCoreMemoryConfig class accepts the following parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
memory_id | str | Yes | ID of the Bedrock AgentCore Memory resource |
session_id | str | Yes | Unique identifier for the conversation session |
actor_id | str | Yes | Unique identifier for the user/actor |
retrieval_config | Dict[str, RetrievalConfig] | No | Dictionary mapping namespaces to retrieval configurations |
RetrievalConfig Parameters
Section titled “RetrievalConfig Parameters”Configure retrieval behavior for each namespace:
| Parameter | Type | Default | Description |
|---|---|---|---|
top_k | int | 10 | Number of top-scoring records to return from semantic search (1-1000) |
relevance_score | float | 0.2 | Minimum relevance threshold for filtering results (0.0-1.0) |
strategy_id | Optional[str] | None | Optional parameter to filter memory strategies |
Namespace Patterns
Section titled “Namespace Patterns”Namespaces follow specific patterns with variable substitution:
/preferences/{actorId}: User-specific preferences across sessions/facts/{actorId}: User-specific facts across sessions/summaries/{actorId}/{sessionId}: Session-specific summaries
The {actorId} and {sessionId} placeholders are automatically replaced with the values from your configuration.
See the following docs for more on namespaces: Memory scoping with namespaces
Important Notes
Section titled “Important Notes”!!! note “Session Limitations” Currently, only one agent per session is supported when using AgentCoreMemorySessionManager. Creating multiple agents with the same session will show a warning.
Resources
Section titled “Resources”- GitHub: bedrock-agentcore-sdk-python
- Documentation: Strands Integration Examples
- Issues: Report bugs and feature requests in the bedrock-agentcore-sdk-python repository