Skip to content

Commit 878d04d

Browse files
committed
wip
1 parent 091d942 commit 878d04d

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

temporalio/contrib/openai_agents/README.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
# OpenAI Agents SDK Integration for Temporal
22

3-
⚠️ **Public Preview** - The interface to this module is subject to change prior to General Availability. We welcome your questions and feedback in the [#python-sdk](https://temporalio.slack.com/archives/CTT84RS0P) Slack channel at [temporalio.slack.com](https://temporalio.slack.com/).
3+
⚠️ **Public Preview** - The interface to this module is subject to change prior to General Availability.
4+
We welcome questions and feedback in the [#python-sdk](https://temporalio.slack.com/archives/CTT84RS0P) Slack channel at [temporalio.slack.com](https://temporalio.slack.com/).
45

56

67
## Introduction
78

89
This integration combines [OpenAI Agents SDK](https://github.com/openai/openai-agents-python) with [Temporal's durable execution](https://docs.temporal.io/evaluate/understanding-temporal#durable-execution).
9-
It allows you to build AI agents that never lose their progress and handle long-running, asynchronous, and human-in-the-loop workflows with ease.
10+
It allows you to build durable agents that never lose their progress and handle long-running, asynchronous, and human-in-the-loop workflows with production-grade reliability.
1011

12+
Temporal and OpenAI Agents SDK are complementary technologies, both of which contribute to simplifying what it takes to build highly capable, high-quality AI systems.
1113
Temporal provides a crash-proof system foundation, taking care of the distributed systems challenges inherent to production agentic systems.
1214
OpenAI Agents SDK offers a lightweight yet powerful framework for defining those agents.
13-
The combination lets you build reliable agentic systems quickly.
1415

1516
This document is organized as follows:
1617
- **[Hello World Agent](#hello-world-durable-agent).** Your first durable agent example.
1718
- **[Background Concepts](#core-concepts).** Background on durable execution and AI agents.
18-
- **[Complete Example](#complete-example)** Complete example.
19-
- **Usage Guide.**
20-
- **Agent Patterns.**
19+
- **[Full Example](#full-example)** Complete example.
20+
- **[Usage Guide].**
21+
[TODO: Complete links]
22+
2123

22-
The [samples repository](https://github.com/temporalio/samples-python/tree/main/openai_agents) contains a number of examples spanning various use cases.
24+
The [samples repository](https://github.com/temporalio/samples-python/tree/main/openai_agents) contains examples including basic usage, common agent patterns, and more complete samples.
2325

2426

2527
## Hello World Durable Agent
2628

27-
The code below shows how straightforward it is to wrap an agent wrapped in durable execution.
29+
The code below shows how to wrap an agent for durable execution.
2830

2931
### File 1: Durable Agent (`hello_world.py`)
3032

@@ -45,20 +47,23 @@ class HelloWorldAgent:
4547
return result.final_output
4648
```
4749

50+
In this example, Temporal provides the durable execution wrapper: the `HelloWorldAgent.run` method.
51+
The content of that method, is regular OpenAI Agents SDK code.
52+
4853
If you are familiar with Temporal and with Open AI Agents SDK, this code will look very familiar.
49-
We annotate the `HelloWorldAgent` class with `@workflow.defn` to define a workflow, then use the `@workflow.run` annotation to define the entrypoint.
54+
We the `@workflow.defn` annotations on the `HelloWorldAgent` indicates that this class will contain durable execution and the `@workflow.run` annotation defines the entrypoint.
55+
We use the `Agent` class from OpenAI Agents SDK to define a simple agent, instructing it to always responds with haikus.
56+
We then run that agent, using the `Runner` class from OpenAI Agents SDK, passing through `prompt` as an argument.
5057

51-
We use the `Agent` class to define a simple agent, instructing it to always responds with haikus.
52-
Within the workflow, we start the agent using the `Runner`, as is typical, passing through `prompt` as an argument.
5358

54-
We will [complete this example below](#complete-example).
55-
However, before digging further into the code, it we will share some more background to set the stage.
59+
We will [complete this example below](#full-example).
60+
Before digging further into the code, we will review some background that will make it easier to understand.
5661

5762
## Background Concepts
5863

59-
We encourage you to form a thorough understanding of AI agents and durable execution with Temporal.
60-
Understanding this will make it easer to design and build durable agents.
61-
If you are well versed in these topics, you may skim this section or skip ahead.
64+
We encourage you to review this section thoroughly to gain a solid understanding of AI agents and durable execution with Temporal.
65+
This knowledge will make it easier to design and build durable agents.
66+
If you are already well versed in these topics, feel free to skim this section or skip ahead.
6267

6368
### AI Agents
6469

@@ -74,10 +79,9 @@ We describe each of these briefly:
7479
- *Handoffs*. A handoff occurs when an agent delegates a task to another agent. During a handoff the conversation history remains the same, and passes to a new agent with its own model, instructions, tools.
7580
- *Context*. This is an overloaded term. Here, context refers to a framework object that is shared across tools and other code, but is not passed to the model.
7681

77-
78-
Now, let's look at how these pieces can fit together.
79-
In one popular pattern, the model receives user input, then performs reasoning to select a tool to call.
80-
The response from the tool is fed back into the model, which may perform additional tool calls, iterating until the task is complete.
82+
Now, let's see how these components work together.
83+
In a common pattern, the model first receives user input and then reasons about which tool to invoke.
84+
The tool's response is passed back to the model, which may call additional tools, repeating this loop until the task is complete.
8185

8286
The diagram below illustrates this flow.
8387

@@ -109,27 +113,31 @@ The diagram below illustrates this flow.
109113
again, until task is complete)
110114
```
111115

112-
Even in a simple example like this, there are many places where something can go wrong.
113-
Tools call APIs that are sometimes down and models have rate limits, requiring retries.
116+
Even in a simple example like this, there are many places where things can go wrong.
117+
Tools call APIs that sometimes fail, while models can encounter rate limits, requiring retries.
114118
The longer the agent runs, the more costly it is to start the job over.
115-
In the next section, we turn to durable execution, which can handle such failures seamlessly.
119+
We next describe durable execution, which handles such failures seamlessly.
116120

117121
### Durable Execution
118122

119123
In Temporal's durable execution implementation, a program that crashes or encounters an exception while interacting with a model or API will retry until it can successfully complete.
120124

121-
Temporal relies heavily on a replay mechanism to recover from failures.
125+
Temporal relies primarily on a replay mechanism to recover from failures.
122126
As the program makes progress, Temporal saves key inputs and decisions, allowing a re-started program to pick up right where it left off.
123127

124128
The key to making this work is to separate the applications repeatable (deterministic) and non-repeatable (non-deterministic) parts:
125129

126-
1. Deterministic pieces, termed *workflows*, execute the same way if re-run with the same inputs.
127-
2. Non-deterministic pieces, termed *activities*, have no limitations—they may perform I/O and any other operations.
130+
1. Deterministic pieces, termed *workflows*, execute the same way when re-run with the same inputs.
131+
2. Non-deterministic pieces, termed *activities*, can run arbitrary code, performing I/O and any other operations.
132+
133+
Workflow code can run for extended periods and, if interrupted, resume exactly where it left off.
134+
Activity code faces no restrictions on I/O or external interactions, but if it fails part-way through it restarts from the beginning.
128135

129-
In the AI agent described in the previous section, model and tool calls run in activities, and the control flow linking them together runs in the workflow.
136+
In the AI-agent example above, model invocations and tool calls run inside activities, while the logic that coordinates them lives in the workflow.
137+
This pattern generalizes to more sophisticated agents.
138+
We refer to that coordinating logic as *agent orchestration*.
130139

131-
In more complex examples, the control flow may be described as *agent orchestration*.
132-
Agent orchestration runs within the Temporal workflow, while model calls and any tool calls involving I/O run in activities.
140+
As a general rule, agent orchestration code executes within the Temporal workflow, whereas model calls and any I/O-bound tool invocations execute as Temporal activities.
133141

134142
The diagram below shows the overall architecture of an agentic application in Temporal.
135143
The Temporal Server is responsible to tracking program execution and making sure associated state is preserved reliably.

0 commit comments

Comments
 (0)