@@ -3,9 +3,15 @@ title: Visualize Agent Trajectories
33description : Show users the step-by-step reasoning process of your agent.
44---
55
6- ## Basic Usage
6+ The Trajectory extension allows you to visualize the step-by-step reasoning process of your agent as it runs.
7+ Trajectory steps appear as expandable sections in the UI, helping users understand your agent's thought process,
8+ making the interaction more transparent and trustworthy.
9+
10+ ## Example: Basic Usage
711
812``` python
13+ import os
14+ from time import sleep
915from typing import Annotated
1016from a2a.types import Message
1117from agentstack_sdk.server import Server
@@ -14,8 +20,10 @@ from agentstack_sdk.a2a.extensions import TrajectoryExtensionServer, TrajectoryE
1420
1521server = Server()
1622
17- @server.agent ()
18- async def my_agent (
23+ @server.agent (
24+ name = " Docs Trajectory Agent" ,
25+ )
26+ async def docs_trajectory_agent (
1927 input : Message,
2028 context : RunContext,
2129 trajectory : Annotated[TrajectoryExtensionServer, TrajectoryExtensionSpec()]
@@ -24,17 +32,41 @@ async def my_agent(
2432 title = " Planning" ,
2533 content = " Analyzing the user request to determine the best approach..."
2634 )
27-
28- # Do work
35+ sleep(3 ) # Sleep so that you can watch the trajectory steps unfold
2936
3037 yield trajectory.trajectory_metadata(
3138 title = " Execution" ,
32- content = " Processing data with temperature=0.7"
39+ content = " Processing data with temperature=0.7... "
3340 )
41+ sleep(3 )
42+
43+ yield " Final result goes here"
44+
45+
46+ def run ():
47+ server.run(host = os.getenv(" HOST" , " 127.0.0.1" ), port = int (os.getenv(" PORT" , 8000 )))
3448
35- yield " Final result"
49+
50+ if __name__ == " __main__" :
51+ run()
3652```
3753
54+ <Steps >
55+ <Step title = " Import the trajectory extension" >
56+ Import ` TrajectoryExtensionServer ` and ` TrajectoryExtensionSpec ` from ` agentstack_sdk.a2a.extensions ` .
57+ </Step >
58+
59+ <Step title = " Inject the extension" >
60+ Add a trajectory parameter to your agent function using the ` Annotated ` type hint
61+ with ` TrajectoryExtensionSpec() ` .
62+ </Step >
63+
64+ <Step title = " Yield trajectory metadata" >
65+ Call ` yield trajectory.trajectory_metadata() ` with a ` title ` and ` content ` to
66+ add a step to the trajectory.
67+ </Step >
68+ </Steps >
69+
3870## Markdown Support
3971
4072The ` content ` field of ` trajectory_metadata ` supports Markdown, which is rendered directly in the UI.
@@ -47,15 +79,16 @@ Supported elements include:
4779- Tables
4880- Code blocks
4981- Links
82+ - Checklists
5083
5184``` python
5285yield trajectory.trajectory_metadata(
5386 title = " Checklist" ,
5487 content = """
55- - Load data
56- - Validate schema
57- - Run inference
58- - Generate report
88+ - [x] Load data
89+ - [x] Validate schema
90+ - [ ] Run inference
91+ - [ ] Generate report
5992"""
6093)
6194```
@@ -73,18 +106,22 @@ yield trajectory.trajectory_metadata(
73106)
74107
75108# Update with results
109+ sleep(3 )
76110yield trajectory.trajectory_metadata(
77111 content = " Found 8 results" ,
78112 group_id = " websearch"
79113)
114+ sleep(3 )
80115yield trajectory.trajectory_metadata(
81116 content = " Found 8 results\n Analyzed 3/8 results" ,
82117 group_id = " websearch"
83118)
119+ sleep(3 )
84120yield trajectory.trajectory_metadata(
85121 content = " Found 8 results\n Analyzed 8/8 results" ,
86122 group_id = " websearch"
87123)
124+ sleep(3 )
88125
89126# Final update
90127yield trajectory.trajectory_metadata(
@@ -145,4 +182,6 @@ yield trajectory.trajectory_metadata(
145182)
146183```
147184
148- Trajectory steps appear as expandable sections in the UI, helping users understand your agent's thought process.
185+ ## Example: Advanced Usage
186+
187+ For a more advanced example, see [ trajectory_agent.py] ( https://github.com/i-am-bee/agentstack/blob/main/apps/agentstack-sdk-py/examples/trajectory_agent.py ) .
0 commit comments