Skip to content

Commit 7300603

Browse files
authored
docs: explains basic trajectory example and how to run it (#1994)
Signed-off-by: Mark Sturdevant <mark.sturdevant@ibm.com>
1 parent 692fd20 commit 7300603

2 files changed

Lines changed: 102 additions & 24 deletions

File tree

docs/development/agent-integration/trajectory.mdx

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ title: Visualize Agent Trajectories
33
description: 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
915
from typing import Annotated
1016
from a2a.types import Message
1117
from agentstack_sdk.server import Server
@@ -14,8 +20,10 @@ from agentstack_sdk.a2a.extensions import TrajectoryExtensionServer, TrajectoryE
1420

1521
server = 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

4072
The `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
5285
yield 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)
76110
yield trajectory.trajectory_metadata(
77111
content="Found 8 results",
78112
group_id="websearch"
79113
)
114+
sleep(3)
80115
yield trajectory.trajectory_metadata(
81116
content="Found 8 results\nAnalyzed 3/8 results",
82117
group_id="websearch"
83118
)
119+
sleep(3)
84120
yield trajectory.trajectory_metadata(
85121
content="Found 8 results\nAnalyzed 8/8 results",
86122
group_id="websearch"
87123
)
124+
sleep(3)
88125

89126
# Final update
90127
yield 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).

docs/stable/agent-integration/trajectory.mdx

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ title: Visualize Agent Trajectories
33
description: 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
915
from typing import Annotated
1016
from a2a.types import Message
1117
from agentstack_sdk.server import Server
@@ -14,8 +20,10 @@ from agentstack_sdk.a2a.extensions import TrajectoryExtensionServer, TrajectoryE
1420

1521
server = 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

4072
The `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
5285
yield 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)
76110
yield trajectory.trajectory_metadata(
77111
content="Found 8 results",
78112
group_id="websearch"
79113
)
114+
sleep(3)
80115
yield trajectory.trajectory_metadata(
81116
content="Found 8 results\nAnalyzed 3/8 results",
82117
group_id="websearch"
83118
)
119+
sleep(3)
84120
yield trajectory.trajectory_metadata(
85121
content="Found 8 results\nAnalyzed 8/8 results",
86122
group_id="websearch"
87123
)
124+
sleep(3)
88125

89126
# Final update
90127
yield 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

Comments
 (0)