strands.telemetry.tracer
¶
OpenTelemetry integration.
This module provides tracing capabilities using OpenTelemetry, enabling trace data to be sent to OTLP endpoints.
AttributeValue = Union[str, bool, float, int, List[str], List[bool], List[float], List[int], Sequence[str], Sequence[bool], Sequence[int], Sequence[float]]
module-attribute
¶
Attributes = Optional[Mapping[str, AttributeValue]]
module-attribute
¶
Messages = List[Message]
module-attribute
¶
A list of messages representing a conversation.
MultiAgentInput = str | list[ContentBlock] | list[InterruptResponseContent]
module-attribute
¶
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
_tracer_instance = None
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 | |
ContentBlock
¶
Bases: TypedDict
A block of content for a message that you pass to, or receive from, a model.
Attributes:
| Name | Type | Description |
|---|---|---|
cachePoint |
CachePoint
|
A cache point configuration to optimize conversation history. |
document |
DocumentContent
|
A document to include in the message. |
guardContent |
GuardContent
|
Contains the content to assess with the guardrail. |
image |
ImageContent
|
Image to include in the message. |
reasoningContent |
ReasoningContentBlock
|
Contains content regarding the reasoning that is carried out by the model. |
text |
str
|
Text to include in the message. |
toolResult |
ToolResult
|
The result for a tool request that a model makes. |
toolUse |
ToolUse
|
Information about a tool use request from a model. |
video |
VideoContent
|
Video to include in the message. |
citationsContent |
CitationsContentBlock
|
Contains the citations for a document. |
Source code in strands/types/content.py
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 | |
InterruptResponseContent
¶
Bases: TypedDict
Content block containing a user response to an interrupt.
Attributes:
| Name | Type | Description |
|---|---|---|
interruptResponse |
InterruptResponse
|
User response to an interrupt event. |
Source code in strands/types/interrupt.py
138 139 140 141 142 143 144 145 | |
JSONEncoder
¶
Bases: JSONEncoder
Custom JSON encoder that handles non-serializable types.
Source code in strands/telemetry/tracer.py
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 | |
encode(obj)
¶
Recursively encode objects, preserving structure and only replacing unserializable values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to encode |
required |
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation of the object |
Source code in strands/telemetry/tracer.py
31 32 33 34 35 36 37 38 39 40 41 42 43 | |
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 | |
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 | |
ToolResult
¶
Bases: TypedDict
Result of a tool execution.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
list[ToolResultContent]
|
List of result content returned by the tool. |
status |
ToolResultStatus
|
The status of the tool execution ("success" or "error"). |
toolUseId |
str
|
The unique identifier of the tool use request that produced this result. |
Source code in strands/types/tools.py
87 88 89 90 91 92 93 94 95 96 97 98 | |
ToolUse
¶
Bases: TypedDict
A request from the model to use a specific tool with the provided input.
Attributes:
| Name | Type | Description |
|---|---|---|
input |
Any
|
The input parameters for the tool. Can be any JSON-serializable type. |
name |
str
|
The name of the tool to invoke. |
toolUseId |
str
|
A unique identifier for this specific tool use request. |
Source code in strands/types/tools.py
52 53 54 55 56 57 58 59 60 61 62 63 64 | |
Tracer
¶
Handles OpenTelemetry tracing.
This class provides a simple interface for creating and managing traces, with support for sending to OTLP endpoints.
When the OTEL_EXPORTER_OTLP_ENDPOINT environment variable is set, traces are sent to the OTLP endpoint.
Both attributes are controlled by including "gen_ai_latest_experimental" or "gen_ai_tool_definitions", respectively, in the OTEL_SEMCONV_STABILITY_OPT_IN environment variable.
Source code in strands/telemetry/tracer.py
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 124 125 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 175 176 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 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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 | |
__init__()
¶
Initialize the tracer.
Source code in strands/telemetry/tracer.py
89 90 91 92 93 94 95 96 97 98 99 100 101 | |
end_agent_span(span, response=None, error=None)
¶
End an agent span with results and metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span
|
Span
|
The span to end. |
required |
response
|
Optional[AgentResult]
|
The response from the agent. |
None
|
error
|
Optional[Exception]
|
Any error that occurred. |
None
|
Source code in strands/telemetry/tracer.py
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | |
end_event_loop_cycle_span(span, message, tool_result_message=None, error=None)
¶
End an event loop cycle span with results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span
|
Span
|
The span to end. |
required |
message
|
Message
|
The message response from this cycle. |
required |
tool_result_message
|
Optional[Message]
|
Optional tool result message if a tool was called. |
None
|
error
|
Optional[Exception]
|
Optional exception if the cycle failed. |
None
|
Source code in strands/telemetry/tracer.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
end_model_invoke_span(span, message, usage, metrics, stop_reason, error=None)
¶
End a model invocation span with results and metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span
|
Span
|
The span to end. |
required |
message
|
Message
|
The message response from the model. |
required |
usage
|
Usage
|
Token usage information from the model call. |
required |
metrics
|
Metrics
|
Metrics from the model call. |
required |
stop_reason
|
StopReason
|
The reason the model stopped generating. |
required |
error
|
Optional[Exception]
|
Optional exception if the model call failed. |
None
|
Source code in strands/telemetry/tracer.py
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 | |
end_span_with_error(span, error_message, exception=None)
¶
End a span with error status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span
|
Span
|
The span to end. |
required |
error_message
|
str
|
Error message to set in the span status. |
required |
exception
|
Optional[Exception]
|
Optional exception to record in the span. |
None
|
Source code in strands/telemetry/tracer.py
224 225 226 227 228 229 230 231 232 233 234 235 236 | |
end_swarm_span(span, result=None)
¶
End a swarm span with results.
Source code in strands/telemetry/tracer.py
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 | |
end_tool_call_span(span, tool_result, error=None)
¶
End a tool call span with results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span
|
Span
|
The span to end. |
required |
tool_result
|
Optional[ToolResult]
|
The result from the tool execution. |
required |
error
|
Optional[Exception]
|
Optional exception if the tool call failed. |
None
|
Source code in strands/telemetry/tracer.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
start_agent_span(messages, agent_name, model_id=None, tools=None, custom_trace_attributes=None, tools_config=None, **kwargs)
¶
Start a new span for an agent invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
Messages
|
List of messages being sent to the agent. |
required |
agent_name
|
str
|
Name of the agent. |
required |
model_id
|
Optional[str]
|
Optional model identifier. |
None
|
tools
|
Optional[list]
|
Optional list of tools being used. |
None
|
custom_trace_attributes
|
Optional[Mapping[str, AttributeValue]]
|
Optional mapping of custom trace attributes to include in the span. |
None
|
tools_config
|
Optional[dict]
|
Optional dictionary of tool configurations. |
None
|
**kwargs
|
Any
|
Additional attributes to add to the span. |
{}
|
Returns:
| Type | Description |
|---|---|
Span
|
The created span, or None if tracing is not enabled. |
Source code in strands/telemetry/tracer.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
start_event_loop_cycle_span(invocation_state, messages, parent_span=None, custom_trace_attributes=None, **kwargs)
¶
Start a new span for an event loop cycle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
invocation_state
|
Any
|
Arguments for the event loop cycle. |
required |
parent_span
|
Optional[Span]
|
Optional parent span to link this span to. |
None
|
messages
|
Messages
|
Messages being processed in this cycle. |
required |
custom_trace_attributes
|
Optional[Mapping[str, AttributeValue]]
|
Optional mapping of custom trace attributes to include in the span. |
None
|
**kwargs
|
Any
|
Additional attributes to add to the span. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Span]
|
The created span, or None if tracing is not enabled. |
Source code in strands/telemetry/tracer.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
start_model_invoke_span(messages, parent_span=None, model_id=None, custom_trace_attributes=None, **kwargs)
¶
Start a new span for a model invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
Messages
|
Messages being sent to the model. |
required |
parent_span
|
Optional[Span]
|
Optional parent span to link this span to. |
None
|
model_id
|
Optional[str]
|
Optional identifier for the model being invoked. |
None
|
custom_trace_attributes
|
Optional[Mapping[str, AttributeValue]]
|
Optional mapping of custom trace attributes to include in the span. |
None
|
**kwargs
|
Any
|
Additional attributes to add to the span. |
{}
|
Returns:
| Type | Description |
|---|---|
Span
|
The created span, or None if tracing is not enabled. |
Source code in strands/telemetry/tracer.py
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 | |
start_multiagent_span(task, instance, custom_trace_attributes=None)
¶
Start a new span for swarm invocation.
Source code in strands/telemetry/tracer.py
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 | |
start_tool_call_span(tool, parent_span=None, custom_trace_attributes=None, **kwargs)
¶
Start a new span for a tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
ToolUse
|
The tool being used. |
required |
parent_span
|
Optional[Span]
|
Optional parent span to link this span to. |
None
|
custom_trace_attributes
|
Optional[Mapping[str, AttributeValue]]
|
Optional mapping of custom trace attributes to include in the span. |
None
|
**kwargs
|
Any
|
Additional attributes to add to the span. |
{}
|
Returns:
| Type | Description |
|---|---|
Span
|
The created span, or None if tracing is not enabled. |
Source code in strands/telemetry/tracer.py
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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
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 | |
get_tracer()
¶
Get or create the global tracer.
Returns:
| Type | Description |
|---|---|
Tracer
|
The global tracer instance. |
Source code in strands/telemetry/tracer.py
872 873 874 875 876 877 878 879 880 881 882 883 | |
serialize(obj)
¶
Serialize an object to JSON with consistent settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to serialize |
required |
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation of the object |
Source code in strands/telemetry/tracer.py
886 887 888 889 890 891 892 893 894 895 | |