AgentCore Memory Session Manager¶
Community Contribution
This is a community-maintained package that is not owned or supported by the Strands team. Validate and review the package before using it in your project.
Have your own integration? We'd love to add it here too!
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¶
Usage¶
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¶
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 os
from bedrock_agentcore.memory import MemoryClient
# This is typically done once, separately from your agent application
client = 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 reuse
memory_id = basic_memory.get('id')
print(f"Created memory with ID: {memory_id}")
os.environ['AGENTCORE_MEMORY_ID'] = memory_id
Using the Session Manager with Existing Memory¶
import uuid
import boto3
from datetime import datetime
from strands import Agent
from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig
from 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 manager
session_manager = AgentCoreMemorySessionManager(
agentcore_memory_config=agentcore_memory_config,
region_name="us-east-1"
)
# Create agent with session manager
agent = 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 persisted
agent("I like sushi with tuna")
agent("What should I buy for lunch today?")
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¶
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 os
from bedrock_agentcore.memory import MemoryClient
# This is typically done once, separately from your agent application
client = 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 reuse
ltm_memory_id = comprehensive_memory.get('id')
print(f"Created LTM memory with ID: {ltm_memory_id}")
os.environ['AGENTCORE_LTM_MEMORY_ID'] = ltm_memory_id
Configuring Retrieval¶
You can configure how the agent retrieves information from different memory namespaces:
Single Namespace Retrieval¶
from datetime import datetime
from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig, RetrievalConfig
from bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager
from 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¶
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¶
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¶
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¶
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¶
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¶
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¶
- GitHub: bedrock-agentcore-sdk-python
- Documentation: Strands Integration Examples
- Issues: Report bugs and feature requests in the bedrock-agentcore-sdk-python repository