strands.agent.agent_result
¶
Agent result handling for SDK.
This module defines the AgentResult class which encapsulates the complete response from an agent's processing cycle.
StopReason = Literal['content_filtered', 'end_turn', 'guardrail_intervened', 'interrupt', 'max_tokens', 'stop_sequence', 'tool_use']
module-attribute
¶
Reason for the model ending its response generation.
- "content_filtered": Content was filtered due to policy violation
- "end_turn": Normal completion of the response
- "guardrail_intervened": Guardrail system intervened
- "interrupt": Agent was interrupted for human input
- "max_tokens": Maximum token limit reached
- "stop_sequence": Stop sequence encountered
- "tool_use": Model requested to use a tool
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 | |
EventLoopMetrics
dataclass
¶
Aggregated metrics for an event loop's execution.
Attributes:
| Name | Type | Description |
|---|---|---|
cycle_count |
int
|
Number of event loop cycles executed. |
tool_metrics |
dict[str, ToolMetrics]
|
Metrics for each tool used, keyed by tool name. |
cycle_durations |
list[float]
|
List of durations for each cycle in seconds. |
agent_invocations |
list[AgentInvocation]
|
Agent invocation metrics containing cycles and usage data. |
traces |
list[Trace]
|
List of execution traces. |
accumulated_usage |
Usage
|
Accumulated token usage across all model invocations (across all requests). |
accumulated_metrics |
Metrics
|
Accumulated performance metrics across all model invocations. |
Source code in strands/telemetry/metrics.py
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
latest_agent_invocation
property
¶
Get the most recent agent invocation.
Returns:
| Type | Description |
|---|---|
Optional[AgentInvocation]
|
The most recent AgentInvocation, or None if no invocations exist. |
add_tool_usage(tool, duration, tool_trace, success, message)
¶
Record metrics for a tool invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
ToolUse
|
The tool that was used. |
required |
duration
|
float
|
How long the tool call took in seconds. |
required |
tool_trace
|
Trace
|
The trace object for this tool call. |
required |
success
|
bool
|
Whether the tool call was successful. |
required |
message
|
Message
|
The message associated with the tool call. |
required |
Source code in strands/telemetry/metrics.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
end_cycle(start_time, cycle_trace, attributes=None)
¶
End the current event loop cycle and record its duration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_time
|
float
|
The timestamp when the cycle started. |
required |
cycle_trace
|
Trace
|
The trace object for this cycle. |
required |
attributes
|
Optional[Dict[str, Any]]
|
attributes of the metrics. |
None
|
Source code in strands/telemetry/metrics.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
get_summary()
¶
Generate a comprehensive summary of all collected metrics.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing summarized metrics data. |
Dict[str, Any]
|
This includes cycle statistics, tool usage, traces, and accumulated usage information. |
Source code in strands/telemetry/metrics.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
reset_usage_metrics()
¶
Start a new agent invocation by creating a new AgentInvocation.
This should be called at the start of a new request to begin tracking a new agent invocation with fresh usage and cycle data.
Source code in strands/telemetry/metrics.py
342 343 344 345 346 347 348 | |
start_cycle(attributes)
¶
Start a new event loop cycle and create a trace for it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attributes
|
Dict[str, Any]
|
attributes of the metrics, including event_loop_cycle_id. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, Trace]
|
A tuple containing the start time and the cycle trace object. |
Source code in strands/telemetry/metrics.py
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 | |
update_metrics(metrics)
¶
Update the accumulated performance metrics with new metrics data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
Metrics
|
The metrics data to add to the accumulated totals. |
required |
Source code in strands/telemetry/metrics.py
350 351 352 353 354 355 356 357 358 359 | |
update_usage(usage)
¶
Update the accumulated token usage with new usage data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
usage
|
Usage
|
The usage data to add to the accumulated totals. |
required |
Source code in strands/telemetry/metrics.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
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 | |
Message
¶
Bases: TypedDict
A message in a conversation with the agent.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
List[ContentBlock]
|
The message content. |
role |
Role
|
The role of the message sender. |
Source code in strands/types/content.py
178 179 180 181 182 183 184 185 186 187 | |