strands.multiagent.base
¶
Multi-Agent Base Class.
Provides minimal foundation for multi-agent patterns (Swarm, Graph).
AttributeValue = Union[str, bool, float, int, List[str], List[bool], List[float], List[int], Sequence[str], Sequence[bool], Sequence[int], Sequence[float]]
module-attribute
¶
MultiAgentInput = str | list[ContentBlock] | list[InterruptResponseContent]
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
AgentResult
dataclass
¶
Represents the last result of invoking an agent with a prompt.
Attributes:
| Name | Type | Description |
|---|---|---|
stop_reason |
StopReason
|
The reason why the agent's processing stopped. |
message |
Message
|
The last message generated by the agent. |
metrics |
EventLoopMetrics
|
Performance metrics collected during processing. |
state |
Any
|
Additional state information from the event loop. |
interrupts |
Sequence[Interrupt] | None
|
List of interrupts if raised by user. |
structured_output |
BaseModel | None
|
Parsed structured output when structured_output_model was specified. |
Source code in strands/agent/agent_result.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
__str__()
¶
Get the agent's last message as a string.
This method extracts and concatenates all text content from the final message, ignoring any non-text content like images or structured data. If there's no text content but structured output is present, it serializes the structured output instead.
Returns:
| Type | Description |
|---|---|
str
|
The agent's last message as a string. |
Source code in strands/agent/agent_result.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
from_dict(data)
classmethod
¶
Rehydrate an AgentResult from persisted JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary containing the serialized AgentResult data |
required |
Returns: AgentResult instance Raises: TypeError: If the data format is invalid@
Source code in strands/agent/agent_result.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
to_dict()
¶
Convert this AgentResult to JSON-serializable dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing serialized AgentResult data |
Source code in strands/agent/agent_result.py
78 79 80 81 82 83 84 85 86 87 88 | |
Interrupt
dataclass
¶
Represents an interrupt that can pause agent execution for human-in-the-loop workflows.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier. |
name |
str
|
User defined name. |
reason |
Any
|
User provided reason for raising the interrupt. |
response |
Any
|
Human response provided when resuming the agent after an interrupt. |
Source code in strands/interrupt.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
to_dict()
¶
Serialize to dict for session management.
Source code in strands/interrupt.py
27 28 29 | |
Metrics
¶
Bases: TypedDict
Performance metrics for model interactions.
Attributes:
| Name | Type | Description |
|---|---|---|
latencyMs |
int
|
Latency of the model request in milliseconds. |
timeToFirstByteMs |
int
|
Latency from sending model request to first content chunk (contentBlockDelta or contentBlockStart) from the model in milliseconds. |
Source code in strands/types/event_loop.py
26 27 28 29 30 31 32 33 34 35 36 | |
MultiAgentBase
¶
Bases: ABC
Base class for multi-agent helpers.
This class integrates with existing Strands Agent instances and provides multi-agent orchestration capabilities.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique MultiAgent id for session management,etc. |
Source code in strands/multiagent/base.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
__call__(task, invocation_state=None, **kwargs)
¶
Invoke synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
MultiAgentInput
|
The task to execute |
required |
invocation_state
|
dict[str, Any] | None
|
Additional state/context passed to underlying agents. Defaults to None to avoid mutable default argument issues. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to underlying agents. |
{}
|
Source code in strands/multiagent/base.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
deserialize_state(payload)
¶
Restore orchestrator state from a session dict.
Source code in strands/multiagent/base.py
252 253 254 | |
invoke_async(task, invocation_state=None, **kwargs)
abstractmethod
async
¶
Invoke asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
MultiAgentInput
|
The task to execute |
required |
invocation_state
|
dict[str, Any] | None
|
Additional state/context passed to underlying agents. Defaults to None to avoid mutable default argument issues. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to underlying agents. |
{}
|
Source code in strands/multiagent/base.py
189 190 191 192 193 194 195 196 197 198 199 200 201 | |
serialize_state()
¶
Return a JSON-serializable snapshot of the orchestrator state.
Source code in strands/multiagent/base.py
248 249 250 | |
stream_async(task, invocation_state=None, **kwargs)
async
¶
Stream events during multi-agent execution.
Default implementation executes invoke_async and yields the result as a single event. Subclasses can override this method to provide true streaming capabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
MultiAgentInput
|
The task to execute |
required |
invocation_state
|
dict[str, Any] | None
|
Additional state/context passed to underlying agents. Defaults to None to avoid mutable default argument issues. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to underlying agents. |
{}
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[dict[str, Any]]
|
Dictionary events containing multi-agent execution information including: |
AsyncIterator[dict[str, Any]]
|
|
AsyncIterator[dict[str, Any]]
|
|
AsyncIterator[dict[str, Any]]
|
|
Source code in strands/multiagent/base.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
MultiAgentResult
dataclass
¶
Result from multi-agent execution with accumulated metrics.
Source code in strands/multiagent/base.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
from_dict(data)
classmethod
¶
Rehydrate a MultiAgentResult from persisted JSON.
Source code in strands/multiagent/base.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
to_dict()
¶
Convert MultiAgentResult to JSON-serializable dict.
Source code in strands/multiagent/base.py
163 164 165 166 167 168 169 170 171 172 173 174 | |
NodeResult
dataclass
¶
Unified result from node execution - handles both Agent and nested MultiAgentBase results.
Source code in strands/multiagent/base.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
from_dict(data)
classmethod
¶
Rehydrate a NodeResult from persisted JSON.
Source code in strands/multiagent/base.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
get_agent_results()
¶
Get all AgentResult objects from this node, flattened if nested.
Source code in strands/multiagent/base.py
58 59 60 61 62 63 64 65 66 67 68 69 | |
to_dict()
¶
Convert NodeResult to JSON-serializable dict, ignoring state field.
Source code in strands/multiagent/base.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
Status
¶
Bases: Enum
Execution status for both graphs and nodes.
Attributes:
| Name | Type | Description |
|---|---|---|
PENDING |
Task has not started execution yet. |
|
EXECUTING |
Task is currently running. |
|
COMPLETED |
Task finished successfully. |
|
FAILED |
Task encountered an error and could not complete. |
|
INTERRUPTED |
Task was interrupted by user. |
Source code in strands/multiagent/base.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
Usage
¶
Bases: TypedDict
Token usage information for model interactions.
Attributes:
| Name | Type | Description |
|---|---|---|
inputTokens |
Required[int]
|
Number of tokens sent in the request to the model. |
outputTokens |
Required[int]
|
Number of tokens that the model generated for the request. |
totalTokens |
Required[int]
|
Total number of tokens (input + output). |
cacheReadInputTokens |
int
|
Number of tokens read from cache (optional). |
cacheWriteInputTokens |
int
|
Number of tokens written to cache (optional). |
Source code in strands/types/event_loop.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
_parse_metrics(metrics_data)
¶
Parse Metrics from dict data.
Source code in strands/multiagent/base.py
287 288 289 | |
_parse_usage(usage_data)
¶
Parse Usage from dict data.
Source code in strands/multiagent/base.py
272 273 274 275 276 277 278 279 280 281 282 283 284 | |
run_async(async_func)
¶
Run an async function in a separate thread to avoid event loop conflicts.
This utility handles the common pattern of running async code from sync contexts by using ThreadPoolExecutor to isolate the async execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
async_func
|
Callable[[], Awaitable[T]]
|
A callable that returns an awaitable |
required |
Returns:
| Type | Description |
|---|---|
T
|
The result of the async function |
Source code in strands/_async.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |