OpenAI + @Temporalio : Building Durable, Production Ready Agents - Cornelia Davis, Temporal
Explore how Temporal, a durable execution framework, brings resilience and scalability to AI agents built with the OpenAI Agents SDK. This summary covers Temporal's core concepts of Workflows and Activities, the official integration that makes OpenAI agents durable, and patterns for orchestrating multiple micro-agents.
The paradigm of AI agents, where LLMs are given agency to decide the flow of an application, introduces significant distributed systems challenges. AI agents are often long-running and interact with numerous external, sometimes unreliable, services. This raises critical questions about their resilience:
- How can they survive network flakiness or infrastructure instability?
- How do they handle rate-limiting from LLMs or other APIs?
- How can a long-running process, potentially lasting days or weeks, recover from a crash without losing its entire state and progress?
Temporal is an open-source, durable execution framework designed to solve these exact problems. It is not a new technology created for the AI boom but a mature one used in production by companies like Snapchat, Airbnb, and even OpenAI for products like Codex and image generation. Temporal allows developers to focus on the “happy path” of their business logic—in this case, the agent’s logic—while the framework handles the complexities of failure recovery.
Temporal’s Foundational Abstractions
Temporal operates as a backing service, much like a database or a message queue, providing durability for your applications. Its power comes from two core abstractions:
-
Activities: An Activity is a function that encapsulates a single unit of work, especially one that involves external interactions that might fail, such as calling an LLM, querying a database, or invoking a third-party API. By decorating a function as an Activity, you signal to the Temporal SDK that it should be managed.
-
Workflows: A Workflow is the orchestrator of Activities. It defines the business logic and the sequence of operations. From the developer’s perspective, a Workflow looks like a standard piece of code, calling one Activity after another.
The magic happens when you combine them. The Temporal SDK, working with the Temporal service, automatically provides:
- Automatic Retries: If an Activity fails (e.g., an LLM call is rate-limited), Temporal will automatically retry it based on a configurable policy (e.g., exponential backoff).
- State Management: Temporal uses an event-sourcing approach to persist the state of a Workflow. Every time an Activity completes, its result is recorded. If the process running the Workflow crashes, Temporal can replay the event history to restore the application’s state to exactly where it was and continue execution.
This means if your agent crashes on the 1,350th turn with an LLM, you will not have to re-run and re-pay for the previous 1,349 calls. Temporal resumes from the point of failure.
- Built-in Queuing: What appears as a direct function call from a Workflow to an Activity is actually a distributed, durable task queue system under the hood, eliminating the need to manage complex infrastructure like Kafka yourself.
A demonstration of a Temporal-native agentic loop showed this durability in action. An agent designed to check for weather alerts was run, and the worker process was intentionally killed mid-execution. Upon restarting the worker, the workflow seamlessly picked up from the exact point of failure, having preserved its entire state, including conversation history and the results of previous tool calls.
Integrating Temporal with the OpenAI Agents SDK
Recognizing the need for resilience in AI agents, OpenAI and Temporal collaborated on an official integration. This allows developers to use the high-level, expressive power of the OpenAI Agents SDK while gaining the industrial-strength durability of Temporal.
The integration was made possible by making the runner class in the OpenAI Agents SDK abstract. Temporal provides its own implementation of this runner, which wraps the agent’s execution logic—including the LLM calls themselves—in durable primitives.
To enable this, you simply:
- Define your tools as Temporal Activities. The integration provides a helper function,
activity_as_tool, to easily generate the necessary JSON schema for the LLM from your Python function and its docstrings. - Define your agent using the standard OpenAI Agents SDK syntax, passing it the activities-as-tools.
- Wrap the agent’s execution (
runner.run) inside a Temporal Workflow. - Configure the Temporal worker with the
OpenAI agents plugin.
With this setup, the entire agentic loop becomes durable. A crash and restart of the worker process will not terminate the agent; it will resume automatically, just as in the native Temporal example.
Orchestrating Durable Micro-Agents
The OpenAI Agents SDK encourages a paradigm of building small, focused “micro-agents” and orchestrating them to accomplish more complex tasks. Temporal enhances this pattern by making the orchestration itself durable. There are two primary methods for this:
-
Just Code: Since the agent execution is wrapped in a Workflow, you can use standard programming logic to orchestrate agents. You can run one agent, take its output, and pass it as input to another. You can run agents in parallel and wait for their results. This orchestration logic is fully durable.
-
Handoffs: The SDK has a built-in concept of “handoffs,” where an agent can delegate a task to another specialized agent. This happens within a single agentic loop, effectively changing the context or “persona” of the loop. Temporal’s integration fully supports this model, ensuring that even these internal state transitions are durable.