Skip to content

Commit 9cae964

Browse files
authored
docs: update agentcore memory docs (#307)
docs: update agentcore memory docs (#307)
1 parent 570add1 commit 9cae964

File tree

1 file changed

+136
-35
lines changed

1 file changed

+136
-35
lines changed

docs/community/session-managers/agentcore-memory.md

Lines changed: 136 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,53 @@ pip install 'bedrock-agentcore[strands-agents]'
1212

1313
## Usage
1414

15-
### Basic Setup (Short-Term Memory)
15+
### Basic Setup (STM)
16+
17+
18+
Short-term memory provides basic conversation persistence within a session. This is the simplest way to get started with AgentCore Memory.
19+
20+
#### Creating the Memory Resource
21+
22+
!!! note "One-time Setup"
23+
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.
1624

1725
```python
18-
from strands import Agent
26+
import os
1927
from bedrock_agentcore.memory import MemoryClient
20-
from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig
21-
from bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager
22-
from datetime import datetime
2328

24-
# Create a basic memory for short-term functionality
29+
# This is typically done once, separately from your agent application
2530
client = MemoryClient(region_name="us-east-1")
2631
basic_memory = client.create_memory(
2732
name="BasicTestMemory",
2833
description="Basic memory for testing short-term functionality"
2934
)
3035

31-
# Configure memory
36+
# Export the memory ID as an environment variable for reuse
37+
memory_id = basic_memory.get('id')
38+
print(f"Created memory with ID: {memory_id}")
39+
os.environ['AGENTCORE_MEMORY_ID'] = memory_id
40+
```
41+
42+
43+
### Using the Session Manager with Existing Memory
44+
45+
```python
46+
import uuid
47+
import boto3
48+
from datetime import datetime
49+
from strands import Agent
50+
from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig
51+
from bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager
52+
53+
54+
MEM_ID = os.environ.get("AGENTCORE_MEMORY_ID", "your-existing-memory-id")
55+
ACTOR_ID = "test_actor_id_%s" % datetime.now().strftime("%Y%m%d%H%M%S")
56+
SESSION_ID = "test_session_id_%s" % datetime.now().strftime("%Y%m%d%H%M%S")
57+
3258
agentcore_memory_config = AgentCoreMemoryConfig(
33-
memory_id=basic_memory.get('id'),
34-
session_id=f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}",
35-
actor_id=f"user_{datetime.now().strftime('%Y%m%d%H%M%S')}"
59+
memory_id=MEM_ID,
60+
session_id=SESSION_ID,
61+
actor_id=ACTOR_ID
3662
)
3763

3864
# Create session manager
@@ -41,25 +67,39 @@ session_manager = AgentCoreMemorySessionManager(
4167
region_name="us-east-1"
4268
)
4369

44-
# Create agent
70+
# Create agent with session manager
4571
agent = Agent(
4672
system_prompt="You are a helpful assistant. Use all you know about the user to provide helpful responses.",
4773
session_manager=session_manager,
4874
)
4975

50-
# Use the agent - conversations are persisted with intelligent retrieval
76+
# Use the agent - conversations are automatically persisted
5177
agent("I like sushi with tuna")
52-
agent("What should I buy for lunch today?") # Agent remembers preferences
78+
agent("What should I buy for lunch today?")
5379
```
5480

55-
### Advanced Setup (Long-Term Memory)
5681

57-
For more sophisticated memory capabilities, create a memory with multiple strategies:
82+
## Long-Term Memory (LTM)
83+
84+
Long-term memory provides advanced capabilities with multiple strategies for learning and storing user preferences, facts, and session summaries across conversations.
85+
86+
### Creating LTM Memory with Strategies
87+
88+
!!! note "One-time Setup"
89+
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.
90+
91+
Bedrock AgentCore Memory supports three built-in memory strategies:
92+
93+
1. **`summaryMemoryStrategy`**: Summarizes conversation sessions
94+
2. **`userPreferenceMemoryStrategy`**: Learns and stores user preferences
95+
3. **`semanticMemoryStrategy`**: Extracts and stores factual information
5896

5997
```python
60-
from bedrock_agentcore.memory.integrations.strands.config import RetrievalConfig
98+
import os
99+
from bedrock_agentcore.memory import MemoryClient
61100

62-
# Create comprehensive memory with all built-in strategies
101+
# This is typically done once, separately from your agent application
102+
client = MemoryClient(region_name="us-east-1")
63103
comprehensive_memory = client.create_memory_and_wait(
64104
name="ComprehensiveAgentMemory",
65105
description="Full-featured memory with all built-in strategies",
@@ -85,11 +125,52 @@ comprehensive_memory = client.create_memory_and_wait(
85125
]
86126
)
87127

88-
# Configure with multiple namespace retrieval
128+
# Export the LTM memory ID as an environment variable for reuse
129+
ltm_memory_id = comprehensive_memory.get('id')
130+
print(f"Created LTM memory with ID: {ltm_memory_id}")
131+
os.environ['AGENTCORE_LTM_MEMORY_ID'] = ltm_memory_id
132+
```
133+
134+
135+
### Configuring Retrieval
136+
137+
You can configure how the agent retrieves information from different memory namespaces:
138+
139+
#### Single Namespace Retrieval
140+
141+
```python
142+
from datetime import datetime
143+
from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig, RetrievalConfig
144+
from bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager
145+
from strands import Agent
146+
147+
MEM_ID = os.environ.get("AGENTCORE_LTM_MEMORY_ID", "your-existing-ltm-memory-id")
148+
ACTOR_ID = "test_actor_id_%s" % datetime.now().strftime("%Y%m%d%H%M%S")
149+
SESSION_ID = "test_session_id_%s" % datetime.now().strftime("%Y%m%d%H%M%S")
150+
151+
config = AgentCoreMemoryConfig(
152+
memory_id=MEM_ID,
153+
session_id=SESSION_ID,
154+
actor_id=ACTOR_ID,
155+
retrieval_config={
156+
"/preferences/{actorId}": RetrievalConfig(
157+
top_k=5,
158+
relevance_score=0.7
159+
)
160+
}
161+
)
162+
163+
session_manager = AgentCoreMemorySessionManager(config, region_name='us-east-1')
164+
ltm_agent = Agent(session_manager=session_manager)
165+
```
166+
167+
#### Multiple Namespace Retrieval
168+
169+
```python
89170
config = AgentCoreMemoryConfig(
90-
memory_id=comprehensive_memory.get('id'),
91-
session_id=f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}",
92-
actor_id=f"user_{datetime.now().strftime('%Y%m%d%H%M%S')}",
171+
memory_id=MEM_ID,
172+
session_id=SESSION_ID,
173+
actor_id=ACTOR_ID,
93174
retrieval_config={
94175
"/preferences/{actorId}": RetrievalConfig(
95176
top_k=5,
@@ -107,40 +188,60 @@ config = AgentCoreMemoryConfig(
107188
)
108189

109190
session_manager = AgentCoreMemorySessionManager(config, region_name='us-east-1')
110-
agent = Agent(session_manager=session_manager)
191+
agent_with_multiple_namespaces = Agent(session_manager=session_manager)
111192
```
112193

113-
## Configuration
194+
195+
## Configuration Options
196+
114197

115198
### Memory Strategies
116199

117200
AgentCore Memory supports three built-in strategies:
118201

119-
1. **summaryMemoryStrategy**: Automatically summarizes conversation sessions for efficient context retrieval
120-
2. **userPreferenceMemoryStrategy**: Learns and stores user preferences across sessions
121-
3. **semanticMemoryStrategy**: Extracts and stores factual information from conversations
202+
1. **`summaryMemoryStrategy`**: Automatically summarizes conversation sessions for efficient context retrieval
203+
2. **`userPreferenceMemoryStrategy`**: Learns and stores user preferences across sessions
204+
3. **`semanticMemoryStrategy`**: Extracts and stores factual information from conversations
122205

123206
### AgentCoreMemoryConfig Parameters
124207

125-
- `memory_id`: ID of the Bedrock AgentCore Memory resource
126-
- `session_id`: Unique identifier for the conversation session
127-
- `actor_id`: Unique identifier for the user/actor
128-
- `retrieval_config`: Dictionary mapping namespaces to RetrievalConfig objects
208+
The `AgentCoreMemoryConfig` class accepts the following parameters:
209+
210+
| Parameter | Type | Required | Description |
211+
|-----------|------|----------|-------------|
212+
| `memory_id` | `str` | Yes | ID of the Bedrock AgentCore Memory resource |
213+
| `session_id` | `str` | Yes | Unique identifier for the conversation session |
214+
| `actor_id` | `str` | Yes | Unique identifier for the user/actor |
215+
| `retrieval_config` | `Dict[str, RetrievalConfig]` | No | Dictionary mapping namespaces to retrieval configurations |
129216

130217
### RetrievalConfig Parameters
131218

132-
- `top_k`: Number of top results to retrieve (default: 5)
133-
- `relevance_score`: Minimum relevance threshold (0.0-1.0)
219+
Configure retrieval behavior for each namespace:
220+
221+
| Parameter | Type | Default | Description |
222+
|-----------|------|---------|-------------|
223+
| `top_k` | `int` | 10 | Number of top-scoring records to return from semantic search (1-1000) |
224+
| `relevance_score` | `float` | 0.2 | Minimum relevance threshold for filtering results (0.0-1.0) |
225+
| `strategy_id` | `Optional[str]` | None | Optional parameter to filter memory strategies |
134226

135227
### Namespace Patterns
136228

229+
Namespaces follow specific patterns with variable substitution:
230+
137231
- `/preferences/{actorId}`: User-specific preferences across sessions
138-
- `/facts/{actorId}`: User-specific factual information
139-
- `/summaries/{actorId}/{sessionId}`: Session-specific conversation summaries
232+
- `/facts/{actorId}`: User-specific facts across sessions
233+
- `/summaries/{actorId}/{sessionId}`: Session-specific summaries
234+
235+
The `{actorId}` and `{sessionId}` placeholders are automatically replaced with the values from your configuration.
236+
237+
See the following docs for more on namespaces: [Memory scoping with namespaces](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/session-actor-namespace.html)
238+
239+
140240

141241
## Important Notes
142242

143-
> **Session Limitations:** Currently, only **one** agent per session is supported when using AgentCoreMemorySessionManager. Creating multiple agents with the same session will show a warning.
243+
!!! note "Session Limitations"
244+
Currently, only **one** agent per session is supported when using AgentCoreMemorySessionManager. Creating multiple agents with the same session will show a warning.
144245

145246
## Resources
146247

0 commit comments

Comments
 (0)