You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/tutorials/10_agentic/10_temporal/060_open_ai_agents_sdk_hello_world/README.md
+53-4Lines changed: 53 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,53 @@
1
1
# [Temporal] OpenAI Agents SDK - Hello World
2
2
3
+
**Part of the [OpenAI SDK + Temporal integration series](../README.md)**
4
+
3
5
## What You'll Learn
4
6
5
7
The OpenAI Agents SDK plugin automatically converts LLM calls into durable Temporal activities. When `Runner.run()` executes, the LLM invocation becomes an `invoke_model_activity` visible in Temporal UI with full observability, automatic retries, and durability.
6
8
7
9
**Key insight:** You don't need to wrap agent calls in activities manually - the plugin handles this automatically, making non-deterministic LLM calls work seamlessly in Temporal workflows.
8
10
11
+
## Prerequisites
12
+
13
+
1. Agentex backend running with Temporal (`make dev` in agentex repo)
14
+
2. OpenAI API key configured (see setup below)
15
+
16
+
## Setup
17
+
18
+
This tutorial uses the OpenAI Agents SDK plugin, which needs to be added in two places:
19
+
20
+
### 1. Add Plugin to ACP (`project/acp.py`)
21
+
```python
22
+
from agentex.lib.plugins.openai_agents import OpenAIAgentsPlugin
23
+
24
+
acp = FastACP.create(
25
+
config=TemporalACPConfig(
26
+
plugins=[OpenAIAgentsPlugin()] # Add this
27
+
)
28
+
)
29
+
```
30
+
31
+
### 2. Add Plugin to Worker (`project/run_worker.py`)
32
+
```python
33
+
from agentex.lib.plugins.openai_agents import OpenAIAgentsPlugin
34
+
35
+
worker = AgentexWorker(
36
+
task_queue=task_queue_name,
37
+
plugins=[OpenAIAgentsPlugin()], # Add this
38
+
)
39
+
```
40
+
41
+
### 3. Configure OpenAI API Key
42
+
Add to `manifest.yaml`:
43
+
```yaml
44
+
secrets:
45
+
- name: OPENAI_API_KEY
46
+
value: "your-openai-api-key-here"
47
+
```
48
+
49
+
Or set in `.env` file: `OPENAI_API_KEY=your-key-here`
50
+
9
51
## Quick Start
10
52
11
53
```bash
@@ -18,10 +60,17 @@ uv run agentex agents run --manifest manifest.yaml
18
60
## Try It
19
61
20
62
1. Send a message to the agent (it responds in haikus)
21
-
2. Open Temporal UI at http://localhost:8080
22
-
3. Find your workflow execution
23
-
4. Look for the `invoke_model_activity` - this was created automatically
The activity shows the external call with retry capability. Each step (model invocation → tool call → model invocation) is durable.
49
+
50
+
### Pattern 2: Multi-Activity Tool (Optional)
51
+
52
+
To try the advanced banking example, uncomment the `move_money` sections in the code, then ask to move money.
53
+
54
+
1. Check the agent response:
33
55
34
-
**Pattern 2 (commented out in code):** Uncomment the `move_money` section, then ask to move money
35
-
- See TWO sequential activities: `withdraw_money` → `deposit_money`
36
-
- If the system crashes after withdraw but before deposit, Temporal resumes exactly where it left off
37
-
- The deposit will still happen - guaranteed transactional integrity
56
+

57
+
58
+
2. Open Temporal UI and see TWO sequential activities:
59
+
60
+

61
+
62
+
- First: `withdraw_money` activity executes
63
+
- Then: `deposit_money` activity executes
64
+
- Each activity shows its parameters and execution time
65
+
66
+
**Critical insight:** If the system crashes after withdraw but before deposit, Temporal resumes exactly where it left off. The deposit will still happen - guaranteed transactional integrity.
38
67
39
68
## Key Code
40
69
70
+
### Pattern 1: Single Activity Tool
41
71
```python
42
-
# Pattern 1: Direct activity conversion
72
+
# Define the activity
73
+
@activity.defn
74
+
asyncdefget_weather(city: str) -> str:
75
+
"""Get the weather for a given city"""
76
+
# This could be an API call - Temporal handles retries
77
+
returnf"The weather in {city} is sunny"
78
+
79
+
# Use activity_as_tool to convert it
43
80
weather_agent = Agent(
81
+
name="Weather Assistant",
82
+
instructions="Use the get_weather tool to answer weather questions.",
instructions="Use move_money to transfer funds between accounts.",
129
+
tools=[move_money]
130
+
)
52
131
```
53
132
133
+
## When to Use Each Pattern
134
+
135
+
### Use Pattern 1 when:
136
+
- Tool performs a single external operation (API call, DB query)
137
+
- Operation is already idempotent
138
+
- No sequencing guarantees needed
139
+
140
+
### Use Pattern 2 when:
141
+
- Tool requires multiple sequential operations
142
+
- Order must be guaranteed (withdraw THEN deposit)
143
+
- Operations need to be atomic from the agent's perspective
144
+
- You want transactional integrity across steps
145
+
54
146
## Why This Matters
55
147
56
148
**Without Temporal:** If you withdraw money but crash before depositing, you're stuck in a broken state. The money is gone from the source account with no way to recover.
57
149
58
-
**With Temporal:** Guaranteed execution with exact resumption after failures. Either both operations complete, or the workflow can handle partial completion. This is what makes agents production-ready for real-world operations like financial transactions, order fulfillment, or any multi-step process.
150
+
**With Temporal (Pattern 2):**
151
+
- Guaranteed execution with exact resumption after failures
152
+
- If the system crashes after withdraw, Temporal resumes and completes deposit
153
+
- Each step is tracked and retried independently
154
+
- Full observability of the entire operation
155
+
156
+
**Key insight:** Pattern 2 moves sequencing control from the LLM (which might call tools in wrong order) to your deterministic code (which guarantees correct order). The LLM still decides *when* to call the tool, but your code controls *how* the operations execute.
59
157
60
-
**Key insight:** Pattern 2 moves sequencing control from the LLM (which might call tools in wrong order) to your deterministic code (which guarantees correct order).
158
+
This makes agents production-ready for:
159
+
- Financial transactions
160
+
- Order fulfillment workflows
161
+
- Multi-step API integrations
162
+
- Any operation where partial completion is dangerous
0 commit comments