Skip to content

Commit a79b0e6

Browse files
feat: Include multi-agent session management to current (#306)
* feat: include multi-agent session management to current * fix: rename multi-agents to multi-agent systems * fix: add a warning to avoid mix usage of session manager in multiagent systems
1 parent b91de85 commit a79b0e6

File tree

1 file changed

+94
-17
lines changed

1 file changed

+94
-17
lines changed

docs/user-guide/concepts/agents/session-management.md

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@ Session management in Strands Agents provides a robust mechanism for persisting
44

55
## Overview
66

7-
A session represents all of the stateful information that is needed by an agent to function, including:
7+
A session represents all of stateful information that is needed by agents and multi-agent systems to function, including:
88

9+
**Single Agent Sessions**:
910
- Conversation history (messages)
1011
- Agent state (key-value storage)
1112
- Other stateful information (like [Conversation Manager](./state.md#conversation-manager))
1213

13-
Strands provides built-in session persistence capabilities that automatically capture and restore this information, allowing agents to seamlessly continue conversations where they left off.
14+
**Multi-Agent Sessions**:
15+
- Orchestrator state and configuration
16+
- Individual agent states and result within the orchestrator
17+
- Cross-agent shared state and context
18+
- Execution flow and node transition history
19+
20+
Strands provides built-in session persistence capabilities that automatically capture and restore this information, allowing agents and multi-agent systems to seamlessly continue conversations where they left off.
1421

1522
Beyond the built-in options, [third-party session managers](#third-party-session-managers) provide additional storage and memory capabilities.
1623

24+
!!! warning
25+
You cannot use a single agent with session manager in a multi-agent system. This will throw an exception. Each agent in a multi-agent system must be created without a session manager, and only the orchestrator should have the session manager. Additionally, multi-agent session managers only track the current state of the Graph/Swarm execution and do not persist individual agent conversation histories.
26+
1727
## Basic Usage
1828

29+
### Single Agent Sessions
30+
1931
Simply create an agent with a session manager and use it:
2032

2133
```python
@@ -34,6 +46,31 @@ agent("Hello!") # This conversation is persisted
3446

3547
The conversation, and associated state, is persisted to the underlying filesystem.
3648

49+
### Multi-Agent Sessions
50+
51+
Multi-agent systems(Graph/Swarm) can also use session management to persist their state:
52+
53+
```python
54+
from strands.multiagent import Graph
55+
from strands.session.file_session_manager import FileSessionManager
56+
57+
# Create agents
58+
agent1 = Agent(name="researcher")
59+
agent2 = Agent(name="writer")
60+
61+
# Create a session manager for the graph
62+
session_manager = FileSessionManager(session_id="multi-agent-session")
63+
64+
# Create graph with session management
65+
graph = Graph(
66+
agents={"researcher": agent1, "writer": agent2},
67+
session_manager=session_manager
68+
)
69+
70+
# Use the graph - all orchestrator state is persisted
71+
result = graph("Research and write about AI")
72+
```
73+
3774
## Built-in Session Managers
3875

3976
Strands offers two built-in session managers for persisting agent sessions:
@@ -43,7 +80,7 @@ Strands offers two built-in session managers for persisting agent sessions:
4380

4481
### FileSessionManager
4582

46-
The [`FileSessionManager`](../../../api-reference/session.md#strands.session.file_session_manager.FileSessionManager) provides a simple way to persist agent sessions to the local filesystem:
83+
The [`FileSessionManager`](../../../api-reference/session.md#strands.session.file_session_manager.FileSessionManager) provides a simple way to persist both single agent and multi-agent sessions to the local filesystem:
4784

4885
```python
4986
from strands import Agent
@@ -60,6 +97,17 @@ agent = Agent(session_manager=session_manager)
6097

6198
# Use the agent normally - state and messages will be persisted automatically
6299
agent("Hello, I'm a new user!")
100+
101+
# Multi-agent usage
102+
multi_session_manager = FileSessionManager(
103+
session_id="orchestrator-456",
104+
storage_dir="/path/to/sessions"
105+
)
106+
graph = Graph(
107+
agents={"agent1": agent1, "agent2": agent2},
108+
session_manager=multi_session_manager
109+
)
110+
63111
```
64112

65113
#### File Storage Structure
@@ -70,12 +118,15 @@ When using [`FileSessionManager`](../../../api-reference/session.md#strands.sess
70118
/<sessions_dir>/
71119
└── session_<session_id>/
72120
├── session.json # Session metadata
73-
└── agents/
74-
└── agent_<agent_id>/
75-
├── agent.json # Agent metadata and state
76-
└── messages/
77-
├── message_<message_id>.json
78-
└── message_<message_id>.json
121+
├── agents/ # Single agent storage
122+
│ └── agent_<agent_id>/
123+
│ ├── agent.json # Agent metadata and state
124+
│ └── messages/
125+
│ ├── message_<message_id>.json
126+
│ └── message_<message_id>.json
127+
└── multi_agents/ # Multi-agent storage
128+
└── multi_agent_<orchestrator_id>/
129+
└── multi_agent.json # Orchestrator state and configuration
79130
```
80131

81132
### S3SessionManager
@@ -104,6 +155,14 @@ agent = Agent(session_manager=session_manager)
104155

105156
# Use the agent normally - state and messages will be persisted to S3
106157
agent("Tell me about AWS S3")
158+
159+
# Use with multi-agent orchestrator
160+
swarm = Swarm(
161+
agents=[agent1, agent2, agent3],
162+
session_manager=session_manager
163+
)
164+
165+
result = swarm("Coordinate the task across agents")
107166
```
108167
#### S3 Storage Structure
109168

@@ -113,12 +172,15 @@ Just like in the [`FileSessionManager`](../../../api-reference/session.md#strand
113172
<s3_key_prefix>/
114173
└── session_<session_id>/
115174
├── session.json # Session metadata
116-
└── agents/
117-
└── agent_<agent_id>/
118-
├── agent.json # Agent metadata and state
119-
└── messages/
120-
├── message_<message_id>.json
121-
└── message_<message_id>.json
175+
├── agents/ # Single agent storage
176+
│ └── agent_<agent_id>/
177+
│ ├── agent.json # Agent metadata and state
178+
│ └── messages/
179+
│ ├── message_<message_id>.json
180+
│ └── message_<message_id>.json
181+
└── multi_agents/ # Multi-agent storage
182+
└── multi_agent_<orchestrator_id>/
183+
└── multi_agent.json # Orchestrator state and configuration
122184
```
123185

124186
#### Required S3 Permissions
@@ -160,13 +222,19 @@ The session management system in Strands Agents works through a combination of e
160222

161223
### 1. Session Persistence Triggers
162224

163-
Session persistence is automatically triggered by several key events in the agent lifecycle:
225+
Session persistence is automatically triggered by several key events in the agent and multi-agent lifecycle:
164226

227+
**Single Agent Events**
165228
- **Agent Initialization**: When an agent is created with a session manager, it automatically restores any existing state and messages from the session.
166229
- **Message Addition**: When a new message is added to the conversation, it's automatically persisted to the session.
167230
- **Agent Invocation**: After each agent invocation, the agent state is synchronized with the session to capture any updates.
168231
- **Message Redaction**: When sensitive information needs to be redacted, the session manager can replace the original message with a redacted version while maintaining conversation flow.
169232

233+
**Multi-Agent Events:**
234+
- **Multi-Agent Initialization**: When an orchestrator is created with a session manager, it automatically restores state from the session.
235+
- **Node Execution**: After each node invocation, synchronizes orchestrator state after node transitions
236+
- **Multi-Agent Invocation**: After multiagent finished, captures final orchestrator state after execution
237+
170238
!!! warning "After initializing the agent, direct modifications to `agent.messages` will not be persisted. Utilize the [Conversation Manager](./conversation-management.md) to help manage context of the agent in a way that can be persisted."
171239

172240

@@ -181,7 +249,7 @@ The [`Session`](../../../api-reference/types.md#strands.types.session.Session) m
181249
- **Purpose**: Provides a namespace for organizing multiple agents and their interactions
182250
- **Key Fields**:
183251
- `session_id`: Unique identifier for the session
184-
- `session_type`: Type of session (currently "AGENT")
252+
- `session_type`: Type of session (currently "AGENT" for both agent & multiagent in order to keep backward compatibility)
185253
- `created_at`: ISO format timestamp of when the session was created
186254
- `updated_at`: ISO format timestamp of when the session was last updated
187255

@@ -211,6 +279,14 @@ The [`SessionMessage`](../../../api-reference/types.md#strands.types.session.Ses
211279

212280
These data models work together to provide a complete representation of an agent's state and conversation history. The session management system handles serialization and deserialization of these models, including special handling for binary data using base64 encoding.
213281

282+
**Multi-Agent State**
283+
284+
Multi-agent systems serialize their state as JSON objects containing:
285+
286+
- **Orchestrator Configuration**: Settings, parameters, and execution preferences
287+
- **Node State**: Current execution state and node transition history
288+
- **Shared Context**: Cross-agent shared state and variables
289+
214290
## Third-Party Session Managers
215291

216292
The following third-party session managers extend Strands with additional storage and memory capabilities:
@@ -277,3 +353,4 @@ When implementing session persistence in your applications, consider these best
277353

278354
- **Understand Persistence Triggers**: Remember that changes to agent state or messages are only persisted during specific lifecycle events
279355

356+
- **Concurrent Access**: Session managers are not thread-safe; use appropriate locking for concurrent access

0 commit comments

Comments
 (0)